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. ASCII 字符代码表

  2. error while loading shared libraries: libevent-2.0.so.5解决办法

    安装memcache时,需要建立文件索引或者说文件连接(link),类似windows下的快捷方式 启动服务时出现 error while loading shared libraries: libe ...

  3. Java [Leetcode 137]Single Number II

    题目描述: Given an array of integers, every element appears three times except for one. Find that single ...

  4. UVA 10098 Generating Fast, Sorted Permutation

    // 给你字符串 按字典序输出所有排列// 要是每个字母都不同 可以直接dfs ^_^// 用前面说的生成排列算法 也可以直接 stl next_permutation #include <io ...

  5. 通过userAgent判断手机浏览器类型

    我们可以通过userAgent来判断,比如检测某些关键字,例如:AppleWebKit*****Mobile或AppleWebKit,需要注意的是有些浏览器的userAgent中并不包含AppleWe ...

  6. WEXT driver的执行过程实现(iwpriv部分/softapcontroller)

    之前在看wifi driver源代码时一直有一个疑惑就是net dev的wireless_handlers中(WEXT类型的接口)提供两个iw_handler接口,怎么知道上层是调用的是private ...

  7. HDU 5114 Collision

    Collision Time Limit: 15000/15000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Total ...

  8. 使用IP欺骗Loadrunner并发测试小结

    测试要求:   在本次测试中,我需要并发50个User,每一个User占用一个独立的IP,并且只执行一次脚本.脚本中发起两个请求,其中第一次请求返回200后才执行第二个请求.使用win7 OS.   ...

  9. C++实现网格水印之调试笔记(四)—— 完成嵌入

    接下来的问题是,当模型是对称的时候,结果是符合预期的,但是当模型是不对称的时候,结果是错误的,如下: 输入: 顶点:233 输出: 这又是什么鬼...,我的马呢!!! 看来逻辑上还是有错误 注意这时候 ...

  10. C++实现网格水印之调试笔记(三)—— 初有结果

    错误: error C2338: THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD 这种错误 ...