tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 863    Accepted Submission(s): 409
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.
 

Source
 

题意:有n个点和n-1条边,一条边连接两个点,每条边都有权值为0或1(只有这两个值)求出每个点的距离其最近的点的个数(两点之间距离为0包括自身在内)xi 然后输出所有xi异或的结果
题解:先用并查集合并所有权值为0的边的两端点,(这样就分成多个块,每个块之间的任意两点之间的权值都为0)用一个数组s记录相同根节点的点的个数,遍历所有点则s[set[i]]就是距离i点最近的点的个数
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#define LL long long
#define PI atan(1.0)*4
#define DD doublea
#define MAX 101000
#define mod 10007
using namespace std;
int set[MAX];
int s[MAX];
int find(int fa)
{
int ch=fa;
int t;
while(set[fa]!=fa)
fa=set[fa];
while(ch!=fa)
{
t=set[ch];
set[ch]=fa;
ch=t;
}
return fa;
}
void mix(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
int main()
{
int n,m,j,i,t;
int u,v,w;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
set[i]=i;
s[i]=0;
} for(i=1;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(w==0)
mix(u,v);
}
//printf("#\n");
for(i=1;i<=n;i++)
{
int ans=find(i);
s[ans]++;
}
int ant=0;
for(i=1;i<=n;i++)
{
ant^=s[set[i]];
}
printf("%d\n",ant);
}
return 0;
}

  

 

BestCoder Round #68 (div.2) tree(hdu 5606)的更多相关文章

  1. BestCoder Round #68 (div.2) geometry(hdu 5605)

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

  2. Codeforces Round #249 (Div. 2)B(贪心法)

    B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  3. BestCoder Round #68 (div.2) 1002 tree

    题意:给你一个图,每条边权值0或1,问每个点周围最近的点有多少个? 思路:并查集找权值为0的点构成的连通块. #include<stdio.h> #include<string.h& ...

  4. BestCoder Round 69 Div 2 1001&& 1002 || HDU 5610 && 5611

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5610 如果杠铃总质量是奇数直接impossible 接着就考验耐心和仔细周全的考虑了.在WA了三次后终于发 ...

  5. BestCoder Round #68 (div.2)

    并查集 1002 tree 题意:中文题面 分析:(官方题解)把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=siz ...

  6. BestCoder Round #50 (div.1) 1002 Run (HDU OJ 5365) 暴力枚举+正多边形判定

    题目:Click here 题意:给你n个点,有多少个正多边形(3,4,5,6). 分析:整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正四边形即可. #include ...

  7. Codeforces Round #272 (Div. 1)D(字符串DP)

    D. Dreamoon and Binary time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  8. Codeforces Round #532 (Div. 2)- C(公式计算)

    NN is an experienced internet user and that means he spends a lot of time on the social media. Once ...

  9. Codeforces Round #527 (Div. 3)F(DFS,DP)

    #include<bits/stdc++.h>using namespace std;const int N=200005;int n,A[N];long long Mx,tot,S[N] ...

随机推荐

  1. Android Studio Gradle

    http://blog.zhaiyifan.cn/2016/03/14/android-new-project-from-0-p2/

  2. 面试题_89_to_92_单元测试 JUnit 面试题

    89)如何测试静态方法?(答案)可以使用 PowerMock 库来测试静态方法. 90)怎么利用 JUnit 来测试一个方法的异常?(答案) 91)你使用过哪个单元测试库来测试你的 Java 程序?( ...

  3. unique() 去重函数

    unique()函数是一个去重函数,STL中unique的函数 unique的功能是去除相邻的重复元素(只保留一个), 还有一个容易忽视的特性是它并不真正把重复的元素删除.他是c++中的函数, 所以头 ...

  4. HDU 4893 线段树

    比赛时太大意,斐波拉契数列开小了. 题目大意:1个序列,3种操作,改变序列某个数大小,将序列中连续的一段每个数都变成其最近的斐波拉契数,以及查询序列中某一段的数之和. 解题思路:维护add[]数组表示 ...

  5. UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge

    题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...

  6. BZOJ3806: Neerc2011 Dictionary Size

    题解: 这题搞得我真是酸(dan)爽(teng) 原来一直不会,一定会用到什么神奇的东西.因为重复的不知道如何计算. 今天中午睡起来忽然想到好像可以在正trie上故意走无出边,因为这样就保证了这次统计 ...

  7. WIN7开无线

    可以的,WIN7自身就带有这个功能的,不过默认是不开启的,也可以下载一个软件来弄.1.请却仍您使用的操作系统是微软的Windows 7或者Windows server 2008 R2,正版盗版皆可.( ...

  8. multipath 安装配置

    二. 安装配置 2.1 安装Multipath 查看相关包: [root@rac1 ~]# rpm -qa|grep device-mapper device-mapper-multipath-0.4 ...

  9. [反汇编练习] 160个CrackMe之009

    [反汇编练习] 160个CrackMe之009. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  10. POJ 1486 Sorting Slides (二分图关键匹配边)

    题意 给你n个幻灯片,每个幻灯片有个数字编号1~n,现在给每个幻灯片用A~Z进行编号,在该幻灯片范围内的数字都可能是该幻灯片的数字编号.问有多少个幻灯片的数字和字母确定的. 思路 确定幻灯片的数字就是 ...