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
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
.
itself.
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]
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
3
1 2 0
2 3 1
Sample Output
1in the sample.$ans_1=2$$ans_2=2$$ans_3=1$$2~xor~2~xor~1=1$,so you need to output 1.
题面:给你一些点,求各个点联通块的个数的异或值。 遇到1则断,0是联通。
比较容易,0就unit,1就不管好了,最后输出的时候整理一下fa[i]的值 如果是偶数就跳过,因为偶数次异或是0,遇到奇数则操作。
比如sample中其实不需要算2^2,只需要算1就好,同理如果有2个2,3个1的话,只需要考虑1,而不需要考虑1^1^1。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <cctype>
#include <ctime>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
#define N 100050
#define eps 1e-5
#define pai acos(-1)
#define MOD 1e9+7
int fa[N],a[N];
void init()
{
for(int i=1;i<=100005;i++)
{
fa[i]=i;
a[i]=0;
}
}
int findd(int x)
{
if(x==fa[x]) return x;
else return fa[x]=findd(fa[x]);
}
void unite(int x,int y)
{
x=findd(x),y=findd(y);
if(x==y) return ;
else fa[x]=y;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
ll _begin=clock();
#endif
int n,m;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
init();
scanf("%d",&m);
int mm=m;
while(--m)
{
int from,to,w;
scanf("%d%d%d",&from,&to,&w);
if(!w)
{
unite(from,to);
}
}
m=mm;
for(int i=1;i<=m;i++) fa[i]=findd(i);
int ans=0;
for(int i=1;i<=m;i++)
{
a[fa[i]]++;
}
for(int i=1;i<=100000;i++)
{
if(a[i]%2==0) continue;
ans=ans^a[i];
}
printf("%d\n",ans);
}
#ifndef ONLINE_JUDGE
ll _end=clock();
printf("%lldms\n",_end-_begin);
#endif
return 0;
}