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. 一个简单的scrapy爬虫抓取豆瓣刘亦菲的图片地址

    一.第一步是创建一个scrapy项目 sh-3.2# scrapy startproject liuyifeiImage sh-3.2# chmod -R 777 liuyifeiImage/ 二.分 ...

  2. PBR综合小实验视频-狮子XL

    这个是上学时候录的一个策略路由小实验

  3. C语言 原码--反码--补码

    //原码,反码,补码 #include<stdio.h> #include<stdlib.h> //数值的表示方法——原码.反码和补码 //原码:最高位为符号位,其余各位为数值 ...

  4. WorldWind源码剖析系列:WorldWind实时确定、更新、初始化和渲染地形和纹理数据

    WorldWind实时确定.更新.初始化和渲染地形和纹理数据 当用户点击WorldWind中的地球时,首先响应的是WorldWindow.OnPaint()函数,后续程序的调用流程如下图所示. 零散知 ...

  5. 修改TrustedInstaller权限文件(无法删除文件)

    1.    右击需要修改的文件-属性 2.    切换到"安全"选项卡,点击"高级"按钮. 3.    切换到"所有者"选项卡 一般情况下默 ...

  6. Java系列:《Java核心技术 卷一》学习笔记,chapter11 记录日志

    11.5 日志记录 可以通过Loger.getGlobal().info(xxxx);的方式来记录log. 11.5.2 高级日志 1)通过一个包名来 创建一个新的日志记录器. private sta ...

  7. 安装Ubuntu 16.04后要做的事

    Ubuntu 16.04发布了,带来了很多新特性,同样也依然带着很多不习惯的东西,所以装完系统后还要进行一系列的优化. 1.删除libreoffice libreoffice虽然是开源的,但是Java ...

  8. Linux内核分析——第七周学习笔记20135308

    第七周 可执行程序的装载 一.预处理.编译.链接和目标文件的格式 1.可执行程序是怎么来的 C代码—>预处理—>汇编代码—>目标代码—>可执行文件 .asm汇编代码 .o目标码 ...

  9. 小甲鱼第51讲:《__name__="__main__"、搜索路径和包》课后练习题

    测试题: 0. __name__属性指的是在调用该模块的时候调用的函数名称,方便在模块的被调用的时候,模块内部被调用的函数不会被运行. 1. 当模块作为主程序运行的时候,__name__属性的值是“_ ...

  10. 学习笔记——Maven pom.xml配置详解

    POM的全称是“ProjectObjectModel(项目对象模型)”. pom.xml详解 声明规范 <projectxmlns="http://maven.apache.org/P ...