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

Problem Description
There is a tree(the tree is a connected graph which contains n points and n−1 edges),the points are labeled from 1 to n,which edge has a weight from 0 to 1,for every point i∈[1,n],you should find the number of the points which are closest to it,the clostest points can contain i itself.
 
Input
the first line contains a number T,means T test cases.

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]

 
Output
for each test case,you need to print the answer to each point.

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.

 
Sample Input
1
3
1 2 0
2 3 1
 
 
题解: 把每条边权是1的边断开,发现每个离他最近的点个数就是他所在的联通快的大小,开一个并查集,每次读到编圈是0的边就合并,最后结果就是每个点的父亲的孩子个数加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(并查集)的更多相关文章

  1. 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 ...

  2. Is It A Tree?(并查集)

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  3. CF109 C. Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  4. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  5. [Swust OJ 856]--Huge Tree(并查集)

    题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...

  6. 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 ...

  7. Is It A Tree?(并查集)(dfs也可以解决)

    Is It A Tree? Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submi ...

  8. 树上统计treecnt(dsu on tree 并查集 正难则反)

    题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...

  9. 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 ...

随机推荐

  1. TCP/IP协议族各层的作用

    从协议分层模型方面来讲,TCP/IP由四个层次组成:数据链路层.网络层.传输层.应用层一.数据链路层 数据链路层是负责接收IP数据报并通过网络发送之,或者从网络上接收物理帧,抽出IP数据报,交给IP层 ...

  2. 快速自检电脑是否被黑客入侵过(Linux版)

    之前写了一篇快速自检电脑是否被黑客入侵过(Windows版), 这次就来写写Linux版本的. 前言 严谨地说, Linux只是一个内核, GNU Linux才算完整的操作系统, 但在本文里还是用通俗 ...

  3. CSS3关于过渡效果的问题

    首先trasition:transform只是单单表示后面只要有含有的tranform的所有属性可以参与动画,而trasition:all表示后面所有动画属性都可以参动画,当父容器有relative时 ...

  4. scala写算法-快排

    快排算法很经典,今天用scala的函数式思维来整理一下并实现: def qsort(list: List[Int]):List[Int]=list match { case Nil=>Nil c ...

  5. MySQL数据库规约.

    一.建表规约 1.表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint(1 表示是, 0 表示否) . 2.表名.字段名必须使用小写字母或数字, 禁止 ...

  6. css div 细边框

    .item{ max-width:48%; float:left; padding:2px; border-top:1px solid #000; border-left:1px solid #000 ...

  7. springCloud系列教程01:Eureka 注册中心集群搭建

    springCloud系列教程包含如下内容: springCloud系列教程01:Eureka 注册中心集群搭建 springCloud系列教程02:ConfigServer 配置中心server搭建 ...

  8. C# DataGridView 列的显示顺序

    this.dataGridView1.Columns["列名"].DisplayIndex=Convert.ToInt32("你要放置的位置")

  9. 使用HTML语言和CSS开发商业站点

    第一章 预习笔记 单词: head:前进 title:标题 meta:超过 body:身体.尸体 W3C:全球最大的中文Web技术教程 strong:强烈的 target:目标,对象 href:水平参 ...

  10. XML文件解析数据结构

    最近在解析Android安装包内经过编译的二进制XML文件时想在内存中建立起其对应的树结构. 想了一早晨,思路如下图. 多叉树中的每个节点除了有子节点和兄弟节点以外还有一个指针指向父节点,然后根据状态 ...