Tree(HDU-5060)
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 1Sample 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)相关推荐
- Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)
Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...
- Codeforces Round #665 (Div. 2) Maximum Distributed Tree(树上贪心)
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 CF1401D Maximum Distributed Tree(树上贪心) 给定一棵 nnn 个节点 ...
- 04-树6 Complete Binary Search Tree(30 分)
title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...
- 一首好听的英文歌lemon tree(柠檬树)的中文歌词
一首好听的英文歌lemon tree(柠檬树)的中文歌词 lemon tree的中文 i'm sitting here in a boring room.我坐在这--一间空屋子里 it's just ...
- C++——LCA例题——Tree(祖孙关系)
Tree(祖孙关系) 题目背景 SOURCE:NOIP2015-SHY-3 题目描述 已知一棵 n 个节点的有根树.有 m 个询问.每个询问给出了一对节点的编号 x 和 y ,询问 x 与 y 的祖孙 ...
- 【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】
[226-Invert Binary Tree(反转二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun- ...
- 2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)
题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~ 这道题的麻烦之处在于如何从一个字符串中去找数字.先首先 ...
- 有源汇有上下界最大流/最小流 配题(HDU 3157)
因为是有源汇所以设源点为 s,汇点为 t. 有源汇有上下界最大流: 连接一条 t 指向 s 的边,容量为 INF. 通过上述步骤,现在图变成了无源汇网络. 引入超级源点 S,超级汇点 T. 连接一条 ...
- 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 ...
- 二叉搜索树(Binary Search Tree)(Java实现)
文章目录 1.二叉搜索树 1.1. 基本概念 1.2.树的节点(BinaryNode) 1.3.构造器和成员变量 1.3.公共方法(public method) 1.4.比较函数 1.5.contai ...
最新文章
- R语言构建ElasticNet回归模型实战:基于mtcars数据集
- 如何让所请读取的数据自动产生编号.
- java开发一年多少钱_4年Java程序员:月薪不过3万,就不要拿命换钱了
- 多而杂不会成为重点-丰收节贸易会:未来农业的发展方向
- Synchronized和Lock的区别
- Redis数据分布一致性哈希
- 密码学系列之:memory-bound函数
- oracle 叠加代码写法,利用st_geometry进行图形叠加分析
- 在AIX上编译Samba
- “约见”面试官系列之常见面试题之第九十篇之页面加载触发函数(建议收藏)
- 334. Increasing Triplet Subsequence
- Xcode 9.0 报错, Safe Area Layout Guide Before IOS 9.0 和launch screens may not set custom classnames报错
- java实现SPFA算法
- CAD如何导出PDF格式
- word添加脚注后正文跑到下一页
- windows下使用命令打开pdf文件
- Java 汉字 转 拼音/首字母
- qemu: usb存储设备仿真
- v4l2架构专题模块handler分析 -- handler ctrl的注册2
- 有趣的十个Python实战项目,让你瞬间爱上Python!