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. Windows下Apache 虚拟主机 VirtualHost 配置

    以下方式适合原生 Apache, XAMPP 和 WAMP 套件 1.修改Apache配置文件(httpd.conf),如下: # Virtual hostsInclude conf/extra/ht ...

  2. wget www.baidu.com执行流程分析

    下载网页的关键函数: main.c(main) b 1394 url_parse:解析url,获取url相关信息,返回结构体 struct url 的指针,存于 url_parsed retrieve ...

  3. Xcode Coule not launch "aaa" press launch failed:timed out waiting for app launch

    遇见这个问题 可能是 由于 runapp 的时候设置里面 设置为release了. 解决办法是:见图 build configuration 设置成 debug 状态就OK了. 要是上面的不行就试一下 ...

  4. 走进C标准库(2)——"stdio.h"中的fopen函数

    其他的库文件看起来没有什么实现层面的知识可以探究的,所以,直接来看stdio.h. 1.茶余饭后的杂谈,有趣的历史 在过去的几十年中,独立于设备的输入输出模型得到了飞速的发展,标准C从这个改善的模型中 ...

  5. SQL Server一些重要视图 1

    第一个: sys.indexs 每个堆与索引在它上有一行. 第二个: sys.partitions每个堆与索引的每一个分区返回一行.每一张表最多可以有1000个区. 第三个: sys. allocat ...

  6. Xcode 3.2.5免证书开发调试

    Xcode 3.2.5免证书开发调试 xcode3.2.5 应该没人用了.这里做个保存而已. Xcode编译遇到过 Code Sign error: a valid provisioning prof ...

  7. springMVC+spring+mybatis整合过程中遇到的问题

    今天在配置SSM整合的过程中遇到了几个错误,折腾了好久,具体如下 1.java.lang.IllegalArgumentException: Mapped Statements collection ...

  8. poj2636---Electrical Outlets(插线板)

    #include <stdio.h> #include <stdlib.h> int main() { int n,nc,i; scanf("%d",&am ...

  9. UVA507-- Jill Rides Again

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. <转>ASP.NET学习笔记之MVC 3 数据验证 Model Validation 详解

    MVC 3 数据验证 Model Validation 详解  再附加一些比较好的验证详解:(以下均为引用) 1.asp.net mvc3 的数据验证(一) - zhangkai2237 - 博客园 ...