tree

把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小.

开一个并查集,每次读到边权是0的边就合并.最后Ans​i​​=size[findset(i)],size表示每个并查集根的size

Ans_i=size[findset(i)],sizeAns​i​​=size[findset(i)],size表示每个并查集根的sizesize.

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstdlib>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const int maxn=+;
int a[maxn];
int fa[maxn];
int find(int x)
{
if(x==fa[x])return x;
return fa[x]=find(fa[x]);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
memset(a,,sizeof(a));
for(int i=; i<=n; ++i)
fa[i]=i;
for(int i=; i<n; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(w==)
{
int fx=find(u);
int fy=find(v);
if(fx==fy)continue;
fa[fy]=fx;
}
}
for(int i=; i<=n; i++)
{
int f=find(i);
a[f]++;
}
int ans=;
for(int i=; i<=n; i++)
{
ans=(ans^a[fa[i]]);
}
printf("%d\n",ans);
}
return ;
}

HDU 5606 tree 并查集的更多相关文章

  1. Hdu.1325.Is It A Tree?(并查集)

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

  2. hdu 1325 Is It A Tree? 并查集

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

  3. tree(并查集)

    tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  4. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  5. Is It A Tree?(并查集)

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

  6. CF109 C. Lucky Tree 并查集

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

  7. [Swust OJ 856]--Huge Tree(并查集)

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

  8. Codeforces Round #363 (Div. 2)D. Fix a Tree(并查集)

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

  9. Is It A Tree?(并查集)(dfs也可以解决)

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

随机推荐

  1. 【BZOJ 2875】 [Noi2012]随机数生成器

    Description  给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 Input ...

  2. Calendar GData API / Google Calendar Connectors deprecation

    http://googleappsupdates.blogspot.fr/2014/06/calendar-gdata-api-google-calendar.html

  3. sql with递归

       with temp as ( select Id, UserId, OfficeID, RoleId, DeptId, IsDelete, IsEnd, ParentId from [dbo]. ...

  4. HttpWebRequest

    同步请求=====================================================================================  byte[] da ...

  5. windows环境下装node.js,npm,express

    windows下安装跟mac环境安装(戳我戳我戳我)大同小异. 1. 下载node.js for Mac 地址: http://nodejs.org/download/ 注意看自己系统是32位还是64 ...

  6. 类似nike+、香蕉打卡的转场动画效果-b

    首先,支持并感谢@wazrx 的 http://www.jianshu.com/p/45434f73019e和@onevcat 的https://onevcat.com/2013/10/vc-tran ...

  7. php如何查找会员无限分类的所有上级和所有下级

    a推广出的a-1,a-2继续推广,得到a-1-1,a-1-2等等数据库设计思路如下:用户表中有一个son这么一个字段,这个字段中存放名下所有会员的id,用分号隔开.这个字段的维护:比如a-1-1推广出 ...

  8. leetcode3 Two Sum III – Data structure design

    Question: Design and implement a TwoSum class. It should support the following operations: add and f ...

  9. 对JAVA动态代理的理解

    叫动态代理就代表着有“静态代理”这回事. 而且,通常“动态”至少听着更NB一点. 关键就在于不明白啥叫“动”,这个得跟“静”比较下. 在我的理解,静态代理得自己声明一个类,实现跟被代理对象同样的接口. ...

  10. HDU4545+LCS

    最长公共子序列. /* LCS 最长公共子序列 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...