tree(并查集)
tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 986 Accepted Submission(s): 452
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5606
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]
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.
3
1 2 0
2 3 1
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define N 100006
int n;
int fa[N];
int num[N];
void init(){
for(int i = ; i < N; i++) fa[i] = i;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x, int y){
int root1 = find(x);
int root2 = find(y);
if(root1==root2) return;
fa[root1] = root2;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
for(int i = ; i < n-; i++)
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
if(c==){
merge(a,b);
}
}
memset(num,,sizeof(num));
for(int i = ; i <= n; i++)
{
int r = find(i);
num[r]++;
}
int ans = ;
for(int i = ; i <= n; i++)
{
ans = (ans^num[find(i)]);
}
printf("%d\n",ans);
}
return ;
}
tree(并查集)的更多相关文章
- 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 ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- 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 ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- hdu 1325 Is It A Tree? 并查集
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
随机推荐
- Python3.5:爬取网站上电影数据
首先我们导入几个pyhton3的库: from urllib import requestimport urllibfrom html.parser import HTMLParser 在Python ...
- HBase跨地区机房的压测小程序——从开发到打包部署(图文版)
今天做了一个跨地区机房的压测小程序,主要的思路就是基于事先准备好的rowkey文件,利用多线程模拟并发的rowkey查询,可以实现并发数的自由控制.主要是整个流程下来,遇到了点打包的坑,所以特意记录下 ...
- C# Excel数据导入到数据库
http://www.jb51.net/article/44743.htm 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: 代码: 复制代码 代码如下: using System ...
- 笔记-NSArray 逆序reverseObjectEnumerator 及 NSEnumerator 遍历
//1.原始数组 NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"2",@&qu ...
- spring的基本使用
Spring的基本使用ioc,今天主要给大家说明了解决强耦合的联系,并且,注入的基本使用 Java里面的强耦合并且讲了spring是如何解决强耦合的第一种方式使用工厂模式,用的是反射,第二种方式是sp ...
- js 错误Error对象详解
一.概念 error,指程序中的非正常运行状态,在其他编程语言中称为"异常"或"错误".解释器会为每个错误情形创建并抛出一个Error对象,其中包含错误的描述信 ...
- ES6 Generators并发
ES6 Generators系列: ES6 Generators基本概念 深入研究ES6 Generators ES6 Generators的异步应用 ES6 Generators并发 如果你已经读过 ...
- [Spark性能调优] 源码补充 : Spark 2.1.X 中 Unified 和 Static MemoryManager
本课主题 Static MemoryManager 的源码鉴赏 Unified MemoryManager 的源码鉴赏 引言 从源码的角度了解 Spark 内存管理是怎么设计的,从而知道应该配置那个参 ...
- VI命令汇总
ia/Ao/Or + ?替换 0:文件当前行的开头$:文件当前行的末尾G:文件的最后一行开头:n :文件中第n行的开头 dd:删除一行3dd:删除3行yy:复制一行3yy:复制3行p:粘贴u:undo ...
- 深入理解ES6之—符号与符号属性
在js已有的基本类型(字符串,数值,布尔型,null和undefined)之外,es6引入了一种新的基本类型:==符号(Symbol)==.符号起初被设计用于创建对象私有成员. 符号没有字面量形式,你 ...