tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 986    Accepted Submission(s): 452

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5606

Problem Description
There is a tree(the tree is a connected graph which contains 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.
 
Input
the first line contains a number T,means T test cases.

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]

 
Output
for each test case,you need to print the answer to each point.

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.

 
Sample Input
1
3
1 2 0
2 3 1
 
 
题解: 把每条边权是1的边断开,发现每个离他最近的点个数就是他所在的联通快的大小,开一个并查集,每次读到编圈是0的边就合并,最后结果就是每个点的父亲的孩子个数加1
太久没有打并查集了水一水
 #include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define N 100006
int n;
int fa[N];
int num[N];
void init(){
for(int i = ; i < N; i++) fa[i] = i;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x, int y){
int root1 = find(x);
int root2 = find(y);
if(root1==root2) return;
fa[root1] = root2;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
for(int i = ; i < n-; i++)
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
if(c==){
merge(a,b);
}
}
memset(num,,sizeof(num));
for(int i = ; i <= n; i++)
{
int r = find(i);
num[r]++;
}
int ans = ;
for(int i = ; i <= n; i++)
{
ans = (ans^num[find(i)]);
}
printf("%d\n",ans);
}
return ;
}

tree(并查集)的更多相关文章

  1. 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 ...

  2. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  3. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  4. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  5. [Swust OJ 856]--Huge Tree(并查集)

    题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...

  6. 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 ...

  7. Is It A Tree?(并查集)(dfs也可以解决)

    Is It A Tree? Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submi ...

  8. 树上统计treecnt(dsu on tree 并查集 正难则反)

    题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...

  9. 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 ...

随机推荐

  1. 【NOIP模拟】从我背后出现

    Description 给定n个点m条边的无向连通图,对于每条边求出强制选这条边后的最⼩⽣成树⼤⼩. \(n\leq 10^5,m\leq 2*10^5\) Input Format 第 1 行包含两 ...

  2. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  3. C:函数:功能:实现字符数组中所有字母的倒序存放并输出

    前两天小测碰到一道题,建立一个函数,功能:实现字符数组中所有字母的倒序存放并输出,一开始觉得简单跟数字数组差不多,运行一下发现很多格式错误,这些是不必要的错误,现在就来说下,先说一下代码思路:定义一个 ...

  4. n年前,我没钱但年轻,我怕n年后我老时,还是一无所成——2017我的收获和反思

    记得当年我刚从学校里出来时,应该和现在的95后差不多,当时还是很惶恐的,怕找不到工作,怕无法挣到足够的钱买房子支撑家庭,(当然还有其它的担心点),却唯独没意识到自己拥有着最宝贵的财富:年轻. 年轻意味 ...

  5. ReactNative实现图集功能

    需求描述: 图片缩放.拖动.长按保存等基础图片查看的功能: 展示每张图片文本描述: 实现效果,如图: 实现步骤 使用第三方插件:react-native-image-zoom-viewer 插件Git ...

  6. jstree 学习

    最近的项目用到了jstree,因为对官方文档理解不充分,所以很多功能都是在网站上搜索再进行使用的.(我只是大自然的搬运工) 对每一级的节点,右键后出现不同的结果. 在jstree中右键是由 conte ...

  7. 编译c语言程序扩展ruby

    环境: windows 10 64bit ruby 2.2.4p230 (2015-12-16 revision 53155) [i386-mingw32] gcc version 4.8.1 (GC ...

  8. bzoj 3626: [LNOI2014]LCA

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1.设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先.有q ...

  9. Linux内置命令

    主要Shell内置命令 Shell有很多内置在其源代码中的命令.这些命令是内置的,所以Shell不必到磁盘上搜索它们,执行速度因此加快.不同的Shell内置命令有所不同. A.2.1 bash内置命令 ...

  10. [经验分享]Linux网络连接-VMware+CentOS 7

    VMware虚拟机中安装CentOS,进行网络连接,分为两步,内网连接,与外网连接. 前提: 当你正确安装VMware后,网络适配器会增加2个新的网卡:(可在设备管理器->网络适配器中查看) 第 ...