tree

 Accepts: 156
 Submissions: 807
 Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
Problem Description

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.

Input

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≤10​5​​,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 ans_ians​i​​ is the answer to point ii,you only need to output,ans_1~xor~ans_2~xor~ans_3..~ans_nans​1​​ xor ans​2​​ xor ans​3​​.. ans​n​​.

Sample Input
1
3
1 2 0
2 3 1
Sample Output
1

in the sample.

ans_1=2ans​1​​=2

ans_2=2ans​2​​=2

ans_3=1ans​3​​=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(简单并差集)的更多相关文章

  1. BTree和B+Tree 简单区别

    本篇作用于各种树之间的区别,非算法详细介绍,只是给我们这种非科班出身的一种大概的印象,现在网上更多是讲各种树的怎么实现的细节问题,本篇不涉及那么高深,如果详细了解可以查阅他人的资料,很多大神已经说的很 ...

  2. 关于Trie Tree简单实现

    最近突然有兴致hiho一下了,实现了下trie tree,感觉而言,还是挺有意思的,个人觉得这货不光可以用来查单词吧,其实也可以用来替代Hash,反正查找,插入复杂度都挺低的,哈哈,啥都不懂,瞎扯.. ...

  3. Huffman Tree 简单构造

    //函数:构造Huffman树HT[2*n-1] #define MAXVALUE 9999//假设权值不超过9999 #define MAXLEAF 30 #define MAXNODE MAXLE ...

  4. Codeforces 1153D Serval and Rooted Tree (简单树形DP)

    <题目链接> 题目大意: Serval拥有的有根树有n个节点,节点1是根. Serval会将一些数字写入树的所有节点.但是,有一些限制.除叶子之外的每个节点都有一个写入操作的最大值或最小值 ...

  5. 【hdu6121】 Build a tree 简单数学题

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6121 我好像推得挺久的诶..... 题目大意:给你一棵有$n$个点的树,根节点为$0$,对于其余节点 ...

  6. easy html+css tree 简单的HTML+css导航树

    code: show:

  7. HDU 6228 tree 简单思维树dp

    一.前言 前两天沈阳重现,经过队友提点,得到3题的成绩,但是看到这题下意识觉得题目错了,最后发现实际上是题目读错了....GG 感觉自己前所未有的愚蠢了....不过题目读对了也是一道思维题,但是很好理 ...

  8. 二叉树:B+tree等

    二叉树:(树是一种可以递归定义数据结构) 度:节点的个数 深度:层数(即从根点到叶子节点的层数) 满二叉树:指底层叶子节点左右均存在的二叉树. 完全二叉树:指底层叶子节点的右侧均存在的二叉树. 一般二 ...

  9. LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)

    669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...

随机推荐

  1. Sencha Touch 之 Ext.fly方法的使用

    Ext.fly方法是Ext.js 4中的flyweight技术,该技术在浏览器中为使用Ext.fly方法的元素节点开辟一块内存,下一次使用Ext.fly方法的元素节点将占据同一块内存,即覆盖前一次的元 ...

  2. 得到IP包的数据意义(简单实现例子)

    #include <stdio.h> #include <unistd.h> #include <linux/if_ether.h> #include <li ...

  3. PHP学习之数据库操作

    PHP数据库操作: 一.连接数据库 mysql_connect() 例:$link=mysql_connent("localhost","root"," ...

  4. 帝国cms <!--list.var1-->产生不同样式

    制作帝国列表模板正常情况下 列表内容模板(list.var) (*) 写 <li class=''>[!--title--]<a href='[!--titleurl--]'> ...

  5. 灵活运用Zend框架

    $aAwardMem = $this->dao_raward->getAwardAndMem($where,'award_level asc',false,false,false,'awa ...

  6. poj2388---求奇数个数字的最中间的数

    #include <stdio.h> #include <stdlib.h> int cmp(const void *a,const void *b) { return (*( ...

  7. kvm中运行kvm

    如何在 KVM 虚拟机上运行 KVM 上次讨论了如何在 VMware ESXi 虚拟机上运行 KVM 问题,前不久有读者想 “在 kvm 上面创建个虚拟机安装 rackspace 的 openstac ...

  8. 理解session机制

    理解session机制 session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息. 当程序需要为某个客户端的请求创建一个session的时候,服务器首 ...

  9. C++ - Vector 计算 均值(mean) 和 方差(variance)

    Vector 计算 均值(mean) 和 方差(variance) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24623187 ...

  10. Android 匿名共享内存C++接口分析

    在上一篇Android 匿名共享内存C接口分析中介绍了Android系统的匿名共享内存C语言访问接口,本文在前文的基础上继续介绍Android系统的匿名共享内存提供的C++访问接口.在C++层通过引入 ...