You are given an undirected unrooted tree, i.e. a connected undirected graph without cycles.
You must assign a nonzero integer weight to each vertex so that the following is satisfied: if any vertex of the tree is removed, then each of the remaining connected components has the same sum of weights in its vertices.
Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer nn (3≤n≤1053≤n≤105) — the number of vertices of the tree.
The next n−1n−1 lines of each case contain each two integers u,vu,v (1≤u,v≤n1≤u,v≤n) denoting that there is an edge between vertices uu and vv. It is guaranteed that the given edges form a tree.
The sum of nn for all test cases is at most 105105.
Output
For each test case, you must output one line with nn space separated integers a1,a2,…,ana1,a2,…,an, where aiai is the weight assigned to vertex ii. The weights must satisfy −105≤ai≤105−105≤ai≤105 and ai≠0ai≠0.
It can be shown that there always exists a solution satisfying these constraints. If there are multiple possible solutions, output any of them.
Example
input
Copy
2 5 1 2 1 3 3 4 3 5 3 1 2 1 3
output
Copy
-3 5 1 2 2 1 1 1
Note
In the first case, when removing vertex 11 all remaining connected components have sum 55 and when removing vertex 33 all remaining connected components have sum 22. When removing other vertices, there is only one remaining connected component so all remaining connected components have the same sum.
思路 可以把一条边看作两条有向边 一条边权为正 一条边权为负 举个例子
5 1 2 1 3 3 4 3 5 中可以让1到2 1到3的边权为-1,2到1为1,3到1 3到4 3到5 为1,4到3 为-1
5到3 为 -1 这样每个点的点权就是与它相连的边权之和 1点权为-2 2点权为1 3点权为3 4点权为-1
5点权为-1 现在整个图的权值和是零 当把一个节点去掉之后每个联通块的权值就是去掉的节点的边权值的负数
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
int n,t1,t2;
cin >> n;
vector<int> v[n+1],out(n+1),w(n+1);
for(int i = 1;i < n; i++){
cin >> t1 >> t2;
v[t1].push_back(t2);
v[t2].push_back(t1);
out[t1]++;
out[t2]++;
}
function<void(int,int,int)> dfs = [&] (int now,int fath,int k){
w[now] = out[now]*(-k);
for(int x : v[now]){
if(x == fath) continue;
dfs(x,now,-k);
}
};
dfs(1,0,1);
for(int i = 1;i <= n; i++){
cout << w[i] << " ";
}
cout << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T;
cin >> T;
while(T--) {
solve();
}
return 0;
}