ZOJ 4124 + topological sort of thinking dfs

Happening - 4124  median

  Title effect: there are n elements, m is given to the relationship a> b, the elements of which may be asked of the (n + 1) / 2 elements, the elements may be a position corresponding to an output, the output 0 and vice versa

  Province, the last two in the past two weeks, but now I fill this title, this title is not difficult to feel, that time may be mixed up his mind, did not understand the meaning of the questions clearly . According to the topic is easy to see this with the relevant topological sorting, but the role of topological sort is to determine whether the relationship given contradictory, looking for judgment may be the first (n + 1) / 2 elements also mainly thinking.

  An intermediate element which may be (n + 1) / 2 elements, that is clearly in front of it that the number of elements (bigger than its) and the number of elements behind it (smaller than it) is not greater than (n-1) / 2 th elements than uncertain because the rest is larger or smaller than, its elements can be any up in front of or behind it. So we can use a dfs to deal with, as well as the number of elements of the latter is clear to any node in front of it. Any node on the right, the Bibi its smaller element of smaller elements is definitely smaller than it, so that specific operation, we dfs for each element x, then it can be inferred for each smaller than its but did not mark the elements y are updated, that is behind the elements x ++ and ++ y previous element, and finally mark x is greater than y.

  For details, see the code, because not many sides, so I direct the vector before useless to star.

 1 #include<cstdio>
 2 #include<vector>
 3 #include<queue>
 4 using namespace std;
 5 const int N=118;
 6 vector<int> vv[N];
 7 bool big[N][N];//big[i][j]就标记i是否比j大
 8 int du[N],vis[N],pre[N],back[N];
 9 void init(int n)
10 {
11     for(int i=0;i<=n;i++)
12     {
13         du[i]=0;
14         vis[i]=0;
15         pre[i]=back[i]=0;
16         vv[i].clear();
17         for(int j=0;j<=n;j++)
18             big[i][j]=false;
19     }
20 }
21 bool tp(int n)
22 {
23     queue<int> q;
24     for(int i=1;i<=n;i++)
25         if(du[i]<=0&&!vis[i])
26         {
27             vis[i]=1;
28             q.push(i); 
29         }
30     int sum=0,x,y;
31     while(!q.empty())
32     {
33         x=q.front();
34         q.pop();
35         sum++;
36         for(int i=0;i<vv[x].size();i++)
37         {
38             y=vv[x][i];
39             du[y]--;
40             if(du[y]<=0&&!vis[y])
41             {
42                 vis[y]=1;
43                 q.push(y); 
44             }
45         }
46     }
47     return sum==n;
48 }
49 void dfs(int u,int f)
50 {
51     for(int= I 0 ; I <VV [U] .size (); I ++ )
 52 is      {
 53 is          int V = VV [U] [I];
 54 is          IF ! (Big [F] [V]) // F> V but unlabeled, it can be updated 
55          {
 56 is              Big [f] [V] = to true ;
 57 is              pre [V] ++; // the number of elements of the front ++ V 
58              ; Back [f] ++ // later f the number of elements - 
59              DFS (V, F);
 60          }
 61 is      }
 62 is  }
 63 is  int main ()
 64  {
 65      int t,n,m,u,v;
66     scanf("%d",&t);
67     while(t--)
68     {
69         scanf("%d%d",&n,&m);
70         init(n);
71         while(m--)
72         {
73             scanf("%d%d",&u,&v);
74             du[v]++;
75             vv[u].push_back(v);
76         }
77         if(!tp(n))// whether to award a topological sort of relationship conflicts 
78          {
 79              for ( int I = . 1 ; I <= n-; I ++ )
 80                  the putchar ( ' 0 ' );
 81              the puts ( "" );
 82          }
 83          the else 
84          {
 85              for ( int I = . 1 ; I <= n-; I ++ )
 86                  DFS (I, I);
 87              for ( int I = . 1 ; I <= n-; I ++ )
 88                  IF(pre[i]<=(n-1)/2&&back[i]<=(n-1)/2)
89                     putchar('1');
90                 else
91                     putchar('0');
92             puts("");
93         }
94     }
95     return 0;
96 }
Layer by layer topology of your heart

 

Guess you like

Origin www.cnblogs.com/LMCC1108/p/10935292.html
Recommended