QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#863864#9983. Color-Balanced Treeucup-team139WA 29ms8588kbC++171.3kb2025-01-20 00:36:452025-01-20 00:36:55

Judging History

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

  • [2025-01-20 00:36:55]
  • 评测
  • 测评结果:WA
  • 用时:29ms
  • 内存:8588kb
  • [2025-01-20 00:36:45]
  • 提交

answer

#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
constexpr int MAXN{200010};

vector<int> g[MAXN];
int oddd_cnt, leaves[2];
int cnd[2];
bool ans[MAXN];

void dfs0(int n, int p, int d)
{
	ans[n] = d & 1;
	oddd_cnt += d & 1;
	if (g[n].size() == 1)
		leaves[d & 1]++;
	else
		cnd[d & 1] = n;
	for (int a : g[n])
		if (a != p)
			dfs0(a, n, d + 1);
}

int r;
void dfs1(int n, int p)
{
	if (r == 0)
		return;
	if (g[n].size() == 1)
	{
		ans[n] ^= 1;
		r--;
		if (r == 0)
			return;
	}
	for (int a : g[n])
		if (a != p)
			dfs1(a, n);
}

void solve()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= 2 * n; i++)
		g[i].clear();
	for (int i = 0, u, v; i < 2 * n - 1; i++)
	{
		scanf("%d %d", &u, &v);
		g[u].push_back(v);
		g[v].push_back(u);
	}
	oddd_cnt = leaves[0] = leaves[1] = 0;
	cnd[0] = cnd[1] = -1;
	dfs0(1, -1, 0);
	bool fail{false};
	if (oddd_cnt != n)
	{
		bool mfc{oddd_cnt > n};
		r = abs(oddd_cnt - n);
		if (leaves[mfc] >= r)
			dfs1(1, -1);
		else
			exit(1);
	}
	if (fail)
		printf("-1\n");
	else
	{
		for (int i = 1; i <= 2 * n; i++)
			printf("%d ", ans[i]);
		printf("\n");
	}
}

int main()
{
	int T;
	scanf("%d", &T);
	while (T--)
		solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 8536kb

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

result:

ok correct! (4 test cases)

Test #2:

score: -100
Wrong Answer
time: 29ms
memory: 8588kb

input:

10000
10
3 1
14 11
17 14
18 12
2 5
9 1
5 19
9 18
13 4
4 6
15 6
2 8
8 20
3 11
7 13
12 10
7 10
20 16
19 17
4
8 4
1 5
5 3
8 2
6 4
2 7
5 4
1
2 1
2
3 2
2 4
4 1
5
4 5
8 1
3 9
10 2
7 2
8 6
6 9
4 7
3 10
3
1 4
6 5
4 3
5 3
2 6
6
12 10
7 6
11 7
5 11
8 11
2 7
4 1
3 10
4 2
9 4
2 10
12
19 6
8 15
16 18
1 18
18 12
...

output:

0 1 1 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0 1 1 
0 0 0 0 1 1 1 1 
0 1 
0 0 1 1 
0 0 0 0 1 0 1 1 1 1 
0 1 0 1 1 0 
1 0 0 1 1 0 1 1 0 1 0 0 
1 0 0 1 0 1 1 1 0 1 1 0 0 1 0 0 0 1 0 0 0 0 1 0 
1 1 0 0 0 0 0 1 0 1 0 0 1 1 1 1 1 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 0 0 1 0 1 0 
0 1 0 1 
0 1 1 0 
1 0 1 1 1 0 1 0 0 0 0 1 0...

result:

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