Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3731
Your task is so easy. I'll give 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 is edges between V1 and V2, V2 and V3, ... Vk and V1, with no other extra edges. The graph would not be contain self-loop. Furthermore, there is at the most one edge between and nodes.
Input
There is multiple cases (no more than 10).
The first line contains integers n and m, which indicate the number of nodes and the number of edges (1 < n <, 1 <= m < 20).
Following is m lines, each contains, integers x and y (1 <= x, y <= n, x ! = y), which means there is an edge between node x and Node y.
There is a blank line between cases.
Output
If the graph is just a circle, output "YES", otherwise output "NO".
Sample Input
3 31 22 31 34 41 22 33 11 4
Sample Output
YESNO
Author:LIN, Yue
Source:The 10th Zhejiang University Programming Contest
Test instructions
All the sides given can just form a circle!
Code One:
#include <cstdio> #include <cstring>int f[147];int ma[147];int Find (int x) {return x==f[x]? X:f[x]=find (f[x ]);} void init (int n) {for (int i = 0; I <= N; i++) {f[i] = i; }}void Union (int x, int y) {int f1 = find (x); int F2 = find (y); if (f1! = F2) {F[f2] = F1; }}int Main () {int n, m; while (~SCANF ("%d%d", &n,&m)) {memset (MA, 0,sizeof (MA)); int A, B; Init (n); for (int i = 0; i < m; i++) {scanf ("%d%d", &a,&b); Union (A, b); ma[a]++, ma[b]++; } int flag = 0; for (int i = 1; I <= n; i++) {if (ma[i]! = 2)//each point can only appear two times {flag = 1; Break } if (F[i]! = F[n]) {flag = 1; Break }} if (flag) {printf ("no\n"); } else {printf ("yes\n"); }} return 0;}
Code two:
#include <cstdio> #include <cstring>int main () {int n, m; int map[25][25]; while (scanf ("%d%d", &n, &m)!=eof) {int A, B; memset (map, 0, sizeof (map)); for (int i = 0; i < m; i++) {scanf ("%d%d", &a, &b); MAP[A][B] = Map[b][a] = 1; } if (n! = m) {printf ("no\n"); Continue } int ans = 0; int flag = 0; int tt = 1; for (int i = 1; I <= m; i++) {flag = 0; for (int j = 1; J <= N; j + +)//start loop from point 1, if last can go back to point 1, it is a circle if (Map[tt][j]) { MAP[TT][J] = Map[j][tt] = 0;//mark the found edge TT = j; flag = 1; Break } if (flag = = 0)//not found {ans =-1; Break }} if (tt! = 1) ans =-1; if (ans = = 0) printf ("yes\n"); if (ans = =-1) printf ("no\n"); } return 0;}
ZOJ 3321 Circle (and look at the collection AH)