BestCoder Round #68 (div.2) 1002 tree

题意:给你一个图,每条边权值0或1,问每个点周围最近的点有多少个?

思路:并查集找权值为0的点构成的连通块。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<math.h>
 5 #include<algorithm>
 6 #define clc(a,b) memset(a,b,sizeof(a))
 7 using namespace std;
 8 int pre[100010],a[100010];//标记根节点
 9 int find(int x)//查找根节点
10 {
11     int r=x;
12     while(pre[r]!=r)//返回根节点r
13         r=pre[r];
14     int i=x,j;
15     while(i!=r)//路径压缩
16     {
17         j=pre[i];//在改变上级之前用临时变量j记录下他的值
18         pre[i]=r;//把上级改为根节点
19         i=j;
20     }
21     return r;
22 }
23 void join(int x,int y)//判断x,y是否连通.如果已经连通,就不用管了;如果不连通,就把它们所在的连通分支合并起
24 {
25     int fx=find(x),fy=find(y);
26     if(fx!=fy) pre[fy]=fx;
27 }
28 int main()
29 {
30     //freopen("in.txt","r",stdin);
31     int t,n,x,y,z;
32     while(~scanf("%d",&t))
33     {
34         for(int i=0; i<t; i++)
35         {
36             clc(a,0);
37             scanf("%d",&n);
38             for(int i=1; i<=n; i++)
39             {
40                 pre[i]=i;
41             }
42             for(int i=1; i<n; i++)
43             {
44                 scanf("%d%d%d",&x,&y,&z);
45                 if(!z) join(x,y);
46             }
47             for(int i=1; i<=n; i++)
48             {
49                 a[find(i)]++;
50             }
51             long long ans=0;
52             for(int i=1; i<=n; i++)
53             {
54                 int p=a[find(i)];
55                 if(p) ans^=p;
56                 else ans^=1;
57             }
58             printf("%I64d\n",ans);
59         }
60     }
61     return 0;
62 }

时间: 2024-10-07 06:07:55

BestCoder Round #68 (div.2) 1002 tree的相关文章

计算几何(水)BestCoder Round #50 (div.2) 1002 Run

题目传送门 1 /* 2 好吧,我不是地球人,这题只要判断正方形就行了,正三角形和正五边形和正六边形都不可能(点是整数). 3 但是,如果不是整数,那么该怎么做呢?是否就此开启计算几何专题了呢 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-8 19:54:14 8 * File Name :B.cpp 9 ************

HDU 5671 Matrix (BestCoder Round #81 (div.2) 1002)

传送门 Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 311    Accepted Submission(s): 142 Problem Description There is a matrix M that has n rows and m columns (1≤n≤1000,1≤m≤1000).Then we

BestCoder Round #82 (div.1) 1002 HDU 5677 dp-类似多重背包的思想

链接:戳这里 ztr loves substring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description ztr love reserach substring.Today ,he has n string.Now ztr want to konw,can he take out exactly k palindrome from all s

BestCoder Round #66 (div.2) 1002

GTW likes gt Accepts: 132 Submissions: 772 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 从前,有nn只萌萌的GT,他们分成了两组在一起玩游戏.他们会排列成一排,第ii只GT会随机得到一个能力值b_ib?i??.在第ii秒的时候,第ii只GT可以消灭掉所有排在他前面的和他不是同一组的且能力值小于他的GT. 为了使游戏更加有趣,

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

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

BestCoder Round #50 (div.2)

题目传送:BestCoder Round #50 (div.2) BC感觉越做越无语了 1001.Distribution money AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset> #

BestCoder Round #11 (Div. 2)题解集合

Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155    Accepted Submission(s): 110 Problem Description Bob and Alice got separated in the Square, they agreed that if they get sepa

hdu5418 BestCoder Round #52 (div.2) Victor and World ( floyd+状压dp)

Problem Description After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to

BestCoder Round #11 (Div. 2) 前三题题解

题目链接: huangjing hdu5054 Alice and Bob 思路: 就是(x,y)在两个參考系中的表示演全然一样.那么仅仅可能在这个矩形的中点.. 题目: Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 216    Accepted Submission(s): 166 Problem De