1320. Graph Decomposition

1320. Graph Decomposition

Time limit: 0.5 second Memory limit: 64 MB
There is a simple graph with an even number of edges. You are to define if it is possible to present it by the set of pairs of adjacent edges (having a common vertex).

Input

contains a sequence of the numbers pairs. Each pair denotes vertices identifiers of one edge. All the identifiers are integers from 1 to 1000. You may assume that there are no loops and multiple edges in the graph defined by the input data.

Output

“1” (without quotation marks), if the decomposition is possible and “0” otherwise.

Samples

inputoutput
1 2
2 3
3 1
1 10
1
1 2
2 3
3 1
4 10
0
Problem Author: Idea: Alexander Petrov, prepared by Alexander Petrov, Leonid Volkov Problem Source: VIII Collegiate Students Urals Programming Contest. Yekaterinburg, March 11-16, 2004
***************************************************************************************
并查集&&计算相应根节点的度数和。
***************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<cstdio>
 6 using namespace std;
 7 int fa[10001];
 8 int de[10001];
 9 int a,b,i,j,k;
10 void make()
11 {
12     for(i=1;i<=10001;i++)
13      fa[i]=i;
14 }
15 int find(int x)//并查集
16  {
17      if(fa[x]!=x)
18       fa[x]=find(fa[x]);
19      if(fa[x]==x)
20       return fa[x];
21 }
22 void uon(int x,int y)
23  {
24      x=find(fa[x]);
25      y=find(fa[y]);
26      if(x!=y)
27       {
28           fa[y]=x;
29           de[x]+=de[y];//计算相应根节点的度数
30           de[y]=0;
31       }
32  }
33 int main()
34 {
35     make();
36     memset(de,0,sizeof(de));
37     while(scanf("%d %d",&a,&b)!=EOF)
38      {
39          uon(a,b);
40          de[find(b)]++;
41      }
42      for(i=1;i<10001;i++)
43       if(de[i]%2)//不为偶数不满足条件
44        {
45            cout<<'0'<<endl;
46            return 0;
47        }
48      cout<<'1'<<endl;
49      return 0;
50 
51 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3259601.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值