九度王道考研机试真题 2011-5吉大1109:连通图
来源:互联网 发布:webclient没执行js 编辑:程序博客网 时间:2024/05/16 11:52
//题目1109:连通图
#include<iostream>
using namespace std;
int Tree[1010];
int findRoot(int x)
{
if(Tree[x]==-1) return x;
else
{
int tmp=findRoot(Tree[x]);
Tree[x]=tmp;
return tmp;
}
}
int main()
{
int n,m,x,y;
while(cin>>n&&n!=0)
{
int ans=0;
for(int i=1;i<=1000;i++) Tree[i]=-1;
cin>>m;
while(m--)
{
cin>>x>>y;
x=findRoot(x);
y=findRoot(y);
if(x!=y)
{
Tree[x]=y;
}
}
for(int i=1;i<=n;i++)
{
if(Tree[i]==-1) ans++;
}
if(ans==1)
{
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}
}
#include<iostream>
using namespace std;
int Tree[1010];
int findRoot(int x)
{
if(Tree[x]==-1) return x;
else
{
int tmp=findRoot(Tree[x]);
Tree[x]=tmp;
return tmp;
}
}
int main()
{
int n,m,x,y;
while(cin>>n&&n!=0)
{
int ans=0;
for(int i=1;i<=1000;i++) Tree[i]=-1;
cin>>m;
while(m--)
{
cin>>x>>y;
x=findRoot(x);
y=findRoot(y);
if(x!=y)
{
Tree[x]=y;
}
}
for(int i=1;i<=n;i++)
{
if(Tree[i]==-1) ans++;
}
if(ans==1)
{
cout<<"YES"<<endl;
}
else cout<<"NO"<<endl;
}
}
0 0
- 九度王道考研机试真题 2011-5吉大1109:连通图
- 九度王道考研机试真题 2011-2北大题目1156:谁是你的潜在朋友 hash
- 九度王道考研机试真题 2011-3哈工 题目1052:找x 查找
- 九度王道考研机试真题 2010-2北邮 题目1173:查找.cpp
- 九度王道考研机试真题 2010-哈工1054:字符串内排序
- 九度王道考研机试真题 2012-北大Jungle Roads 并查集 最小生成树
- 九度1109:连通图
- 九度-王道1384
- 【九度】2014年王道论坛计算机考研机试全真模拟考试解题报告
- 九度王道考研机试 2009-2哈工大1048:判断三角形类型
- 九度oj 题目1109:连通图
- 九度 题目1109:连通图
- 九度题目1109:连通图
- 九度 题目1109:连通图
- 九度oj-1109-连通图
- 九度OJ - 1109 - 连通图
- 九度OJ 1109 连通图
- 九度OJ-1109:连通图
- [转] git使用初涉
- Hibernate4.3.6+struts2 2.3.16+Spring4.1整合
- iOS:字符串过滤掉非法字符
- Set, List And Map
- SPRING中的线程池ThreadPoolTaskExecutor
- 九度王道考研机试真题 2011-5吉大1109:连通图
- CSS3中-moz、-ms、-webkit和-o分别代表什么意思
- if和switch语句、循环语句while和do……while、for
- cocos2dx之EditBox
- firebird中删除多列重复的数据
- 栈与队列
- fragment中加入tabhost以及修改TabWidget
- SQL注入
- UIWebView 使用详解