I - Circle
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld
& %llu
Submit Status Practice ZOJ
3321
Description
Your task is so easy. I will give you an undirected graph, and you just need to tell me whether the graph is just a circle. A cycle is three or more nodes V1, V2, V3,
... Vk, such that there are edges between V1 and V2, V2 and V3, ... Vk and V1,
with no other extra edges. The graph will not contain self-loop. Furthermore, there is at most one edge between two nodes.
Input
There are multiple cases (no more than 10).
The first line contains two integers n and m, which indicate the number of nodes and the number of edges (1 < n < 10, 1 <= m <
20).
Following are m lines, each contains two integers x and y (1 <= x, y <= n, x != y),
which means there is an edge between node x and nodey.
There is a blank line between cases.
Output
If the graph is just a circle, output "YES", otherwise output "NO".
Sample Input
3 3 1 2 2 3 1 3 4 4 1 2 2 3 3 1 1 4
Sample Output
YES NO
给你一幅无向图,让你判断是不是圆。。
成圆条件有两个。
第一:每一个点的入度只能为2.
第二:所有点属于一个集合。
上代码;
#include <stdio.h> #include <cstring> #include <algorithm> using namespace std; #include <iostream> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <time.h> #include <stdlib.h> int p[26]; int v[26]; int find(int x) { if(p[x]!=x) p[x]=find(p[x]); return p[x]; } int hebing(int x,int y) { return p[x]=y; } int main() { int n,m,i,j,a,b; while(cin>>n>>m) { memset(v,0,sizeof(v)); for(i=1; i<=25; i++) p[i]=i; while(m--) { cin>>a>>b; v[a]++; //入度 v[b]++; a=find(a); b=find(b); if(a!=b) hebing(a,b); //合并、 } for(i=1; i<=n; i++) { if(p[1]!=p[i]|| v[i]!=2) break; } // cout<<i<<endl; if(i==n+1) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }