Circle ZOj3321

/*不知为何比赛的时候就是做不出来,思路真的很清晰地。求一个回路,没有多余的边
Circle

--------------------------------------------------------------------------------

Time Limit: 1 Second      Memory Limit: 32768 KB

--------------------------------------------------------------------------------

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 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 3
1 2
2 3
1 3

4 4
1 2
2 3
3 1
1 4

Sample Output

YES
NO

*/
#include<iostream>
#include<cstdio>
using namespace std;
int num[21];
int root[21];
int map[21];
int find(int a)
{
 int r = a;
 while(num[r] != r)
  r = num[r];
 return r;
}

void mergy(int a, int b)
{
 if(a > b)
  num[a] = b;
 else
  num[b] = a;
}

int main()
{
 int n, m;
 while(cin>>n>>m)
 {
  int i;
  int x, y;
  for(i = 1; i <= n; i++)
  {
   num[i] = i;
   root[i] = 0;
   map[i] = 2;
  }
  int flag = 0;
  int flag1 = 0;
  int a, b, t;
  int j;
  for(i = 1; i <= m; i++)
  {
   cin>>x>>y;
   root[x] = 1;
   root[y] = 1;
   map[x]--;
   map[y]--;
   a = find(x);
   b = find(y);
   
   if(a != b)
    mergy(a, b);
   else
   { 
    flag1++;
    
   }
   if(flag1 == 1)
   for(j = 1; j <= n; j++)
   if( map[j] < 0)
   {
    flag = 1;
    break;
   }
   //if(flag == 1)
   //{
    //for(j = i + 1; j<= m; j++)//一开始这里变量弄错了,测试数据都过不了
     //cin>>x>>y;
     //puts("NO");
     //break;
   //}

   
  }
  
  if(flag1 == 1 && flag == 0)
   puts("YES");
  else
   puts("NO");
  

 }
}
  

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值