QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#629477#7502. Painting the Roadsxyz123WA 18ms40840kbC++233.0kb2024-10-11 12:17:432024-10-11 12:17:44

Judging History

你现在查看的是最新测评结果

  • [2024-10-11 12:17:44]
  • 评测
  • 测评结果:WA
  • 用时:18ms
  • 内存:40840kb
  • [2024-10-11 12:17:43]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long T,a,b,c,d[1000001],v[1000001],o,h[1000001],fa[1000001],q,w,e,an,cn,fac[1000001],inv[1000001],st[1000001],u[1000001],e1,e2,dp[5001][10001],lim=5000,inf=1e12,ls[1000001];
long long y[1000001];
int si[1000001],si1[1000001],sm[1000001];
char s[1000001];
struct p{long long q,w,e;}l[1000001];
long long pow_(long long qq,long long ww){long long ee=1;while(ww){if(ww&1) ee*=qq,ee%=mod;qq*=qq,qq%=mod,ww>>=1;}return ee%mod;}
inline int read(){int x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}return x*f;}
void add(long long qq,long long ww){l[++o].q=ww,l[o].w=h[qq],h[qq]=o;}
long long gcd(long long qq,long long ww){return !ww?qq:gcd(ww,qq%ww);}
long long find(long long qq){return qq==fa[qq]?qq:fa[qq]=find(fa[qq]);}
void merge(long long qq,long long ww){long long f1=find(qq),f2=find(ww);if(f1==f2) return;fa[f1]=f2;}
long long C(long long qq,long long ww){return fac[qq]*inv[ww]%mod*inv[qq-ww]%mod;}
vector<p> qu[1000001];
void dfs(int qq,int ww)
{
	sm[qq]=0;si[qq]=si1[qq]=0;
	for(int i=0;i<qu[qq].size();i++)
	{
		if(qu[qq][i].q==ww) continue;
		fa[qu[qq][i].q]=qu[qq][i].w;
		y[qu[qq][i].q]=qu[qq][i].e;
		dfs(qu[qq][i].q,qq);
		sm[qq]^=sm[qu[qq][i].q];
		si[qq]+=si[qu[qq][i].q];
		si1[qq]+=si1[qu[qq][i].q];
	}
	sm[qq]^=y[qq];
	si[qq]+=sm[qq];
	si1[qq]+=v[qq];
}
void dfs2(int qq,int ww)
{
	si[qq]=si1[qq]=0;
	si[qq]++;
	si1[qq]+=v[qq];
	for(int j=0;j<=v[qq];j++) dp[qq][lim+j]=0;
	for(int i=0;i<qu[qq].size();i++)
	{
		if(qu[qq][i].q==ww) continue;
		dfs2(qu[qq][i].q,qq);
		for(int j=lim-si[qq];j<=lim+si1[qq];j++) ls[j]=dp[qq][j],dp[qq][j]=inf;
		for(int j=lim-si[qq];j<=lim+si1[qq];j++)
		{
			for(int k=lim-si[qu[qq][i].q];k<=lim+si1[qu[qq][i].q];k++)
			{
				dp[qq][j+k-lim]=min(dp[qq][j+k-lim],ls[j]+dp[qu[qq][i].q][k]);
			}
		}
		si[qq]+=si[qu[qq][i].q];
		si1[qq]+=si1[qu[qq][i].q];
	}
	for(int i=lim-1;i>=lim-si[qq];i--) dp[qq][i]=min(dp[qq][i],dp[qq][i+1]);
	if(qq!=1)
	{
		for(int i=lim-si[qq];i<=lim+si1[qq];i++)
		{
			if(i%2!=y[qq]) dp[qq][i]=inf;
			else dp[qq][i]+=(long long)(abs(i-lim))*fa[qq];
//			cout<<qq<<" "<<i-lim<<" "<<dp[qq][i]<<" "<<y[qq]<<"\n";
		}
	}
}
int main()
{
//	freopen("1.in","r",stdin);
	srand((unsigned)(time(0)^(*new int)));
	fac[0]=1;for(int i=1;i<=1000000;i++) fac[i]=fac[i-1]*i%mod;
	inv[1000000]=pow_(fac[1000000],mod-2);for(int i=999999;i>=0;i--) inv[i]=inv[i+1]*(i+1)%mod;
	T=1;
	scanf("%lld",&T);
	for(int ii=1;ii<=T;ii++)
	{
		scanf("%lld%lld",&a,&b);
		for(int i=1;i<=a;i++)
		{
			for(int j=lim-max(a,b);j<=lim+max(a,b);j++) dp[i][j]=inf;
		}
		for(int i=1;i<=a;i++) qu[i].clear(),v[i]=0;
		for(int i=1;i<a;i++)
		{	
			scanf("%lld%lld%lld%lld",&q,&w,&e1,&e2);
			qu[q].push_back(p{w,e1,e2});
			qu[w].push_back(p{q,e1,e2}); 
		}
		for(int i=1;i<=b;i++) d[i]=read(),v[d[i]]++;
		dfs(1,0);sm[1]=0;
		dfs2(1,0);
		if(dp[1][lim]>=1e9) printf("-1\n");
		else printf("%lld\n",dp[1][lim]);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 40676kb

input:

5
3 2
1 2 1 1
2 3 2 1
1 3
4 2
1 2 3 1
2 3 1 0
3 4 4 1
1 2
5 4
1 2 3 0
2 3 1 1
3 4 2 0
4 5 2 1
1 1 1 1
5 2
1 2 2 1
1 3 3 0
1 5 2 1
3 4 1 1
1 2
10 5
1 2 10 1
2 3 3 1
3 4 4 0
4 5 4 1
5 6 2 1
2 7 8 0
2 8 9 1
4 9 1 0
1 10 4 0
10 10 2 1 8

output:

3
9
21
-1
42

result:

ok 5 number(s): "3 9 21 -1 42"

Test #2:

score: -100
Wrong Answer
time: 18ms
memory: 40840kb

input:

1000
5 5
1 2 4 1
2 3 9 0
3 4 10 1
3 5 8 1
1 5 2 5 1
5 3
1 2 7 1
1 3 7 0
2 4 9 0
3 5 4 1
3 4 3
5 3
1 2 7 1
2 3 1 0
1 4 7 1
4 5 5 1
4 4 3
5 1
1 2 3 1
1 3 6 0
2 4 10 0
2 5 7 0
1
5 3
1 2 10 1
1 3 10 0
1 4 1 1
3 5 4 0
2 5 2
5 5
1 2 7 0
1 3 5 0
2 4 8 1
2 5 10 0
2 2 3 5 4
5 4
1 2 6 1
1 3 4 0
3 4 4 0
1 5 5 ...

output:

22
-1
19
3
11
8
11
-1
-1
0
38
1
1
-1
-1
28
-1
-1
19
16
26
13
-1
-1
9
18
16
14
10
12
24
0
11
-1
17
-1
-1
14
27
-1
11
-1
6
6
15
18
46
0
14
9
-1
-1
8
22
-1
-1
17
-1
25
6
0
24
6
15
21
15
22
-1
6
0
-1
20
5
-1
20
0
20
-1
18
-1
10
0
-1
-1
27
-1
-1
11
11
4
-1
20
11
0
8
20
31
-1
-1
-1
8
-1
11
-1
9
13
-1
-1
1...

result:

wrong answer 8th numbers differ - expected: '7', found: '-1'