九度OJ-1109:连通图
来源:互联网 发布:淘宝卖家怎么注册帐号 编辑:程序博客网 时间:2024/05/22 18:49
本题与“1012-畅通工程”完全相同,就是求连通分量个数。不赘述。
- 题目描述:
给定一个无向图和其中的所有边,判断这个图是否所有顶点都是连通的。
- 输入:
每组数据的第一行是两个整数 n 和 m(0<=n<=1000)。n 表示图的顶点数目,m 表示图中边的数目。如果 n 为 0 表示输入结束。随后有 m 行数据,每行有两个值 x 和 y(0<x, y <=n),表示顶点 x 和 y 相连,顶点的编号从 1 开始计算。输入不保证这些边是否重复。
- 输出:
对于每组输入数据,如果所有顶点都是连通的,输出"YES",否则输出"NO"。
- 样例输入:
4 31 22 33 23 21 22 30 0
- 样例输出:
NOYES
- 来源:
- 2011年吉林大学计算机研究生机试真题
- 答疑:
- 解题遇到问题?分享解题心得?讨论本题请访问:http://t.jobdu.com/thread-7832-1-1.html
#include <iostream> #define MAXSIZE 1000using namespace std;struct UFS{//UnionFindSet ´Ó0¿ªÊ¼ int elem[MAXSIZE];int size;void initiate(int size){this->size=size;for (int i=0;i<size;i++)elem[i]=-1;}int findRoot(int x){//reduce deep if (elem[x]==-1) return x;else return elem[x]=findRoot(elem[x]);}void unionSet(int x,int y){int xroot=findRoot(x),yroot=findRoot(y);if (xroot!=yroot)//if x and y are from different set elem[xroot]=yroot; //merge xset into yset }};int main(){int n,m;int a,b;int count;UFS ufs;while (cin>>n>>m,n){//initiateufs.initiate(n);count=0;//input&&unionfor (int i=0;i<m;i++){cin>>a>>b;a-=1;b-=1;ufs.unionSet(a,b);}for (int i=0;i<ufs.size;i++){if (ufs.elem[i]==-1)count++;} if (count==1)cout<<"YES"<<endl;elsecout<<"NO"<<endl;}return true;}
0 0
- 九度oj 题目1109:连通图
- 九度oj-1109-连通图
- 九度OJ - 1109 - 连通图
- 九度OJ 1109 连通图
- 九度OJ-1109:连通图
- 题目1109:连通图 九度OJ
- 九度OJ题目1109:连通图
- 九度OJ 1109:连通图 (最小生成树)
- 【九度OJ】题目1109:连通图 解题报告
- 九度OJ——1109连通图
- 九度1109:连通图
- 九度oj 1545奇怪的连通图
- 九度OJ 1545 奇怪的连通图
- <九度 OJ>题目1545:奇怪的连通图
- 九度 题目1109:连通图
- 九度题目1109:连通图
- 九度 题目1109:连通图
- 九度 题目1109:连通图
- Java 网络 URL 从网页上获取数据
- 与JavaWeb开发相关的一些名词解释
- android使用高德地图SDK获取定位信息
- POJ - 2385 Apple Catching解题报告
- Java中的IO流
- 九度OJ-1109:连通图
- Codeforces Round #396 (Div. 2)D. Mahmoud and a Dictionary(带权并查集)
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [] associated with con
- 主成分分析-PCA
- Leetcode 97 - Interleaving String(dp)
- 运算电路基础
- 虚拟机ping不通主机,但是主机可以ping通虚拟机 ——linux笔记(1)
- 九度OJ-1445:How Many Tables
- Codeforces Round #321 (Div. 2)E 线段树+字符串hash