I haven't written a question for a long time = This time it's bzoj 1051

Source: Internet
Author: User

Alas = I thought of the Tarjan point reduction, but I didn't think of the last step. = We can easily think of reverse side creation and then point reduction, at this time, we can see how many Unicom blocks have a 0 inbound degree. If it is 1, the size of this block will be output; otherwise, 0 will be output;

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 10010;const int maxe = 50010;struct edge {    int t;    edge* next;}e[maxe * 2], *head[maxn]; int ne = 0;int n, m;void addedge(int f, int t) {    e[ne].t = t, e[ne].next = head[f]; head[f] = e + ne ++;}void read() {    scanf("%d%d", &n, &m);    for(int i = 1; i <= m; ++ i) {        int u, v;        scanf("%d%d", &u, &v);        addedge(v, u);    }}int dfs[maxn], back[maxn];int s[maxn], top = 0, Index = 0;bool vis[maxn]; int num = 0;int size[maxn];int belong[maxn];void tarjan(int now) {    dfs[now] = back[now] = ++ Index; vis[now] = true;    s[++ top] = now;    for(edge* p = head[now]; p; p = p-> next) {        if(!dfs[p-> t]) tarjan(p-> t), back[now] = min(back[now], back[p-> t]);        else if(vis[p-> t] && back[now] > dfs[p-> t]) {             back[now] = dfs[p-> t];        }    }    if(dfs[now] == back[now]) {        ++ num;        while(1) {            belong[s[top]] = num;             size[num] ++;            vis[s[top]] = false;            if(s[top] == now) break;            top --;        }        top --;    }}int f[maxn]; int in[maxn];bool have[maxn];void sov() {    memset(vis, false, sizeof(false));    memset(size, 0, sizeof(size));    memset(have, 0, sizeof(have));    for(int i = 1; i <= n; ++ i) {        if(!dfs[i]) tarjan(i);    }    memset(in, 0, sizeof(in));    int tol = 0;    for(int i = 1; i <= n; ++ i) {        for(edge* p = head[i]; p; p = p-> next) {            if(belong[p-> t] != belong[i]) {                in[belong[p-> t]] ++;            }        }    }    int pos = 0;    for(int i = 1; i <= num; ++ i) {        if(in[i] == 0) tol ++, pos = i;    }    if(tol == 1) printf("%d\n", size[pos]);    else printf("0\n");}int main() {    //freopen("test.in", "r", stdin);    read(); sov();    return 0;}

 

I haven't written a question for a long time = This time it's bzoj 1051

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.