HDU1325 Is It A Tree? 【并查集】
Is It A Tree?
There is exactly one node, called the root, to which no directed edges point.
Every node except the root has exactly one edge pointing to it.
There is a unique sequence of directed edges from the root to each node.
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not.



In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.
of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.
6 8 5 3 5 2 6 4
5 6 0 0
8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0
3 8 6 8 6 4
5 3 5 6 5 2 0 0
-1 -1
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
题意:给定一个图,推断是否为树。
题解:须要推断森林,推断回路,推断入度是否大于1.
#include <stdio.h>
#include <string.h>
#define maxn 10002 int in[maxn], pre[maxn], count;
bool vis[maxn]; int ufind(int k)
{
int a = k;
while(pre[k] != -1) k = pre[k];
int b;
while(pre[a] != -1){
b = pre[a];
pre[a] = k;
a = b;
}
return k;
} int main()
{
//freopen("stdin.txt", "r", stdin);
int a, b, ok = 1, cas = 1;
memset(pre, -1, sizeof(pre));
while(scanf("%d%d", &a, &b)){
if(a < 0) break;
if(a == 0){
printf("Case %d is ", cas++);
if(count != 1) ok = 0;
printf(ok ? "a tree.\n" : "not a tree.\n");
ok = 1; count = 0;
memset(in, 0, sizeof(in));
memset(vis, 0, sizeof(vis));
memset(pre, -1, sizeof(pre));
continue;
}
if(!ok) continue;
if(!vis[a]){ ++count; vis[a] = 1; }
if(!vis[b]){ ++count; vis[b] = 1; }
if(++in[b] > 1) ok = 0;
a = ufind(a); b = ufind(b);
if(a == b) ok = 0;
else { pre[a] = b; --count; }
}
return 0;
}
HDU1325 Is It A 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 ...
- 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 ...
- HDU 5606 tree 并查集
tree 把每条边权是1的边断开,发现每个点离他最近的点个数就是他所在的连通块大小. 开一个并查集,每次读到边权是0的边就合并.最后Ansi=size[findset(i)],size表示每个并 ...
- [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 ...
- tree(并查集)
tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- 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 ...
随机推荐
- ExtJS4 根据分配不同的树形菜单在不同的角色登录后
继续我的最后.建立cookie后,带他们出去 var userName = Ext.util.Cookies.get('userName'); var userAuthority = Ext.util ...
- poj 3237 Tree(树链拆分)
题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...
- Andrew Stankevich's Contest (1)
Andrew Stankevich's Contest (1) 打一半出门了,回来才补完了...各种大数又不能上java..也是蛋疼无比 A:依据置换循环节非常easy得出要gcd(x, n) = 1 ...
- Centos6.5下一个Ceph存储集群结构
简单的介绍 Ceph的部署模式下主要包括下面几个类型的节点 • Ceph OSDs: A Ceph OSD 进程主要用来存储数据,处理数据的replication,恢复,填充.调整资源组合以及通过检查 ...
- 学习vi和vim编辑(3):一个简单的文本编辑器(2)
然后文章,继续评论vi编辑简单的文本编辑命令. 本文主要是删除的文字.复制,运动命令. 删除文本: 正如上一篇文章中讲过的,对于删除命令("d")也具有"(command ...
- BC 2015在百度之星程序设计大赛 - 预赛(1)(矩形区域-旋转卡)
矩形区域 Accepts: 717 Submissions: 1619 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HTTP响应代码
HTTP响应代码 1xx - 消息通知 这些状态代码表示临时响应.client在收到常规响应.应准备接收一个或多个 1xx 应. · 100 - Continue 初始的请求已经接受,客户应当继续发送 ...
- 解决因特网和xshell考虑到问题
首先需要解释.我们学校的网络是免费的.无论是实验室或宿舍.因此,互联网是基于Mac地址分配IP的,所以我VirtualBox安装了centos之后,话.就须要将VirtualBox的mac地址改成和我 ...
- 云梯vpn
刚和大饼合买了一个云梯的vpn 表示可以把俺的优惠连接放出来了 貌似必须是新注册用户才能够享用优惠 http://protizi.com/?r=5e3fecd7eae558ec 把云梯推荐给朋友们 让 ...
- Android checkbox和radiobutton 以及Toast和AlertDialog的使用
package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...