HDU 5606 tree 并查集
tree
把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小.
开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并查集根的size
Ans_i=size[findset(i)],sizeAnsi=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 并查集的更多相关文章
- 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 ...
- 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 ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- Is It A Tree?(并查集)
Is It A Tree? Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26002 Accepted: 8879 De ...
- CF109 C. Lucky Tree 并查集
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal re ...
- [Swust OJ 856]--Huge Tree(并查集)
题目链接:http://acm.swust.edu.cn/problem/856/ Time limit(ms): 1000 Memory limit(kb): 10000 Description T ...
- 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 ...
- Is It A Tree?(并查集)(dfs也可以解决)
Is It A Tree? Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
随机推荐
- 【HDU2222】Keywords Search
Problem DescriptionIn the modern time, Search engine came into the life of everybody like Google, Ba ...
- mysql建表且某字段内不允许出现重复值
CREATE TABLE `admin` ( `id` ) NOT NULL AUTO_INCREMENT , `username` varchar() NOT NULL , `password` v ...
- Webstorm10.0.4注册码
分享几个Webstorm10的注册码: (1) user name :EMBRACE ===== LICENSE BEGIN =====17739-1204201000002KkN!4z2O8JEyj ...
- 项目图片上传存储的目录部分代码思路Calendar类获取年月日
在项目中将产品图片全部放置在一个文件夹下面的话,如果图片很多的话,加载速度会减慢,可以按照文件夹按年,月,日来分开存放,图片文件名字取系统时间long类型加上5位随机数字码避免重复. Calendar ...
- hdu 2509 Be the Winner 博弈论
博弈论水题!!! 代码如下: #include<stdio.h> #include<iostream> using namespace std; int main(){ int ...
- struts2 权限拦截器 拦截没有登陆的请求
假设有这样的登陆: ActionContext.getContext().getSession().put("UserMsg", userMsg); 则可以这样判断是否登陆: im ...
- Posix IPC
- js 实现list类
js中没有list类,可以使用Array来实现list类 (function(win) { var ArrayList = function() { this.datas = []; }; var p ...
- Java经典书籍
Java Web开发教程---孙霞JSP应用开发详解(第三版)---刘晓华.张健.周慧贞Spring in Action---Craig Walls精通Struts基于MVC的Java Web设计与开 ...
- winform学习日志(十九)----------真正三层架构之登录
摘要:一:三层构架的基础知识在项目开发的过程中,有时把整个项目分为三层架构,其中包括:表示层(UI).业务逻辑层(BLL)和数据访问层(DAL).三层的作用分别如下: 表示层:为用户提供交互操作界面, ...