Luo Gu P2341 popular cattle (tarjan strongly connected component template)

Topic links: Luo Gu P2341 popular cattle

The meaning of problems

Each cow dreams of becoming a star in the bullpen. Like all cows are cows cow is a star. All cows are the narcissists per cow always like to own. Between cow "like" can pass - if A like B, B like C, the A like C. A total of N cowshed cows given love relationship between the number of cows, you have to calculate how many cows can become a star.


Thinking

Can be obtained from the title, the popular dairy cow may be the only all strongly connected subgraph only out of the zero, so that if there are two or more degrees of strongly connected subgraph 0 star no presence of cows because that love of a plurality of sub-picture is equal to zero can not pass out. The only sub-graph that can be subject to other sub-loved figure at the same time can pass each other in the sub-graph, so all the cows in the sub-graph are the stars.


Code

#include <the iostream> 
#include <cstdio> 
#include <CString> 
#include <Stack>
 the using STD :: min;
 the using STD :: Stack;
 const  int N = 10010 , M = 50010 ;
 struct Edge 
{ 
    int to, NEX; 
Edge} [M]; 
int num_e;
 int head [N], du [N];
 // SCC: SCC [u] represents a strong link subgraph where the number u; SZ [i] denotes the i-th sub FIG strong Unicom scale; idx: dfs timestamp; tot: count the number of strong link subgraph; 
int DFN [N], Low [N], SCC [N], SZ [N], IDX, TOT;
 BOOL Insta [N];
 int STA [N], Top;
 void init() {
    num_e = 0, top = 0, idx = 0, tot = 0;
    memset(head, 0, sizeof(head));
    memset(du, 0, sizeof(du));
    memset(insta, 0, sizeof(insta));
    memset(scc, 0, sizeof(scc));
    memset(dfn, 0, sizeof(dfn));
}
void add_edge(int x, int y) {
    edge[++num_e].to = y;
    edge[num_e].nex = head[x];
    head[x] = num_e;
}
void tarjan(int u) {  // tarjan求强联通分量模板
    dfn[u] = low[u] = ++idx;
    sta[++top] = u;
    insta[u] = true;
    for (int i = head[u]; i; i = edge[i].nex) {
        int v = edge[i].to;
        if (!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (insta[v]) low[u] = min(low[u], dfn[v]);
    }
    if (dfn[u] == low[u]) {
        ++tot;
        do {
            scc[sta[top]] = tot;
            sz[tot]++;
            insta[sta[top]] = false;
        } while (sta[top--] != u);
    }
}

int main() {
    int n, m;
    while (~scanf("%d %d", &n, &m)) {
        init();
        for (int i = 0, u, v; i < m; i++) {
            scanf("%d %d", &u, &v);
            add_edge(u, v);
        }
        for (int i = 1; i <= n; i++) {
            if (!dfn[i]) tarjan(i);
        }
        for (int u = 1; u <= n; u++) {
            for (int i = head[u]; i; i = edge[i].nex) {
                int v = edge[i].to;
                if (scc[u] != scc[v]) du[scc[u]]++;
            }
        }
        int ans = 0;
        for (int i = 1; i <= tot; i++) {
            if (!du[i]) {
                if (ans) {
                    ans = 0;
                    break;
                }
                ans = i;
            }
        }
        printf("%d\n", ans ? sz[ans] : 0);
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/kangkang-/p/11360662.html