Equal Tree Sums 二分图

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;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB中的isequal函数用于比较两个对象是否相等。该函数不考虑数据类型,只关注元素的值是否相等。当比较数值时,isequal函数不考虑数据类型,例如逻辑真和1、字母A和65,它们都视为相等。然而,不同的NaNs不相等,因此包含NaN的数组都不相等。如果想将NaN视为相等,可以使用isequalwithequalnans函数。对于元胞数组和结构体,isequal函数会循环比较它们的内容。如果所有元素的值都相等,则isequal函数返回逻辑1(真)。 在转化为C语言时,可以通过编写对应的代码来模拟isequal函数的运算。根据需要的功能,可以使用比较运算符(例如==)来比较数值,使用循环或递归来比较元胞数组和结构体的内容。同时,需要注意C语言和MATLAB的语法和数据类型的差异,确保代码的正确性和可移植性。 请注意,MATLAB中的isequal函数有一些特殊情况的处理,如对NaN的处理,以及对逻辑真和数值1的等价视为相等。在C语言中需要根据具体需求进行相应的处理。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [MATLAB中isequal函数转化为C语言,有项目算法使用matlab中isequal函数进行运算,这里需要将转化为C语言](https://download.csdn.net/download/li171049/88279963)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [MATLAB中的isequal函数的用法](https://blog.csdn.net/xuxinrk/article/details/80367911)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值