tree

 Accepts: 143
 Submissions: 807
 Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
有一个树(nn个点, n-1n−1条边的联通图),点标号从11~nn,树的边权是00或11.求离每个点最近的点个数(包括自己).
输入描述
第一行一个数字TT,表示TT组数据.
对于每组数据,第一行是一个nn,表示点个数,接下来n-1n−1,每行三个整数u,v,wu,v,w,表示一条边连接的两个点和边权.
T = 50,1 \leq n \leq 100000, 1 \leq u,v \leq n,0 \leq w \leq 1T=50,1≤n≤100000,1≤u,v≤n,0≤w≤1.
输出描述
对于每组数据,输出答案.
考虑到输出规模过大,设ans_ians​i​​表示第ii个点的答案.你只需输出ans_1 \ xor \ ans_2 \ xor \ ans_3.. \ xor \ ans_nans​1​​ xor ans​2​​ xor ans​3​​.. xor ans​n​​即可.
输入样例
1
3
1 2 0
2 3 1
输出样例
1
Hint
ans_1 = 2ans​1​​=2
ans_2 = 2ans​2​​=2
ans_3 = 1ans​3​​=1
2 \ xor \ 2 \ xor \ 1=12 xor 2 xor 1=1, 因此输出11.

马丹!!我想骂人!刚开始做这道题就开始开了一个数组求每一个为0的两个点的数量,最后猛然发现如果三个数都是0答案就不对,然后有想到了深搜,用vector,存与之相连为0的点,写到一半,发现又不对!马丹,猛然想起就是并查集啊,把为0的点放在一起,就是就每个集合中数的个数嘛!敲了不到10分钟敲完,卡在8:45提交了,WA了,擦!猛然发现一个细节给忽略了,果断改了,8:46了。。。呵呵呵呵。。刚才提交a了,擦!太弱了,伤心的是又掉rating了....

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int MAX = + ;
int num[MAX],father[MAX];
int find_father(int x)
{
int r = x;
while(r != father[r])
r = father[r];
int i = x,j;
while(r != father[i])
{
j = father[i];
father[i] = r;
//num[r] += num[father[i]] 擦!就是这!没必要啊,以为此时num[r]中肯定含有num[father[i]],因为这只是跟新直系跟节点,数量并不需修改!!!
i = j;
}
return r;
}
void Union(int x, int y)
{
int fx = find_father(x);
int fy = find_father(y);
if(fx != fy)
{
father[fx] = fy;
num[fy] += num[fx];
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int u,v,w,n;
scanf("%d", &n);
for(int i = ; i <= n; i++)
{
father[i] = i;
num[i] = ;
}
for(int i = ; i < n; i++)
{
scanf("%d%d%d", &u,&v,&w);
if(w == )
{
Union(u,v);
}
}
int temp = find_father();
int ans = num[temp]; for(int i = ; i <= n; i++)
{
int fx = find_father(i);
ans = ans ^ num[fx];
}
printf("%d\n",ans);
} return ;
}

BC68(HD5606) 并查集+求集合元素的更多相关文章

  1. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

  2. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  3. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  4. 并查集(不相交集合)详解与java实现

    目录 认识并查集 并查集解析 基本思想 如何查看a,b是否在一个集合? a,b合并,究竟是a的祖先合并在b的祖先上,还是b的祖先合并在a上? 其他路径压缩? 代码实现 结语 @(文章目录) 认识并查集 ...

  5. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. More is better——并查集求最大集合(王道)

    Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...

  7. 杭电 1856 More is better (并查集求最大集合)

    Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...

  8. POJ 3723 Conscription (Kruskal并查集求最小生成树)

    Conscription Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14661   Accepted: 5102 Des ...

  9. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

随机推荐

  1. The Geometry has no Z values 解决办法

    from:http://dufan20086.blog.163.com/blog/static/6616452320145269343675/ 我们在创建要素时,简单的IFeatureClass.Cr ...

  2. 09Mybatis_入门程序——删除用户以及更新用户

    删除用户: 还是前面的的案例,别的都不改,就修改两处地方.1.user.xml文件以及2.Mybatis_first.java文件 user.xml文件代码修改如下: <?xml version ...

  3. [Python] 利用commands模块执行Linux shell命令

    http://blog.csdn.net/dbanote/article/details/9414133 http://zhou123.blog.51cto.com/4355617/1312791

  4. 如何解决机器重启后MySql服务跑不起来了1067,附解决过程

    今个一位同事的mysql跑不起来了,之前还好好的,问我怎么恢复,我没在她出问题的机器上弄,在自己机器上试的,最终在CTO的指导下解决了,现记录于此. 1,首先把她的mysql安装文件夹.数据结构文件. ...

  5. Python自动化测试 (二) ConfigParser模块读写配置文件

    ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单. 直接上代码,不解释,不多说. 配置文件的格式是: []包含的叫section,    section 下有op ...

  6. 基于jsp的文件上传和下载

    参考: 一.JavaWeb学习总结(五十)--文件上传和下载 此文极好,不过有几点要注意: 1.直接按照作者的代码极有可能listfile.jsp文件中 <%@taglib prefix=&qu ...

  7. 拿什么拯救你,我的代码--c#编码规范实战篇 (转)

    http://www.cnblogs.com/lazio10000/p/5413439.html 此文为译文,原文地址请点击. 本文通过重构一个垃圾代码,阐述了如何写出优秀的代码.开发人员及代码审核人 ...

  8. C++ VS2010 声明没有存储类或类型说明符

    函数外只能定义全局变量或者对象,而不能执行语句及调用函数.

  9. Win10开发究竟能实现哪些牛逼的功能

    经Win10开发者群(53078485)大咖Aran童鞋授权,这次先Show一下他通过vs2015做的跨端APP一些高级功能的GIF图,大家可以回帖想要哪个功能的DEMO,我和Aran说一下,会把DE ...

  10. Sublime Text 3使用参考手册

    什么是Sublime Text? Sublime Text 是一个代码编辑器(Sublime Text 2是收费软件,但可以无限期试用),也是HTML和散文先进的文本编辑器.Sublime Text是 ...