tree

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

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
 
Sample Output
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.

 题意:n个节点的树,有的边权是1有的是0,是0表示距离近,而且比如1和2是0,1和3也是0,那么2和3之间也是0,转化为并查集,顺便路径压缩;
AC代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4. #include <cstring>
  5. #include <algorithm>
  6. using namespace std;
  7. const int N=1e6+;
  8. int flag[N],p[N];
  9. int findset(int x)
  10. {
  11. if(x!=p[x])return p[x]=findset(p[x]);
  12. return x;
  13. }
  14. int main()
  15. {
  16. int T,n,u,v,w;
  17. scanf("%d",&T);
  18. while(T--)
  19. {
  20. scanf("%d",&n);
  21. for(int i=;i<=n;i++)
  22. {
  23. flag[i]=;
  24. p[i]=i;
  25. }
  26. for(int i=;i<n-;i++)
  27. {
  28. scanf("%d%d%d",&u,&v,&w);
  29. if(w==)
  30. {
  31. int a=findset(u);
  32. int b=findset(v);
  33. p[a]=b;
  34. }
  35. }
  36. for(int i=;i<=n;i++)
  37. {
  38. flag[findset(i)]++;
  39. }
  40. int ans=flag[findset()];
  41. for(int i=;i<=n;i++)
  42. {
  43. ans=(ans^flag[findset(i)]);
  44. }
  45. printf("%d\n",ans);
  46. }
  47. return;
  48. }

hdu5606 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. tree(并查集)

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

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

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

随机推荐

  1. [转]python 书籍推荐

    原地址: http://python.jobbole.com/85620/ https://github.com/jobbole/awesome-python-books http://blog.cs ...

  2. Xampp 环境问题集合

    1.不小心把虚拟机的环境删了,需要重新安装xmapp 安装很简单,但是重启:/opt/lampp/lampp restart 发现 XAMPP:"Another web server dae ...

  3. 谷歌浏览器console.log()失效,打印不出来内容

    这个问题困扰好几天了,网上说的都说的是下图: 勾选这三个就好了,但是我的本来就是勾选上的,还是不行. 后来发现这个: 把这个去掉就可以了,如下图: 原来是因为之前调试js的时候,使用了这个过滤,导致对 ...

  4. thinkPHP5.0的学习研究【基础】

    2017年6月19日13:25:56 基础:1.ThinkPHP5的环境要求如下: PHP >= 5.4.0        PDO PHP Extension        MBstring P ...

  5. H - Coins

    H - Coins Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descripti ...

  6. WCF基础之会话、实例和并发

    这篇笔记是一些概念性的东西. 会话,借用百科上的描述就是一个客户与服务器之间的不中断的请求响应序列.wcf的会话模式是通过服务契约的SessionModel进行设置的,其值为枚举,分别为:Allowe ...

  7. Latent Activity Trajectory (LAT)

    https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/funcZone_TKDE_Zheng.pdf Specific ...

  8. Django 动态导入配置文件的类

    导入配置文件的类 #如何用字符串的形式动态导入模块 from importlib import import_module path=''api.cors.CORSMiddleware' #CORSM ...

  9. 程序运行之ELF文件的段

    我们将之前的代码增加下变量来具体看下 在代码中增加了全局变量以及静态变量,还有一个简单的函数. #include <stdio.h> int global_var=1; int globa ...

  10. 解决ajax get方式提交中文参数乱码问题

    最近在工作中遇到,使用ajax get方式提交中文参数的时候出现乱码,通过上网搜索,总结出比较简单的两种解决方案: 第一种,由于tomcat默认的字符集是ISO-8859-1,修改Tomcat中的se ...