BC68(HD5606) 并查集+求集合元素
tree
有一个树(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_iansi表示第ii个点的答案.你只需输出ans_1 \ xor \ ans_2 \ xor \ ans_3.. \ xor \ ans_nans1 xor ans2 xor ans3.. xor ansn即可.
1
3
1 2 0
2 3 1
1
ans_1 = 2ans1=2
ans_2 = 2ans2=2
ans_3 = 1ans3=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) 并查集+求集合元素的更多相关文章
- 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 ...
- 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 ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
- 并查集(不相交集合)详解与java实现
目录 认识并查集 并查集解析 基本思想 如何查看a,b是否在一个集合? a,b合并,究竟是a的祖先合并在b的祖先上,还是b的祖先合并在a上? 其他路径压缩? 代码实现 结语 @(文章目录) 认识并查集 ...
- 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 ...
- More is better——并查集求最大集合(王道)
Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...
- 杭电 1856 More is better (并查集求最大集合)
Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
- 杭电 1213 How Many Tables (并查集求团体数)
Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...
随机推荐
- C# 使用正则表达式去掉字符串中的数字,或者去掉字符串中的非数字
/// 去掉字符串中的数字 public static string RemoveNumber(string key) { ...
- ArcGis 统计方法
from:http://blog.sina.com.cn/s/blog_4177d50b0100fjbg.html 概述 一般常用的统计功能例如:唯一字段统计.数据行数统计.数据值求和统计等. 1.基 ...
- HTML---总结
(1) margin负值能让没有宽度的盒子变大. 对于有宽度的盒子,没有浮动,只有margin-top 和margin-left 有效:如果浮动,margin-浮动方向 有效,-top有效,margi ...
- AndroidStudio .gitinore编写
# Built application files *.apk *.ap_ # files for the dex VM *.dex # Java class files *.class */R.ja ...
- 在创维E900-S悦Me盒子上安装第三方软件
0x00 不甘寂寞 创维E900-S这款悦Me盒子功能还算可以,但不能接受它禁止安装第三方软件这一点.网上搜了半天,可能是比较新的机型没人关注,找不到任何方法,只好自己动手试试. 0x01 Fiddl ...
- Java连接Elasticsearch集群
package cn.test; import java.net.InetAddress; import java.net.UnknownHostException; import org.elast ...
- DIV相关的操作总结
由于有时候需要做网站项目,遇到CSS的问题总是需要百度或者google一下,比较麻烦,索性今天就来总结一下,这里就拿div开刀先. DIV在HTML前端页面布局中,非常重要,我们经常遇到的问题是:DI ...
- matlab取消和添加注释以及一些快捷键
1 matlab中关于注释: 多行注释: 选中要注释的若干语句,工具栏菜单Text->Comment,或者鼠标右击选"Comment",或者快捷键Ctrl+R 取消注释: 选 ...
- 九度oj-1003-Java
题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计算A+B的结果,并以正常形式输出. 输入: 输入包含多组数据数据,每组数据占一行,由两 ...
- 采用指数退避算法实现ajax请求的重发,全部完成时触发回调函数
目录: 0.Chrome扩展开发(Gmail附件管理助手)系列之〇——概述 1.Chrome扩展开发之一——Chrome扩展的文件结构 2.Chrome扩展开发之二——Chrome扩展中脚本的运行机制 ...