字节10.9后端笔试第二题(珂朵莉的树)测试数据真没问题吗?
难吐了,我感觉我代码没有任何毛病啊,样例和自己编的数据都能过,为啥一直暴0
#include <bits/stdc++.h>
using namespace std;
int DFS(vector<vector<int>> &t, vector<int> &color, int idx)
{
int cnt = 0;
for (auto &child: t[idx])
{
cnt += DFS(t, color, child);
}
if (cnt % 2 == 0)
color[idx] = 1;
return cnt + color[idx];
}
int main()
{
int n, a, b;
while (cin >> n)
{
vector<vector<int>> t(n + 1);
vector<int> color(n + 1, 0);
for (int i = 0; i < n - 1; ++i)
{
cin >> a >> b;
if (a > b)
swap(a, b);
t[a].push_back(b);
}
DFS(t, color, 1);
for (int i = 1; i <= n; ++i)
{
cout << (color[i] ? "B" : "R");
}
cout << endl;
}
return 0;
} 