Coloring Edges sentenced ring topology []

Topic links: https://vjudge.net/contest/330119#problem/A

Subject to the effect:

1 gives a directed graph, FIG painted to the request in the same side of the ring are not all the same color. Q. How much color requires a minimum, and the output of each side painted.

Problem-solving ideas:

1. Multi-painting found a few pictures, the kind of color will only be 1 or 2. When present in the ring, the entire coating 1. When the presence of a ring, the ring may be divided into two sides (small big node node pointing coating 1, coating large node pointing to 2 small cell), you will find all of the rings will not all be the same color.

2. 1 found thought about this question only needs to determine whether the ring can exist. Analyzing the topology can be used. Principle is: in the process of topology, the 0-degree point will be into the team, but because of the points on the ring can not be impossible to 0. into the team. So at the end of the topology, there is no point into the team, that is present in the ring.

 

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<queue>
  4 #define mem(a, b) memset(a, b, sizeof(a))
  5 const int MAXN = 5100;
  6 const int MAXM = 5100;
  7 using namespace std;
  8 
  9 int n, m;
 10 int head[MAXN], cnt, in[MAXN], out[MAXN], tot;
 11 queue<int> Q;
 12 
 13 struct Edge
 14 {
 15     int from, to, next;
 16 }edge[MAXM];
 17 
 18 void add(int a, int b)
 19 {
 20     cnt ++;
 21     edge[cnt].from = a;
 22     edge[cnt].to = b;
 23     edge[cnt].next = head[a];
 24     head[a] = cnt;
 25 }
 26 
 27 int topo()
 28 {
 29     for(int i = 1; i <= n; i ++)
 30     {
 31         if(!in[i])
 32         {
 33             Q.push(i);
 34             tot ++;
 35         }
 36     }    
 37     while(!Q.empty())
 38     {
 39         int temp = Q.front();
 40         Q.pop();
 41         for(int i = head[temp]; i != -1; i = edge[i].next)
 42         {
 43 is              int to = Edge [I] .to;
 44 is              in [to] - ;
 45              IF (! In [to])
 46 is              {
 47                  Q.push (to);
 48                  TOT ++ ;
 49              }
 50          }
 51 is      }
 52 is      IF (! = n-TOT) // point of presence is not queued 
53 is          return  . 1 ;
 54 is      the else 
55          return  0 ;
 56 is  }
 57 is  
58 int main()
 59 {
 60     scanf("%d%d", &n, &m);
 61     mem(head, -1);
 62     for(int i = 1; i <= m; i ++)
 63     {
 64         int a, b;
 65         scanf("%d%d", &a, &b);
 66         in[b] ++, out[a] ++;
 67         add(a, b); 
 68     }
 69     if(topo()) // judged whether there ring 
70      {
 71 is          the printf ( " 2 \ n- " );
 72          int In Flag = . 1 ;
 73 is          for ( int I = . 1 ; I <= CNT; I ++ )
 74          {
 75              int A = Edge [ . I] from , B = Edge [I] .to;
 76              IF (In Flag)
 77              {
 78                  IF (A < B)
 79                      the printf ( " . 1 " );
 80                  the else
 81                     printf("2");
 82                 flag = 0;
 83             }
 84             else
 85             {
 86                 if(a < b)
 87                     printf(" 1");
 88                 else
 89                     printf(" 2");
 90             }
 91         }
 92         printf("\n");
 93     }
 94     else
 95     {
 96         printf("1\n1");
 97         for(int i = 1; i < m; i ++)
 98             printf(" 1");
 99         printf("\n");
100     }
101     return 0;
102 }
View Code

 

Guess you like

Origin www.cnblogs.com/yuanweidao/p/11612282.html