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
input | output |
---|---|
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
***************************************************************************************
&& disjoint-set degree and calculating the corresponding root.
***************************************************************************************
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]; // calculate the corresponding degree of root 30 de [Y] = 0 ; 31 is } 32 } 33 is int main () 34 is { 35 the make (); 36 Memset (de, 0 , the sizeof (de)); 37 [ the while (Scanf ( " % D% D " , & A, & B) =! the EOF) 38 is { 39 UoN (A, B); 40 de [Find (B)] ++ ; 41 is } 42 is for (I = . 1; I < 10001 ; I ++ ) 43 is IF (de [I]% 2 ) // does not satisfy the condition is even 44 is { 45 COUT << ' 0 ' << endl; 46 is return 0 ; 47 } 48 COUT << ' . 1 ' << endl; 49 return 0 ; 50 51 is }
Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3259601.html