QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#880817#9983. Color-Balanced Treemortis_lifeWA 1ms3840kbC++141.1kb2025-02-03 21:11:052025-02-03 21:11:06

Judging History

This is the latest submission verdict.

  • [2025-02-03 21:11:06]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3840kb
  • [2025-02-03 21:11:05]
  • Submitted

answer

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;
int n , a; bool vis[N];
struct node {
	int to , nex;
}e[N]; int cnt , h[N] , col[N];
void add(int u , int v) {
	e[++ cnt] = (node){v , h[u]}; h[u] = cnt;
}
void dfs(int x , int fa) {
	col[x] = 1-col[fa];
	if(col[x] == 1) a ++;
	for(int i = h[x];i;i = e[i].nex) {
		if(e[i].to == fa) continue;
		dfs(e[i].to , x);
		vis[x] = 0;
	}
}

int main() {
	int t; cin >> t;
	while(t --) {
		cin >> n; cnt = 0;
		for(int i = 1;i <= 2 * n;i ++) 
			vis[i] = 1 , h[i] = 0;
		for(int i = 1 , u , v;i < 2 * n;i ++) {
			scanf("%d%d",&u,&v);
			add(u , v); add(v , u);
		}
		col[0] = 1; dfs(1 , 0);
	//	cout << n << endl;
		for(int i = 1;i <= 2 * n;i ++) {
			if(vis[i]) {
				if(a > n && (col[i] == 1)) col[i] = 0 , a --;
				if(a < n && (col[i] == 0)) col[i] = 1 , a ++;
			}
			printf("%d ",col[i]);
		}
	}
	
	return 0;
}
/*
3 1 2 2 3 2 4 4 5 4 6

4
3 1 2 2 3 2 4 4 5 4 6
3 1 2 1 3 1 4 1 5 1 6
4 1 2 2 3 3 4 4 5 5 6 6 7 7 8
5 1 2 1 4 4 3 4 5 5 6 5 8 8 7 8 9 9 10
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

0 1 0 0 1 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0 1 0 0 

result:

wrong answer The number of black vertices and red vertices are not equal (test case 2)