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 ...
随机推荐
- 快捷键forMac
1.手动补全快捷键 设置completion+basic或者completion+smartType 2.快速导入指定API的包 command+1
- git config配置文件
设置 git status的颜色. git config --global color.status auto 一.Git已经在你的系统中了,你会做一些事情来客户化你的Git环境.你只需要做这些设置一 ...
- 【WPF】Winform调用WPF窗体注意事项
1.需要添加一些引用 2.调用处使用如下方法进行调用 Window win= new Window(); ElementHost.EnableModelessKeyboardInterop(win) ...
- 【转】【C++】ShellExecute, WinExec, CreateProcess 三者的区别
ShellExecute ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制. 有几个API函数都可以实现这些功能 ...
- C# 将透明图片的非透明区域转换成Region
以下代码实现将一张带透明度的png图片的非透明部分转换成Region输出 /// <summary> /// 根据图片得到一个图片非透明部分的区域 /// </summary> ...
- Kinect for Windows SDK开发初体验(一)环境配置
1.开发环境需求 (1).硬件需求 a.需要拥有双核,2.66GHz以上的CPU. b.显卡支持Microsoft DirectX 9.0c; c.2GB的内存 d.Kinect for Window ...
- Integrating JAD decompiler into JDeveloper(转)
原文地址:Integrating JAD decompiler into JDeveloper In JDeveloper, when debugging or otherwise navigatin ...
- libevent+bufferevent总结
libevent+bufferevent总结 1 学习参考网址 libevent学习网址:http://blog.csdn.net/feitianxuxue/article/details/93725 ...
- 无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性。
新建一个MVC4的项目,引用DAL后,将DAL的连接字符串考入: <connectionStrings> <add name="brnmallEntities&qu ...
- 使用Windows Live Writer发布日志
前言 Windows Live Writer是非常不错的一个日志发布工具,支持本地写文章,然后通过点击一个按钮就发布到网站上,如果借助插件,还可以同时发布到多个博客网站,功能非常强大,很多博友认识她之 ...