tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 823    Accepted Submission(s): 394

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
 
Sample Output
1

in the sample.

$ans_1=2$

$ans_2=2$

$ans_3=1$

$2~xor~2~xor~1=1$,so you need to output 1.

 题意:n个节点的树,有的边权是1有的是0,是0表示距离近,而且比如1和2是0,1和3也是0,那么2和3之间也是0,转化为并查集,顺便路径压缩;
AC代码:
#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
const
int N=1e6+;
int
flag[N],p[N];
int
findset(int x)
{

if
(x!=p[x])return p[x]=findset(p[x]);
return
x;
}

int
main()
{

int
T,n,u,v,w;
scanf("%d",&T);
while
(T--)
{

scanf("%d",&n);
for
(int i=;i<=n;i++)
{

flag[i]=;
p[i]=i;
}

for
(int i=;i<n-;i++)
{

scanf("%d%d%d",&u,&v,&w);
if
(w==)
{

int
a=findset(u);
int
b=findset(v);
p[a]=b;
}
}

for
(int i=;i<=n;i++)
{

flag[findset(i)]++;
}

int
ans=flag[findset()];
for
(int i=;i<=n;i++)
{

ans=(ans^flag[findset(i)]);
}

printf("%d\n",ans);
}

return
;
}

hdu5606 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. tree(并查集)

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

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

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

随机推荐

  1. 使用 SourceTree 遇到冲突的解决方法

    首先,更新代码之前先 git stash ,然后 git pull ,再 git stash pop 这时候如果本地改的代码跟线上的冲突了,就报错了.那么就需要手动解决冲突. 打开存在冲突的文件,会看 ...

  2. 使用java命令运行class文件带包名时出错

    会出现classnotfound的错误. 如:文件x:/Test.java; package xx public class Test{} javac 编译后,java命令出错,需要手动构建包目录xx ...

  3. python 快速排序详述

    快速排序是对“冒泡排序”的优化算法,都属于交换排序类. 描述:它通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据要小,然后再按此方法对这两部分数据分别进行快速 ...

  4. 解决PL/SQL Developer过期

    1 2 3 4 5 6 分步阅读 PL/SQL Developer过期了,又没有注册码,又不想花钱买,而且事情又非常急,这时候怎么办?不要着急,请随小编一起解决这种情况吧. 工具/原料   PL/SQ ...

  5. cocos2dx使用cocostudio导出的ui

    local uilocal function createLayerUI() if not ui then ui=cc.Layer:create(); createLayerUI=nil; end r ...

  6. zip 解压脚本

    zip 解压脚本 gpk-unzip.py #!/usr/bin/env python # -*- coding: utf-8 -*- # unzip-gbk.py import os import ...

  7. sublime 添加 颜色插件 colorcoder

    高亮所有变量,因此可以极大的简化代码定位.尤其是对那些有阅读障碍的程序员非常有帮助.

  8. Django中日志管理

    在settings中设置日志的相关信息,然后再逻辑代码区就可以保存相应的信息了 #简单设置: LOGGING = { 'version': 1, 'disable_existing_loggers': ...

  9. iOS category 类别 和 extension 扩展

    category  类别 又称为 分类 在ios项目开发中允许使用类别为现有的类添加新的方法,并不需要创建子类.通过类别我们可以动态地为现有的类添加新的方法,可以将类的定义模块化地布局到多个相关文件中 ...

  10. 网页中显示xml,直接显示xml格式的文件

    第一种方法 使用<pre></pre>包围代码(在浏览器中测试不行啊,但是在富编辑器中又可以,怪):使用<xmp></xmp>包围代码(官方不推荐,但是 ...