Poj 2186 Popular cows (tarjan)

Source: Internet
Author: User

At the beginning of this question, I ignored the case that if it is not connected, the join inbound judgment made the inbound judgment not 0, but the outbound judgment 1, but if it is not connected, it is wrong. If there is no edge between two scc, the inbound degree is 0, and the outbound degree is also 0. If the scc block outbound degree is 0, if the whole figure is not an scc, the scc block entry level will not be 0, and there is no need to judge it!

The topic is to find the most popular cows. A cow is the most popular only when it is recognized by all other cows. All this question is the number of points that can be reached by any point. The connectivity problem occurs between the centers of one scc block. The problem is that it can be reached by all other scc blocks. When a node in the DAG can be reached by all nodes, the DAG has a directed acyclic graph, obviously there is no ring, there is no scc in the scaled-down graph. To reach this point, you must have an edge pointing to this point only or indirectly, if the point is indirectly directed to this point, the outbound degree is not 0, and the direct outbound degree is not 0. At the same time, we can see that if this point causes the point to be directed, it cannot point to any other point, because if it points to any other point and it is a point that can be reached by all other points, it forms a ring, this is in conflict with DAG! Therefore, the outgoing degree of the most popular vertex must be 0. Second, if the most popular vertex exists, the number of vertices can only be 1, because if it is the most popular vertex, all other vertices must point to it or indirectly point to its outdegree. Once the outdegree is 0
If the vertex is greater than 1, it means that there must be a certain vertex that has no degree of exit, that is to say, there is no point pointing or indirectly pointing to the other vertex with a degree of exit of 0, then the point with an exit degree of 0 cannot reach other points with an exit degree of 0. If all the points with an exit degree of 0 cannot reach other points with an exit degree of 0, therefore, these points are not the most popular points, so the answer is 0.

To sum up, the solution step is to reduce the point and determine the number of scc. If it is 1, the answer is for you. This is not mandatory. If the scc> 1, then judge the degree, if the number of scc with an outbound degree of 0 is 1, the number of the most popular cow is the number of scc midpoint. Do not output 1 directly; if the number of scc with an outbound degree of 0 is greater than 1, the output is 0. This is also applicable to the case where the graph is not connected. If the number of scc is greater than 1, two dags may be formed, so there must be more than one scc with a degree of 0.

Code:

#include <cstdio>#include <cstring>#include <stack>#include <vector>#include <algorithm>using namespace std;const int N = 10010;int n, m;vector <int> G[N];int pre[N], lowlink[N], sccno[N], num[N], dfs_clock, scc_cnt;int in[N], out[N];stack <int> S;void dfs( int u ){    pre[u] = lowlink[u] = ++dfs_clock;    S.push(u);    for ( int i = 0; i < G[u].size(); ++i ) {        int v = G[u][i];        if ( !pre[v] ) {            dfs(v);            lowlink[u] = min( lowlink[u], lowlink[v] );        }        else if ( !sccno[v] ) lowlink[u] = min( lowlink[u], pre[v] );    }    if ( lowlink[u] == pre[u] ) {        scc_cnt++;        for (;;) {            int x = S.top(); S.pop();            sccno[x] = scc_cnt;            num[scc_cnt]++;            if ( x == u ) break;        }    }}void find_scc( ){    dfs_clock = scc_cnt = 0;    memset( num, 0, sizeof(num));    memset( pre, 0, sizeof(pre));    memset( sccno, 0, sizeof(sccno));    for ( int i = 1; i <= n; ++i ) if ( !pre[i] ) dfs(i);}int main(){    while ( scanf("%d%d", &n, &m) != EOF ) {        for ( int i = 0; i <= n; ++i ) G[i].clear();        memset(out, 0, sizeof(out));        while ( m-- ) {            int a, b;            scanf("%d%d", &a, &b);            G[a].push_back(b);        }        find_scc();        if ( scc_cnt == 1 ) {            printf("%d\n", n);            continue;        }        //printf("%d\n", scc_cnt);        for ( int i = 1; i <= n; ++i )            for ( int j = 0; j < G[i].size(); ++j ) {                int v = G[i][j];                if ( sccno[i] != sccno[v] ) {                    //printf("%d %d || %d %d \n", sccno[i], sccno[v], i, v );                    out[sccno[i]]++;                    //in[sccno[v]]++;                }            }        int ans = 0;        //for ( int i = 1; i <= scc_cnt; ++i ) printf("%d\n", out[i]);        for ( int i = 1; i <= scc_cnt; ++i ) {            if ( !out[i] ) {                if ( !ans ) ans = num[i];                else {ans = 0; break;}            }        }            //if ( !out[i] && !ans ) ans = num[i];            //else if ( !out[i] && ans ) { ans = 0; break;}        printf("%d\n", ans);        //printf("ans  %d\n", ans);    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.