QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#212200#7502. Painting the Roadschenxinyang2006WA 1ms8080kbC++143.9kb2023-10-13 11:24:362023-10-13 11:24:36

Judging History

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

  • [2023-10-13 11:24:36]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:8080kb
  • [2023-10-13 11:24:36]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
	if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
	if(x > y) x = y;
}

inline int popcnt(int x){
	return __builtin_popcount(x);
}

inline int ctz(int x){
	return __builtin_ctz(x);
}


/*ll power(ll p,int k = mod - 2){
	ll ans = 1;
	while(k){
		if(k % 2 == 1) ans = ans * p % mod;
		p = p * p % mod;
		k /= 2;	
	}
	return ans;
}*/
int T,n,m;

int cnt;
int head[5005];
struct eg{
	int to,nxt,w,c;
}edge[10005];

void make(int u,int v,int w,int c){
	edge[++cnt].to = v;
	edge[cnt].w = w;
	edge[cnt].c = c;
	edge[cnt].nxt = head[u];
	head[u] = cnt;
}

int a[5005],inst[5005],dep[5005];
int siz[5005],sum[5005];//siz[u] 子树内 bot 的数量
void dfs(int u,int fa){
	inst[u] = a[u];
	for(int i = head[u];i;i = edge[i].nxt){
		int v = edge[i].to;
		if(v == fa) continue;
		a[v] = edge[i].c;
		dep[v] = dep[u] + edge[i].w;
		inst[u] ^= edge[i].c;
		dfs(v,u);
	}
}
int f[5005][5005],g[5005][5005],tf[5005],tg[5005];//f[u][i],u 子树内,多了 i 个 bot 点.g[u][i] u 子树内,多了 i 个标记点.g[u][0] 不存在
void mrg(int u,int v,int V){
	rep(i,0,siz[u] + siz[v]) tf[i] = inf;
	rep(i,0,sum[u] + sum[v]) tg[i] = inf;
	rep(i,0,siz[u]){
		rep(j,0,siz[v]) chkmin(tf[i],f[u][i] + f[v][j]);
		rep(j,0,sum[v]){
			if(i >= j) chkmin(tf[i - j],f[u][i] + g[v][j] - V * j);
			else chkmin(tg[j - i],f[u][i] + g[v][j] - V * i);
		}
	}
	rep(i,0,sum[u]){
		rep(j,0,sum[v]) chkmin(tg[i],g[u][i] + g[v][j]);
		rep(j,0,siz[v]){
			if(i > j) chkmin(tg[i - j],g[u][i] + f[v][j] - V * j);
			else chkmin(tf[j - i],g[u][i] + f[v][j] - V * i);
		}
	}
	rep(i,0,siz[u] + siz[v]) f[u][i] = tf[i];
	rep(i,0,sum[u] + sum[v]) g[u][i] = tg[i];
	siz[u] += siz[v];
	sum[u] += sum[v];
}

void dfs2(int u,int fa){
	inst[u] ^= (siz[u] % 2);
	sum[u] = inst[u];
	fill(f[u],f[u] + siz[u] + 1,inf);
	fill(g[u],g[u] + sum[u] + 1,inf);
	f[u][siz[u]] = siz[u] * dep[u];
	for(int i = head[u];i;i = edge[i].nxt){
		int v = edge[i].to;
		if(v == fa) continue;
		dfs2(v,u);
		mrg(u,v,2 * dep[u]);
	}
	if(u == 1) return;
	if(inst[u]){
		per(i,sum[u],2) g[u][i] = g[u][i - 1] + dep[u];
		g[u][1] = f[u][0] + dep[u];
//		printf("?? %d %d %d\n",u,f[u][0],g[u][1]);
		per(i,siz[u] - 1,0) f[u][i] = f[u][i + 1] - dep[u];
		f[u][siz[u]] = inf;
	}
	//允许 + 2 标记点
	per(i,siz[u],2) chkmin(f[u][i - 2],f[u][i] - 2 * dep[u]);
	if(sum[u] && siz[u]) chkmin(g[u][1],f[u][1]);
	if(sum[u] >= 2) chkmin(g[u][2],f[u][0] + 2 * dep[u]);
	rep(i,1,sum[u] - 2) chkmin(g[u][i + 2],g[u][i] + 2 * dep[u]);
}

void solve(){
	scanf("%d%d",&n,&m);
	cnt = 0;
	fill(head + 1,head + n + 1,0);
	fill(siz + 1,siz + n + 1,0);
	rep(i,1,n - 1){
		int u,v,w,c;
		scanf("%d%d%d%d",&u,&v,&w,&c);
		make(u,v,w,c);make(v,u,w,c);
	}
	dfs(1,0);
//	rep(u,1,n) printf("%d ",inst[u]);
//	printf("\n");
//	rep(u,1,n) printf("%d ",dep[u]);
//	printf("\n");
	rep(i,1,m){
		int u;
		scanf("%d",&u);
		siz[u]++;
	}
	dfs2(1,0);	
/*	rep(u,1,n){
		printf("node %d siz %d sum %d\n",u,siz[u],sum[u]);
		rep(i,0,siz[u]) printf("%d ",f[u][i]);
		printf("\n");
		rep(i,0,sum[u]) printf("%d ",g[u][i]);
		printf("\n");
	}*/
	int ans = inf;
	rep(i,0,m) chkmin(ans,f[1][i]);
	if(ans <= 250000000) printf("%d\n",ans);
	else printf("-1\n");
}

int main(){
//	freopen("test.in","r",stdin);
	scanf("%d",&T);
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 8080kb

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
-1

result:

wrong answer 5th numbers differ - expected: '42', found: '-1'