九度题目1481:Is It A Tree?
来源:互联网 发布:查看淘宝店铺排名 编辑:程序博客网 时间:2024/06/06 17:44
题意:
判断是不是树的条件如下
1、只有一个根节点,根节点的入度为0
2、每个点只有一条边指向该节点
3、从根节点开始只有一条边指向该节点
输入输出格式
每一个case以 0 0 结束
-1 -1 结束所有输入样例
竟然还有判断有没有环,疏忽了。
#include<stdio.h>#include<iostream>#include<vector>#include<string.h>#include<queue>#include<set>using namespace std;vector<int>G[100005];vector<int>::iterator ite;queue<int>q;set<int>s;int fr,to;int maxn;int cases;int n;bool vis[100005];int degree[100005];bool isTree(){ while(!q.empty()) q.pop(); for(int i = 1;i<=maxn;i++) { if(vis[i]) { if(degree[i]==0) q.push(i); else if(degree[i]>1) return false; } } if(q.size()>1) return false; int cnt = 0; while(!q.empty()) { int tmp = q.front(); q.pop(); cnt++; for(ite = G[tmp].begin();ite!=G[tmp].end();ite++) { degree[*ite]--; if(degree[*ite]==0) q.push(*ite); } } if(cnt==n) return true; else return false;}int main(){ cases = 0; while(~scanf("%d%d",&fr,&to)) { if(fr == -1&&to==-1) break; if(fr!=0&&to!=0) { maxn =max(maxn,fr); maxn =max(maxn,to); //if(!G[to].find(fr)) G[fr].push_back(to); degree[to]++; if(!vis[fr]) { vis[fr] = 1; n++; } if(!vis[to]) { vis[to] = 1; n++; } } else if(fr==0&&to==0) { cases++; int flag = 1; int rot = 0; // n=s.size();// for(int i = 1 ; i <=maxn;i++)// {// if(vis[i]==1)// {// degree[i] = G[i].size();// // cout<<i<<endl;// if(G[i].size()>1)// flag = 0;// else if(G[i].size()==0)// {//// if(rot)// flag = 0;// else// rot = 1;// }// }////// } // cout<<flag<<" "<<rot<<endl; if(isTree()) cout<<"Case "<<cases<<" is a tree."<<endl; else cout<<"Case "<<cases<<" is not a tree."<<endl; for(int i = 1 ; i <=maxn;i++) { degree[i] = 0; G[i].clear(); } memset(vis,0,sizeof(vis)); n = 0; } } return 0;}/*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 03 8 6 8 6 45 3 5 6 5 2 9 1 0 0*/
0 0
- 九度题目1481:Is It A Tree?
- 九度 oj 题目1481:Is It A Tree?
- 题目1481:Is It A Tree?
- 题目1481:Is It A Tree?
- 题目1481:Is It A Tree?
- 题目1481:Is It A Tree?
- Is It A Tree?,判断是否是一棵树。(题目来源:九度OJ 1481,2012年北京大学计算机研究生机试真题)
- Is It A Tree?
- Is it a tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- Is It A Tree?
- IS IT A tree
- 【牛腩新闻发布系统之handler】
- 如何在C++中调用C程序?
- android基础知识点
- Java 序列化
- 路由匹配顺序
- 九度题目1481:Is It A Tree?
- The Same Calendar
- css选择器
- Ipad12.9寸上传的屏幕尺寸
- scala进阶32-用for实现map、flattop、filter
- Merge Two Sorted Lists
- Rserve安装、配置和远程操控
- 自己写bootloader笔记1---流程分析
- 45. Jump Game II 向前跳