ZOJ 4124 Median (topology DFS)

Topic links: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=6026

Determining whether there is a ring topological sort, DFS search 1 ~ n greater than / less than the number of element i

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#define eps 1e-8
using namespace std;
typedef long long ll;
static const int MAX_N = 1e2 + 5;
static const ll Mod = 233;
static const int INF = 0x3f3f3f3f;
vector<int>veh[MAX_N];
vector<int>vel[MAX_N];
int in[MAX_N], cnth[MAX_N], cntl[MAX_N];
bool vis[MAX_N];
int n;
bool topolog(){
    queue<int>q;
    for(int i = 1; i <= n; ++i) if(!in[i]) q.push(i);
    while(!q.empty()){
        int now = q.front();
        q.pop();
        if(!vis[now]){
            vis[now] = true;
            for(int i = 0; i < vel[now].size(); ++i){
                int v = vel[now][i];
                if(--in[v] == 0) q.push(v);
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        if(!vis[i]) return false;
    }
    return to true ; 
} 
void dfs ( int U, int & Rev, BOOL FG) {     // Here dfs be improved, better vector parameter passing, without ... 
    VIS [U] = to true ;
     IF (FG) {
          for ( int = I 0 ; I <Vel [U] .size (); ++ I) {
             int V = Vel [U] [I];
             IF (! VIS [V]) {
                 ++ Rev; 
                VIS [V] = to true ; 
                DFS (V, Rev, FG); 
            } 
        } 
    } 
    the else{
        for(int i = 0; i < veh[u].size(); ++i){
            int v = veh[u][i];
            if(!vis[v]){
                ++rev;
                dfs(v, rev, fg);
            }
        }
    }
}
int main(){
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int T;
    scanf("%d", &T);
    while(T--){
        int m;
        scanf("%d%d", &n, &m);
        for(int i = 0; i <= n; ++i){
            vel[i].clear();
            veh[i].clear();
            vis[i] = false;
            in[i] = 0;
            cnth[i] = 0;
            cntl[i] = 0;
        }
        for(int i = 0; i < m; ++i){
            int u, v;
            scanf("%d%d", &u, &v);
            vel[u].push_back(v);
            veh[v].push_back(u);
            ++in[v];
        }
        if(!topolog()) for(int i = 1; i <= n; ++i) putchar('0');
        else{
            bool fg = true;
            for(int i = 1; i <= n; ++i){
                memset(vis, false, sizeof(vis));
                int rev = 0;
                dfs(i, rev, fg);
                cntl[i] = rev;
            }
            fg = false;
            for(int i = 1; i <= n; ++i){
                memset(vis, false, sizeof(vis));
                int rev = 0;
                dfs(i, rev, fg);
                cnth[i] = rev;
            }
            int mv = (n - 1) >> 1;
            for(int i = 1; i <= n; ++i){
                putchar(cntl[i] <= mv && cnth[i] <= mv ? '1' : '0');
            }
        }
        putchar('\n');
    }
}
View Code

 

Guess you like

Origin www.cnblogs.com/xorxor/p/10958954.html
ZOJ
ZOJ