tree(简单并差集)
tree
There is a tree(the tree is a connected graph which contains nn points and n-1n−1 edges),the points are labeled from 1 to nn,which edge has a weight from 0 to 1,for every point i\in[1,n]i∈[1,n],you should find the number of the points which are closest to it,the clostest points can contain ii itself.
the first line contains a number T,means T test cases.
for each test case,the first line is a nubmer nn,means the number of the points,next n-1 lines,each line contains three numbers u,v,wu,v,w,which shows an edge and its weight.
T\le 50,n\le 10^5,u,v\in[1,n],w\in[0,1]T≤50,n≤105,u,v∈[1,n],w∈[0,1]
for each test case,you need to print the answer to each point.
in consideration of the large output,imagine ans_iansi is the answer to point ii,you only need to output,ans_1~xor~ans_2~xor~ans_3..~ans_nans1 xor ans2 xor ans3.. ansn.
1
3
1 2 0
2 3 1
1 in the sample. ans_1=2ans1=2 ans_2=2ans2=2 ans_3=1ans3=1 2~xor~2~xor~1=12 xor 2 xor 1=1,so you need to output 1.
题解:找每个点距离自己最近的点的个数的异或值,注意最近点包括自己;
思路:并差集,将权值为0的点组成一颗树,这棵树代表的就是距离自己最近的点的个数;
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const int MAXN=100010;
int a[MAXN];
int n;
struct Node{
int u,v,w;
Node init(){SI(u);SI(v);SI(w);}
}dt[MAXN];
int pre[MAXN],num[MAXN];
int find(int x){
return pre[x]=x==pre[x]?x:find(pre[x]);
}
void merge(Node a){
int f1=find(a.u),f2=find(a.v);
if(pre[f1]!=f2)pre[f2]=f1,num[f1]+=num[f2];
}
void initial(){
for(int i=1;i<=n;i++)pre[i]=i,num[i]=1;
}
int main(){
int T;
SI(T);
while(T--){
mem(a,0);
SI(n);
initial();
int u,v,w;
for(int i=0;i<n-1;i++){
dt[i].init();
w=dt[i].w;
if(!w)merge(dt[i]);
}int ans=0;
for(int i=1;i<=n;i++)ans^=num[pre[i]];
printf("%d\n",ans);
}
return 0;
}
tree(简单并差集)的更多相关文章
- BTree和B+Tree 简单区别
本篇作用于各种树之间的区别,非算法详细介绍,只是给我们这种非科班出身的一种大概的印象,现在网上更多是讲各种树的怎么实现的细节问题,本篇不涉及那么高深,如果详细了解可以查阅他人的资料,很多大神已经说的很 ...
- 关于Trie Tree简单实现
最近突然有兴致hiho一下了,实现了下trie tree,感觉而言,还是挺有意思的,个人觉得这货不光可以用来查单词吧,其实也可以用来替代Hash,反正查找,插入复杂度都挺低的,哈哈,啥都不懂,瞎扯.. ...
- Huffman Tree 简单构造
//函数:构造Huffman树HT[2*n-1] #define MAXVALUE 9999//假设权值不超过9999 #define MAXLEAF 30 #define MAXNODE MAXLE ...
- Codeforces 1153D Serval and Rooted Tree (简单树形DP)
<题目链接> 题目大意: Serval拥有的有根树有n个节点,节点1是根. Serval会将一些数字写入树的所有节点.但是,有一些限制.除叶子之外的每个节点都有一个写入操作的最大值或最小值 ...
- 【hdu6121】 Build a tree 简单数学题
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6121 我好像推得挺久的诶..... 题目大意:给你一棵有$n$个点的树,根节点为$0$,对于其余节点 ...
- easy html+css tree 简单的HTML+css导航树
code: show:
- HDU 6228 tree 简单思维树dp
一.前言 前两天沈阳重现,经过队友提点,得到3题的成绩,但是看到这题下意识觉得题目错了,最后发现实际上是题目读错了....GG 感觉自己前所未有的愚蠢了....不过题目读对了也是一道思维题,但是很好理 ...
- 二叉树:B+tree等
二叉树:(树是一种可以递归定义数据结构) 度:节点的个数 深度:层数(即从根点到叶子节点的层数) 满二叉树:指底层叶子节点左右均存在的二叉树. 完全二叉树:指底层叶子节点的右侧均存在的二叉树. 一般二 ...
- LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)
669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...
随机推荐
- [LeetCode]题解(python):010-Regular Expression Matching
题目来源: https://leetcode.com/problems/regular-expression-matching/ 题意分析: 这道题目定义了两个正则表达式规则.’.’代表任意字符,’* ...
- Activity之间定时跳转
起源:很多应用在打开时,首先会加载欢迎页面,经过几秒后再跳转到主页面. 下面,我通过两种不同的方式来实现页面的定时跳转. 第一种方式: 通过Timer类的schedule方法. 实现从MainActi ...
- Linux文件系统与结构
一.Linux文件系统结构 /bin 二进制的缩写,用来放置可执行的二进制程序,基本命令 /boot 用来存放启动文件,kernel 和boot配置文件 /dev 用来放置设备文件 /dev/cons ...
- J2SE知识点摘记(八)
1. 多线程指的是在单个进程中可以同时运行多个不同的线程,执行不用的任务.多线程意味着一个程序的多行语句可以看上去几乎同时进行. 同样作为基本的执行单元,线程是划分得比进程更小的执行单位 ...
- Drools
http://www.infoq.com/news/2009/06/drools-5.0-release http://www.javacodegeeks.com/2014/03/event-proc ...
- C++箴言:避免构造或析构函数中调用虚函数
如果你已经从另外一种语言如C#或者Java转向了C++,你会觉得,避免在类的构造函数或者析构函数中调用虚函数这一原则有点违背直觉.但是在C++中,违反这个原则会给你带来难以预料的后果和无尽的烦恼. 正 ...
- logrotate 清理tomcat日志
rsyslog tomcat 服务器: 192.168.32.215 input(type="imfile" File="/usr/local/apache-tomcat ...
- 你是否决绝平庸,你有勇气来学C/C++吗,有勇气来检验你是否经得起世界五百强的面试
如果你来传智播客学习 你的目标就是要积累工作经验 有机会参加世界五百强的面试 秒杀世界五百强的面试 赢得高薪的offer! C/C++课程大纲 C语言3周21天 完全掌握C语言的本质,成为一名合 ...
- hdu1114Piggy-Bank(DP完全背包)
题意:在ACM可以做任何事情,必须准备和预算获得必要的财政支持.这次行动的主要收入来自不可逆绑定金钱(IBM).背后的想法很简单.每当一些ACM成员有任何小的钱,他把所有的硬币和成小猪银行抛出.你知道 ...
- js中字符串方法
字符串方法: 1. charAt(索引值)//通过索引值获取字符串中对应的值 例如: var str='sdf123'; alert(str.charAt(0));//结果弹出第一个索引对应的值:s