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

Hint

in the sample.

  • ans_1=2
  • ans_2=2
  • ans_3=1

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

题意:给出 t 组数据,每组数据给出一个整数 n 代表有 n 个点 n-1 条边,然后依次给出 n-1 条边及其权值(仅有 0、1),要统计每个点距离他最近的点的个数(包括其自身),最后将每个点距离他最近的个数异或后输出

思路:由于要统计距离每个点最近的点的个数,且边的权值仅有 0、1,因此可以考虑当边权为 0 时,使用并查集将点连接,边权为 1 时,不进行任何操作,这样将所有的点可划分为多个连通块,然后统计每个点所在的连通块中的点的个数即可,最后将所有点的值异或输出。

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 16007
#define INF 0x3f3f3f3f
#define N 1000001
#define LL long long
using namespace std;
int father[N];
int num[N];
int Find(int x){while(father[x]!=x)x=father[x];return x;
}
void Union(int x,int y){x=Find(x);y=Find(y);if(x!=y)father[x]=y;num[y]+=num[x];
}
int main(){int t;scanf("%d",&t);while(t--){int n;scanf("%d",&n);memset(num,0,sizeof(num));for(int i=1;i<=n;i++)father[i]=i;int m=n-1;for(int i=0;i<m;i++){int x,y,w;scanf("%d%d%d",&x,&y,&w);if(w==0)Union(x,y);}for(int i=1;i<=n;i++){//枚举每个点father[i]=Find(i);//寻找每个点的父节点num[father[i]]++;//统计子节点个数(包括其自身)}for(int i=1;i<=n;i++)//统计父节点外的点的个数num[i]=num[father[i]];int res=0;for(int i=1;i<=n;i++)//将所有点的值异或res^=num[father[i]];printf("%d\n",res);}return 0;
}

Tree(HDU-5060)相关推荐

  1. Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)

    Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...

  2. Codeforces Round #665 (Div. 2) Maximum Distributed Tree(树上贪心)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 CF1401D Maximum Distributed Tree(树上贪心) 给定一棵 nnn 个节点 ...

  3. 04-树6 Complete Binary Search Tree(30 分)

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  4. 一首好听的英文歌lemon tree(柠檬树)的中文歌词

    一首好听的英文歌lemon tree(柠檬树)的中文歌词 lemon tree的中文 i'm sitting here in a boring room.我坐在这--一间空屋子里  it's just ...

  5. C++——LCA例题——Tree(祖孙关系)

    Tree(祖孙关系) 题目背景 SOURCE:NOIP2015-SHY-3 题目描述 已知一棵 n 个节点的有根树.有 m 个询问.每个询问给出了一对节点的编号 x 和 y ,询问 x 与 y 的祖孙 ...

  6. 【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】

    [226-Invert Binary Tree(反转二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun- ...

  7. 2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)

    题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~ 这道题的麻烦之处在于如何从一个字符串中去找数字.先首先 ...

  8. 有源汇有上下界最大流/最小流 配题(HDU 3157)

    因为是有源汇所以设源点为 s,汇点为 t. 有源汇有上下界最大流: 连接一条 t 指向 s 的边,容量为 INF. 通过上述步骤,现在图变成了无源汇网络. 引入超级源点 S,超级汇点 T. 连接一条 ...

  9. LeetCode 1506. Find Root of N-Ary Tree(异或)

    文章目录 1. 题目 2. 解题 1. 题目 Given all the nodes of an N-ary tree as an array Node[] tree where each node ...

  10. 二叉搜索树(Binary Search Tree)(Java实现)

    文章目录 1.二叉搜索树 1.1. 基本概念 1.2.树的节点(BinaryNode) 1.3.构造器和成员变量 1.3.公共方法(public method) 1.4.比较函数 1.5.contai ...

最新文章

  1. R语言构建ElasticNet回归模型实战:基于mtcars数据集
  2. 如何让所请读取的数据自动产生编号.
  3. java开发一年多少钱_4年Java程序员:月薪不过3万,就不要拿命换钱了
  4. 多而杂不会成为重点-丰收节贸易会:未来农业的发展方向
  5. Synchronized和Lock的区别
  6. Redis数据分布一致性哈希
  7. 密码学系列之:memory-bound函数
  8. oracle 叠加代码写法,利用st_geometry进行图形叠加分析
  9. 在AIX上编译Samba
  10. “约见”面试官系列之常见面试题之第九十篇之页面加载触发函数(建议收藏)
  11. 334. Increasing Triplet Subsequence
  12. Xcode 9.0 报错, Safe Area Layout Guide Before IOS 9.0 和launch screens may not set custom classnames报错
  13. java实现SPFA算法
  14. CAD如何导出PDF格式
  15. word添加脚注后正文跑到下一页
  16. windows下使用命令打开pdf文件
  17. Java 汉字 转 拼音/首字母
  18. qemu: usb存储设备仿真
  19. v4l2架构专题模块handler分析 -- handler ctrl的注册2
  20. 有趣的十个Python实战项目,让你瞬间爱上Python!

热门文章

  1. 又又叒更新,Win 12要来了?
  2. Altium Designer之PCB
  3. ARM和X86功耗差别的深层原因探讨
  4. c++十进制转二进制_二进制与十进制相互转换的原理
  5. 功能强大!IntelliJ IDEA 2022.1正式发布
  6. 这个开源组织里的项目都是精品
  7. 面试时说Redis是单线程的,被喷惨了!
  8. 面向犯罪编程,9名程序员锒铛入狱
  9. 【JEECG技术文档】JEECG部门管理员操作手册
  10. 架构设计 | 分布式系统调度,Zookeeper集群化管理