tree(并查集)

时间:2022-09-23 22:45:44

tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 986    Accepted Submission(s): 452

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5606

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
 
 
题解: 把每条边权是1的边断开,发现每个离他最近的点个数就是他所在的联通快的大小,开一个并查集,每次读到编圈是0的边就合并,最后结果就是每个点的父亲的孩子个数加1
太久没有打并查集了水一水
 #include<cstdio>
#include<cstring>
#include<string>
using namespace std;
#define N 100006
int n;
int fa[N];
int num[N];
void init(){
for(int i = ; i < N; i++) fa[i] = i;
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x, int y){
int root1 = find(x);
int root2 = find(y);
if(root1==root2) return;
fa[root1] = root2;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
for(int i = ; i < n-; i++)
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
if(c==){
merge(a,b);
}
}
memset(num,,sizeof(num));
for(int i = ; i <= n; i++)
{
int r = find(i);
num[r]++;
}
int ans = ;
for(int i = ; i <= n; i++)
{
ans = (ans^num[find(i)]);
}
printf("%d\n",ans);
}
return ;
}

tree(并查集)的更多相关文章

  1. Hdu&period;1325&period;Is It A Tree&quest;&lpar;并查集)

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  2. Is It A Tree&quest;&lpar;并查集&rpar;

    Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26002   Accepted: 8879 De ...

  3. CF109 C&period; Lucky Tree 并查集

    Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...

  4. HDU 5606 tree 并查集

    tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并 ...

  5. &lbrack;Swust OJ 856&rsqb;--Huge Tree&lpar;并查集&rpar;

    题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...

  6. Codeforces Round &num;363 &lpar;Div&period; 2&rpar;D&period; Fix a Tree&lpar;并查集&rpar;

    D. Fix a Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Is It A Tree&quest;&lpar;并查集&rpar;(dfs也可以解决)

    Is It A Tree? Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submi ...

  8. 树上统计treecnt&lpar;dsu on tree 并查集 正难则反&rpar;

    题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...

  9. hdu 1325 Is It A Tree&quest; 并查集

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

随机推荐

  1. VUE 入门基础&lpar;3&rpar;

    三,模板语法 Vue将模板编译成虚拟DOM渲染函数,结合响应系统,在应用状态改变时,vue能够智能地计算出重新渲染组件的最小代价并DOM操作上. 插值,文本 数据绑定常见的形式就是使用"Mu ...

  2. &lbrack;Html5&rsqb;sessionStorage和localStorage的区别

    摘要 有时需要在浏览器中保存一些数据,特别在app中嵌入的h5页面中,需要在webview中保存一些数据,作为客户端的数据持久化. h5中web storage有两种存储方式:sessionStora ...

  3. 0x0A和0x0D

    这里主要是在windows下面做的小实验,linux没有试 先贴源码 #include <iostream> #include <string> #include <st ...

  4. 怎样在Ubuntu系统安装可用的QQ

    http://jingyan.baidu.com/article/9f63fb91d1f6bbc8400f0e1c.html

  5. android的jni

    一.底层实现: c文件:hardware/libhardware_legacy/power/power.c 以其中set_screen_state(int)函数为例 其Android.mk中添加:   ...

  6. 文成小盆友python-num11-(1) 线程 进程 协程

    本节主要内容 线程补充 进程 协程 一.线程补充 1.两种使用方法 这里主要涉及两种使用方法,一种为直接使用,一种为定义自己的类然后继承使用如下: 直接使用如下: import threading d ...

  7. Gentoo启动菜单设置:使用官方LiveDVD Grub主题

    设置Gentoo Grub启动主题 拷贝官方LiveDVD grub主题: 下载官方DVD,找到 /boot/grub/themes/GenGrub目录,并拷贝出来. 安装GenGrub主题: 将Ge ...

  8. 简述SharePoint designer 工作流实现用户撤回申请得解决方案,how to revoke&sol;recall application in SharePoint designer workflow

    在工作中很容易碰到用户提出,需要可以撤回申请得需求.有这么几种实现方法. 方案一:designer中加一个parallel(平行)得anction, 然后在里面添加两个平行运行得审批 第一个是给真正得 ...

  9. tooltip&period;css-2&period;0文档

     tooltip.css 纯CSS鼠标提示工具. v. 2.0.0 更新日期:2018.4.12 预览DEMO.  安装: 只需在页面中引入"tooltip.css"或" ...

  10. Smokeping安装部署

    稳定性检测工具smokeping安装配置和使用方法 Smokeping介绍 是对IDC网络质量,稳定性等最好的检测工具,包括常规的 ping,dig,echoping,curl等,可以监视www服务器 ...