一棵树有N个节点,编号为1至N。树的第i条边连接节点ui和节点vi,长度为wi。你应将这棵树的所有节点染上黑色或白色(所有节点可以是同一种颜色),染色后的树应满足:
对于任意两个相同颜色的节点,它们之间的距离是偶数。
输出任意一组合法的解,第i行输出i号节点的颜色。输出0表示该节点是白色,输出1表示该节点为黑色。可以证明该问题至少有一组解。
保证所有输入都是整数。
1≤N≤105
1≤ui<vi≤N
1≤wi≤109
输入输出样例
输入 #1复制
3 1 2 2 2 3 1输出 #1复制
0 0 1输入 #2复制
5 2 5 2 2 3 10 1 3 8 3 4 2输出 #2复制
1 0 1 0 1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
using namespace std;
#define endl '\n'
#define int long long
typedef pair<int,int>PII;
constexpr int N=1e5+7;
vector<PII>adj[N];
bool k[N];
void dfs(int x,int fa){
for(int i=0;i<adj[x].size();i++){
auto t=adj[x][i];
if(t.first==fa) continue;
if(t.second) k[t.first]=!k[x];
else k[t.first]=k[x];
dfs(t.first,x);
}
}
signed main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n-1;i++){
int u,v,w;
cin>>u>>v>>w;
adj[u].push_back({v,w%2});
adj[v].push_back({u,w%2});
}
dfs(1,0);
for(int i=1;i<=n;i++){
if(k[i]) {
cout <<1<< endl;
}
else{
cout<<0<<endl;
}
}
return 0;
}