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. ABP-Module

    [TOC] 什么是Module? Module就是模块化的设计思想.开发人员可以将自定义的功能以模块的形式集成到项目中.具体的功能也可以设计成一个单独的模块 AbpModule AbpModule是所 ...

  2. FastJson--阿里开源的速度最快的Json和对象转换工具

    示例 import java.util.ArrayList; import java.util.List; import java.util.HashMap; import java.util.Map ...

  3. 小白的 MySQL 笔记(一)

    来自 stackoverflow 的内容居多. 1- MySQL VARCHAR size? 2- 数据库设计范式 3- What is InnoDB and MyISAM in MySQL ? 4- ...

  4. lodash源码分析之Hash缓存

    在那小小的梦的暖阁,我为你收藏起整个季节的烟雨. --洛夫<灵河> 本文为读 lodash 源码的第四篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash gitbo ...

  5. PHP (超文本预处理器)

    PHP(外文名:PHP: Hypertext Preprocessor,中文名:"超为本预处理器")是一种通用开源脚本语言.语法吸收了C语言.java和Rerl的特点,利于学习,使 ...

  6. php-redis 操作类 封装

    <?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...

  7. 服务器 Python服务停服、起服脚本

    近日,在阿里云服务器上部署了一个Python,Web框架为Tornado,服务器为Ubuntu 16.04. 服务的启动也十分的简单: python services.py 我是利用Xshell工具连 ...

  8. php array_walk

    PHP array_walk() 函数 对数组中的每个元素应用用户自定义函数: <?php function myfunction($value,$key) { echo "The k ...

  9. 阿里云EMR集群初始化后的开发准备工作

              前言:EMR的集群使用越来越普遍,但是每一次的集群释放到集群的重新创建,期间总有一些反复的工作需要查询与配置.为方便后续工作查阅,现在对集群初始化后的工作进行大概的梳理如下.   ...

  10. TPYBoard v102 驱动28BYJ-48步进电机

    TPYBoard v102 驱动28BYJ-48步进电机 实验目的 了解步进电机的工作原理 学习步进电机的驱动方法 实验器材 TPYBoard v102 1块 微型步进电机(28BYJ-48) 1个 ...