hdoj--5606--tree(并查集)
tree
n
points and n−1
edges),the points are labeled from 1 to n,which
edge has a weight from 0 to 1,for every point i∈[1,n],you
should find the number of the points which are closest to it,the clostest points can contain
i
itself.
for each test case,the first line is a nubmer n,means
the number of the points,next n-1 lines,each line contains three numbers
u,v,w,which
shows an edge and its weight.
T≤50,n≤105,u,v∈[1,n],w∈[0,1]
in consideration of the large output,imagine ansi
is the answer to point i,you
only need to output,ans1 xor ans2 xor ans3.. ansn.
1
3
1 2 0
2 3 1
1 in the sample. $ans_1=2$ $ans_2=2$ $ans_3=1$ $2~xor~2~xor~1=1$,so you need to output 1.
#include<stdio.h>
#include<string.h>
int rank[100100];
int pre[100100];
void init()
{
for(int i=0;i<100100;i++)
pre[i]=i;
}
//int find(int x)
//{
// return pre[x]==x?x:pre[x]=find(pre[x]);
//}
int find(int p)
{
int child=p;
while(p!=pre[p])
p=pre[p];
while(child!=p)
{
int t=pre[child];
pre[child]=p;
child=t;
}
return p;
}
void join(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fy!=fx)
pre[fx]=fy;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
memset(rank,0,sizeof(rank));
init();
int a,b,c;
scanf("%d",&n);
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c==0)
join(a,b);
}
for(int i=1;i<=n;i++)
{
if(pre[i]==i)
rank[i]++;
else
rank[find(i)]++;
}
int ans=0;
for(int i=1;i<=n;i++)
ans^=(rank[find(i)]);
printf("%d\n",ans);
}
return 0;
}
hdoj--5606--tree(并查集)的更多相关文章
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- Hdu.1325.Is It A Tree?(并查集)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)
D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- hdu 1325 Is It A Tree? 并查集
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- HDU 5493 Queue 树状数组+二分
Queue Problem Description N people numbered from 1 to N are waiting in a bank for service. They all ...
- 新东方雅思词汇---6.1、oppose
新东方雅思词汇---6.1.oppose 一.总结 一句话总结:op(相反)+pos(放) 英 [ə'pəʊz] 美 [ə'poz] vt. 反对:对抗,抗争 vi. 反对 [ 过去式 oppos ...
- 【POJ 2182】Lost Cows
[题目链接] http://poj.org/problem?id=2182 [算法] 树状数组 + 二分 [代码] #include <algorithm> #include <bi ...
- BigInteger类型转换成Long类型或int类型问题
BigInteger bi = new BigInteger("123"); int i = bi.intValue(); lo ...
- 修改mysql连接的密码
mysql8.0修改密码: ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '你的password'; msyql开启实现 ...
- iis 部署
配置错误1: 由于权限不足而无法读取配置文件 建立一个新用户,分配所有权限 http://blog.csdn.net/jaychouliyu/article/details/7237143 配置错误2 ...
- UWP tips (与wp8.1的不同)
一.异步调用之后,要更新UI时,代码如下 await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&g ...
- ASP内建对象
Active Server Pages 提供内建对象,这些对象使用户更容易收集通过浏览器请求发送的信息.响应浏览器以及存储用户信息(如用户首选项).本文简要说明每一个对象.有关每个对象的详细信息,请参 ...
- Vue组件之作用域插槽
写作用域插槽之前,先介绍一下Vue中的slot内容分发: 如果<child-component></child-component>标签之间没有插入那两个p标签的话,页面会显示 ...
- ansible 主机清单 /etc/ansible/hosts
主机清单 [webservers] ansible01 ansible02 ansible03 ansible04 [root@ftp:/root] > ansible webservers - ...