B
来源:互联网 发布:淘宝联盟是不是真的 编辑:程序博客网 时间:2024/04/30 14:17
A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties.
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.
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.
6 8 5 3 5 2 6 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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.
解题思路:本题与上一题相差甚微。只需要加一个判断被指几次的数组以及数清楚到底有几个根(数根:只用在遍历的时候查出有几个是pre[i]=i即可)
1.有且只有一个最上层的根节点,它不能被指向。
2.除此之外所有的节点都只能被指向一次(所以称作有向的图),因此需要再用一个 数组来记录被指向的次数。
3.依然采用节点数减去线条数等于1,来判断是否会形成回路
#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>using namespace std;#define MAXN 100010bool Set[MAXN];int pre[MAXN];int in[MAXN];bool flag=1;int Find(int x) //查 一层一层向上查找父亲{ int r=x; while(pre[x]!=r) { r=pre[x]; } return r;}void join(int x,int y) //并 把x的父亲设为y{ int fx=Find(x),fy=Find(y); if(fx!=fy) { pre[fx]=fy; } else //这里是比较创新的一个位置因为说明:如果有一个孤立的节点就直接No了 { flag=0;//围成一个圈 }}int main(){ //freopen("d:\\A.txt","r",stdin); int a,b; int cut=0;//记录有几个 非孤立的节点 int root_num = 0; int k=0; memset(Set,0,sizeof(Set)); memset(in,0,sizeof(in)); for(int i=1;i<=MAXN;i++) { pre[i]=i;//把所有的父亲都设为本身 } while(cin>>a>>b) { if(a==-1&&b==-1)break; if(a!=0||b!=0) { if(Set[a]==0) { cut++; Set[a]=1;//用过了 } if(Set[b]==0) { cut++; Set[b]=1; } if(a<b)//总是把大的数设为节点,就避免了重复 { join(a,b);//a被指向的次数加1 in[a]++; } else { join(b,a); in[b]++; } } else { k++; int n=0; //记录有几条边 for(int i=1;i<=MAXN;i++) { if(pre[i]!=i)//说明已经和别人建立联系的 方法 算出有几根线 { n++; } if(in[i]>1) { flag=0; } if(Set[i]==1&&(pre[i]=i)) { root_num++; } } for(int i=1;i<MAXN;i++) { pre[i]=i; Set[i]=0; //还原语句,因为不能再回去初始化了 in[i]=0; } //cout<<"n:"<<n<<"cut:"<<cut<<"flag"<<flag; if(flag && (n==(cut-1)||n==0&cut==0)&& root_num)cout<<"Case "<<k<<" is a tree."<<endl; else cout<<"Case "<<k<<" is not a tree."<<endl; flag=1; cut=0; root_num=0; } } return 0;}http://www.xukeyang.com/index.php/archives/35/
阅读全文
0 0
- b
- b
- b
- //b
- B
- b
- B
- B
- B
- B
- b
- B
- B
- B
- B
- B
- B
- b
- Android常用控件二
- 明日之子
- vue瀑布流组件滑动加载更多
- Jmeter使用
- Let the Balloon Rise(map+前向迭代器)
- B
- 浅析 Linux 初始化 init 系统,第 3 部分: Systemd
- Android Killer 反编译出现问题 正在反编译APK源码,请稍等 解决
- laravel+vue2.0终于搞定了!
- 【CJOJ2499】【DP合集】棋盘 chess
- AlertDialog不能显示问题,变暗不显示
- java WebService接口开发注意点 java 利用Xstream生成和解析xml知识结构
- Python几种比较常见的测试框架
- 11:图片起始位置