Codeforces Round #589 (Div. 2) D. Complete Tripartite

Topic links: https://codeforces.com/contest/1228/problem/D

Topic effect: give a n points undirected graph, Loop-free, does not guarantee Unicom, asked whether energy-third of this figure. For each set is required, the interior of the set of points, there can be connected between any two sides, between the point needs to have different sets of linked edge. If this FIG third, output scheme; if not, the output of -1.

answer:

FIG determines whether the first link, if the link is not output directly -1.

The graph coloring.

Specifically, first of all the points of a dye, from a first traversing this point of view (assuming traverse to point u) If there is an edge between u and v and the same color, then a change v color (i.e., color [v] ++); if there is an edge between u and v and different colors, continue.

After staining staining protocol to determine whether or not legal.

Specifically, if the color. 1 2 || || Color 3 has a color set to 0, indicating that the program is not valid, the output of -1.

Analyzing the elements in each set is equal to the degree (n - set size). If there is a point does not satisfy the degree = (n - set size), indicating that the program is not feasible, the output of -1.

For example, for a sample of the subject, the second set of points belonging to II, the size of the second set is 2, and II points need beyond the second set point has all sides, so II degree point must be equal to 6--2 = 4 will be legitimate.

If the above is not legal does not occur, the output program.

 1 #include<iostream>
 2 #include<vector>
 3 #include<map>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstdio>
 7 #include<cmath>
 8 #include<string>
 9 #include<set>
10 #include<complex>
11 #include<cstdio>
12 #include<cstring>
13 #include<stack>
14 #include<iomanip>
15 #include<bitset>
16 #include<typeinfo>
17 #include<random>
18 #include<unordered_map>
19 #include<unordered_set>
20 using namespace std;
21 
22 const int maxn = 3e5 + 10;
23 
24 int cnt;
25 int n, m;
26 int x, y;
27 vector<int> G[maxn];
28 int vis[maxn], color[maxn], deg[maxn];
29 
30 void dfs(int x){
31     vis[x] = cnt;
32     for(int i = 0; i < G[x].size(); i++){
33         int y = G[x][i];
34         if(vis[y]) continue;
35         dfs(y);
36     }
37 }
38 
39 int main(){
40     scanf("%d %d", &n, &m);
41     for(int i = 1; i <= m; i++){
42         scanf("%d %d", &x, &y);
43         G[x].push_back(y);
44         G [Y] .push_back (X);
 45          deg [X] ++; // degrees 
46 is          deg [Y] ++; // degrees 
47      }
 48      for ( int I = . 1 ; I <= n-; I ++) { // Analyzing Unicom 
49          IF (! VIS [I]) {
 50              CNT ++ ;
 51 is              DFS (I);
 52 is          }
 53 is      }
 54 is      IF (CNT> = 2 ) { // does Unicom, illegal 
55          the printf ( " -1 \ n- " );
56 is          return  0 ;
 57 is      }
 58      for ( int I = . 1 ; I <= n-; I ++) Color [I] = . 1 ;
 59      for ( int I = . 1 ; I <= n-; I ++ ) {
 60          for ( int J = 0 ; J <G [I] .size (); J ++ ) {
 61 is              int V = G [I] [J];
 62 is              IF ! (Color [I] = Color [V]) Continue ; // points belonging to different set 
63 is              the else color [v] ++; // two points in the same set, to change a color point v
64         } 
65     }
66     int cnt_one = 0, cnt_two = 0, cnt_three = 0; // 集合1 2 3大小
67     for(int i = 1; i <= n; i++){
68         if(color[i] == 1) cnt_one++;
69         else if(color[i] == 2) cnt_two++;
70         else cnt_three++;
71     }
72     if(cnt_one == 0 || cnt_two == 0 || cnt_three == 0 ) { // if 0 is set, illegitimate 
73 is          the printf ( " -1 \ n- " );
 74          return  0 ;
 75      }
 76      for ( int I = . 1 ; I <= n-; I ++ ) { // determines whether a point set to meet the requirements of the degree 
77          iF (Color [I] == . 1 && deg [I] n-= -! cnt_one) {
 78              the printf ( " -1 \ n- " );
 79              return  0 ;
 80          }
 81          the else if(color[i] == 2 && deg[i] != n - cnt_two){
82             printf("-1\n");
83             return 0;
84         }
85         else if(color[i] == 3 && deg[i] != n - cnt_three){
86             printf("-1\n");
87             return 0;
88         }
89     }
90     for(int i = 1; i <= n; i++){ //Output scheme 
91 is          the printf ( " % D " , Color [I]);
 92      }
 93      the printf ( " \ n- " );
 94      return  0 ;
 95 }
View Code

 

Guess you like

Origin www.cnblogs.com/mrzhangziheng/p/11611078.html