Is It A Tree?(并查集)(dfs也可以解决)
Description
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.
Input
Output
Sample Input
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
Sample Output
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree. 题意:给定一定关系,判断其是否是一棵树,注意,空树也是一棵树,不能有成环的情况,不能是森林,不能有入度为2的节点。
下面给出两种思路:
1,并查集:维护一个森林 每输入一组关系(边)的时候,将这两个点连起来看做一棵树,如果可以将其合并到其他的树上的话就将其合并,并且树的总数目加1;
(注意: 只有当第二个端点值在其他树上是根节点的时候才能合并仍保持是一棵树,及从第一个端点指向第二个端点的时候会保证不会出现入度为2的点)
如果不满足合并条件的时候就将定义的树的总数加1,中间过程有可能再将这两棵树连起来,所有操作完成后,统计树的个数,要是树的棵树大于1的话则所给点不能构成一棵树
注意要排除自成环的情况,和空树的情况。
2,dfs:
将所有的边用链接表的方式存起来,当按照正常的顺序扫描一遍的时候如果是一个符合条件的树的话就不会存在一个点被访问两次,即扫描的时候不会出现visit数组被标记为走过的情况
而且当以任一点为开始扫描完一遍的时候不存在没有扫描过的点,因为不存在森林。
这里同样要考虑空树的情况。
/**
---------------------------------------------------
HuHanwu
2012-7-12
并查集快速判断每次输入是破坏树的性质
时间复杂度: o(n) n为节点的个数
---------------------------------------------------
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 1000002 int p[MAXN]; //存放父亲节点 只要存在父亲节点则此节点一存在某棵树中
int find(int x){ return p[x]==x ? x :(p[x]=find(p[x]));} //查找父亲节点并 压缩路径 int main()
{
int a,b,tree_num,t=,father_a,father_b,TF; //tree_num 保存当前的树的棵数
while(~scanf("%d%d",&a,&b) && (a>=||b>=))
{
tree_num=TF=;
if(a==)
{
printf("Case %d is a tree.\n",++t);
continue;
}//空树也是一棵树
if(a==b)
TF=;//不能成环
else
{
memset(p,,sizeof(p)); //用 0 初始化所有节点的父亲节点
p[a]=p[b]=a;
}
while(~scanf("%d%d",&a,&b) && a!=)
{
if(TF== ) // 只要已经判断出该森林已经不是树
continue;
if(p[b]!=) // b节点已经存在
{
father_b=find(b);
if(b!=father_b) // b 节点不是树根就不可以连成一棵树
{
TF=;
continue;
}
else//下面都是当b 为树根的情况
{
if(p[a]!=) // a b 节点都存在则树的的棵数-1
{
p[b]=find(a);//将两棵树连起来,只有当b是父节点的时候才可以这么连
tree_num--;
}
else //a 节点不存在 而 b 节点存在
p[b]=p[a]=a;
}
}
else
{
if(p[a]==) // a b 节点都不存在则树的棵数+1
{
p[a]=p[b]=a;
tree_num++;
}
else //a 节点存在 而 b 节点不存在
p[b]=find(a);
}
}
printf("Case %d ",++t);
if(TF==)
{
printf("is not a tree.\n");
continue;
}
if(tree_num==) // 树的棵数是否为1
printf("is a tree.\n");
else
printf("is not a tree.\n");
}
return ;
}
下面给出dfs代码,其中用链表存放边
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
#define N 50005 int head[N];
struct Edge{
int v, next;
}edge[N*];
int Ecnt; void init()
{
Ecnt = ;
memset(head, -, sizeof(head));
} void add(int u, int v)
{
edge[Ecnt].v = v;
edge[Ecnt].next = head[u];
head[u] = Ecnt++;
} bool visited[N];
bool dfs(int u)
{
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if(visited[v]) return false;
visited[v] = ;
int res = dfs(v);
if(res == false) return false;
}
return true;
} struct Node {
int s, t;
}node[N]; map<int, int>mp; int in[N];
int main()
{
int s, t, cas = ;
while(~scanf("%d %d", &s, &t))
{
cas++;
if(s == && t == ){
printf("Case %d is a tree.\n", cas);
continue;
}
if(s < && t < ) break;
int c = ;
node[c].s = s, node[c++].t = t;
while(~scanf("%d %d", &s, &t), s||t)
node[c].s = s, node[c++].t = t; mp.clear();
int n = ;//结点总数
for(int i = ; i < c; i++)
{
s = node[i].s, t = node[i].t;
if(mp.find(s) == mp.end()) mp[s] = n++;
if(mp.find(t) == mp.end()) mp[t] = n++;
node[i].s = mp[s];
node[i].t = mp[t];
}
init();
memset(in, , sizeof(in));
for(int i = ; i < c; i++)
{
s = node[i].s, t = node[i].t;
add(s, t);
in[t]++;
}
bool ans = true;
int root = -;
for(int i = ; i < n; i++)
{
if(in[i] == )
{
if(root == -) root = i;
else ans = false;
}
}
if(ans == false || root == -)
{
printf("Case %d is not a tree.\n", cas);
ans = false;
continue;
}
memset(visited, , sizeof(visited));
visited[root] = ;
ans = dfs(root);
if(ans == false)
{
printf("Case %d is not a tree.\n", cas);
ans = false;
continue;
}
for(int i = ; i < n; i++)
if(!visited[i])
{
printf("Case %d is not a tree.\n", cas);
ans = false;
break;
}
if(ans)printf("Case %d is a tree.\n", cas);
}
}
Is It A Tree?(并查集)(dfs也可以解决)的更多相关文章
- F2 - Spanning Tree with One Fixed Degree - 并查集+DFS
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...
- 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS
原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- 树上统计treecnt(dsu on tree 并查集 正难则反)
题目链接 dalao们怎么都写的线段树合并啊.. dsu跑的好慢. \(Description\) 给定一棵\(n(n\leq 10^5)\)个点的树. 定义\(Tree[L,R]\)表示为了使得\( ...
- Codeforces_764_C. Timofey and a tree_(并查集)(dfs)
C. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 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 ...
随机推荐
- Run a task only once in (akka) cluster
在stackOverflow网站上看到这一提问,下文是部分摘抄问题简述: Java cluster, run task only once We have a java process, which ...
- DNS查询相关
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/45 一种简单的设计方式是在因特网上使用一个DNS服务器,该服务器 ...
- Git详解之八:Git与其他系统
Git 与其他系统 世界不是完美的.大多数时候,将所有接触到的项目全部转向 Git 是不可能的.有时我们不得不为某个项目使用其他的版本控制系统(VCS, Version Control System ...
- 导出生成xsl文件
public String expData() throws Exception{ List<SubArea> list = subAreaService.fin ...
- 项目实战10.1—企业级自动化运维工具应用实战-ansible
实战环境: 公司计划在年底做一次大型市场促销活动,全面冲刺下交易额,为明年的上市做准备.公司要求各业务组对年底大促做准备,运维部要求所有业务容量进行三倍的扩容,并搭建出多套环境可以共开发和测试人员做测 ...
- jdk8新特性(文章推荐)
文章推荐 jdk9都已经出来了,虽然很多项目都已经使用jdk8,但是很少会用到jdk8中的新特性.本人经常用的到也就是使用Stream,Lambda,但也仅仅是使用,基本不知道什么Function,C ...
- [Spark内核] 第31课:Spark资源调度分配内幕天机彻底解密:Driver在Cluster模式下的启动、两种不同的资源调度方式源码彻底解析、资源调度内幕总结
本課主題 Master 资源调度的源码鉴赏 [引言部份:你希望读者看完这篇博客后有那些启发.学到什么样的知识点] 更新中...... 资源调度管理 任务调度与资源是通过 DAGScheduler.Ta ...
- mysql加锁读
Locking Reads 在同一个事务中,如果你先查询数据,随后对相关数据进行插入或修改,那么在标准的SLELECT中不会给出足够的保护.在你查询期间另一个事务可以更新或者删除相同的行.InnoDB ...
- 自学Aruba1.4-WLAN厂家魔力象限
点击返回:自学Aruba之路 1.4 自学Aruba1.4-WLAN厂家魔力象限 以下为2017<有线和无线局域网接入基础设施的魔力象限>报告: Aruba.cisco为无线领域 ...
- Python如果导出失败,pass函数功能
由于服务器中有一些模块不存在,在文件中导入这些模块时提示错误,导致本地运行正常,服务器测试不通过. 此时,需要捕捉ImportError,当导入的包不存在时,pass掉定义的功能. 示例代码: try ...