zoukankan      html  css  js  c++  java
  • 【BZOJ1051】[HAOI2006]受欢迎的牛

    【BZOJ1051】[HAOI2006]受欢迎的牛

    题面

    bzoj

    洛谷

    题解

    假如(A)喜欢(B)就连一条(A)(B)的边

    然后缩点,如果图不连通就(Impossible)

    否则输出出度为(0)的环的大小

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    
    inline int gi(){
        register int data=0, w = 1;
        register char ch=0;
        while (ch != '-' && (ch > '9' || ch < '0')) ch = getchar();
        if (ch == '-') w = -1 , ch = getchar();
        while (ch >= '0' && ch <= '9') data = (data << 1) + (data << 3) + (ch ^ 48), ch = getchar();
        return w * data;
    } 
    #define MAX_N 10005
    #define MAX_M 50005 
    struct Edge { 
        int to, next; 
    } e[MAX_M]; 
    int h[MAX_N], cnt; 
    int n, m; 
    void Add_Edge(int u, int v) { 
        e[cnt].to = v, e[cnt].next = h[u], h[u] = cnt++; 
    } 
    int tim = 0, num = 0, top = 0; 
    int low[MAX_N], dfn[MAX_N], color[MAX_N], st[MAX_N]; 
    int tot[MAX_N]; 
    void tarjan(int x) { 
        low[x] = dfn[x] = ++tim; 
        st[++top] = x; 
        for (register int i = h[x]; ~i; i = e[i].next) { 
            if (!dfn[e[i].to]) { 
                tarjan(e[i].to); 
                low[x] = min(low[x], low[e[i].to]); 
            } else if (!color[e[i].to]) 
                low[x] = min(low[x], dfn[e[i].to]); 
        } 
        if (low[x] == dfn[x]) { 
            color[x] = ++num; 
            tot[num]++; 
            while (st[top] != x) { 
                color[st[top]] = num; 
                tot[num]++; 
                --top; 
            } 
            --top; 
        }
    } 
    int d[MAX_N]; 
    int main () { 
        memset(h, -1, sizeof(h)); 
        n = gi(), m = gi(); 
        for (register int i = 1; i <= m; i++) { 
            int u = gi(), v = gi(); 
            Add_Edge(u, v); 
        } 
        for (register int i = 1; i <= n; i++) 
            if (!dfn[i]) tarjan(i); 
        int ans = 0, F = 0; 
        for (register int x = 1; x <= n; x++) 
            for (int i = h[x]; ~i; i = e[i].next) 
            if (color[x] != color[e[i].to]) d[color[x]]++; 
        for (register int i = 1; i <= num; i++) {
        	if (d[i] == 0) F++, ans = tot[i]; 
        } 
        if (F == 1) printf("%d
    ", ans); 
    	else printf("0
    ");   
        return 0; 
    }
    
  • 相关阅读:
    PSE Access Service
    The JXTA Migration
    JXSE 2.5 : What's Cool #6 PeerGroup Executor and ScheduledExcutor
    JXTA Kitchen
    LookupListener中的resultChanged方法是在EDT中执行么?
    同一台机器启动两个结点时的端口冲突问题
    (转)OpenSSL中对称加密算法的统一接口
    关于“未能加载文件或程序集“System.Core, Version=3.5.0.0
    暗香浮动的夜晚
    java xml序列化与反序列化
  • 原文地址:https://www.cnblogs.com/heyujun/p/10179180.html
Copyright © 2011-2022 走看看