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. 1016: [JSOI2008]最小生成树计数 - BZOJ

    Description 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一条边不同,则这两个最小生成树就是不同的 ...

  2. 关闭MyEclipse的Quick Update

    关闭MyEclipse的Quick Update, Windows > Preferences > MyEclipse > Community Essentials, 把选项 &qu ...

  3. 双十一运维怎么过,OneAPM 帮你救火,轻松运维

    一年一度的双十一来了 有人剁手买买买,有人愁眉苦脸抱着服务器哭 对,说的就是你,汪 苦逼的运维工程师双十一怎么过来的? 守着电脑看预警还是通宵加服务器扩容 还是帮女朋友守着购物车等着一键清空 或者 今 ...

  4. 2013流行Python项目汇总

    2013流行Python项目汇总 转自:http://www.kankanews.com/ICkengine/archives/102963.shtml Python作为程序员的宠儿,越来越得到人们的 ...

  5. Deployment of VC2008 apps without installing anything

    If you create a default CRT/MFC application with VS2008, this application will not run on other comp ...

  6. 【leetcode】Palindrome Number (easy)

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  7. Android SlidingMenu的getSupportActionBar无法找到的解决

    getSupportActionBar()方法不能用 进入Library中的src下找到SlidingFragmentActivity.java,修改 public class SlidingFrag ...

  8. 解读Q_GLOBAL_STATIC(QFontDatabasePrivate, privateDb)

    根据 src/corelib/global.h template <typename T>class QGlobalStatic{public: T *pointer; inline QG ...

  9. SRM589

    250: 给一个串S,可以做这样的操作,可以将串中的一种字母变成另一种字母,代价是该种字母的数量.求解的问题是,最小的代价将串S变成回文串. 根据回文关系,我们可以形成等价对应关系,a与b等价对应说明 ...

  10. 如何快速查看linux的发行版信息

    思路一: 在CentOS中想查看发行版信息,输入了lsb_release -a 命令却报错了,通过输入以下命令进行安装 yum install redhat-lsb -y 然后继续查看发行版信息 [r ...