BZOJ1051: [HAOI2006]受欢迎的牛(tarjan缩点)

版权声明:转载请声明出处,谢谢支持 https://blog.csdn.net/Dreamstar_DS/article/details/82501143

1051: [HAOI2006]受欢迎的牛

Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 7834 Solved: 4222
[Submit][Status][Discuss]
Description

  每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这
种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头
牛被所有的牛认为是受欢迎的。
Input

  第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可
能出现多个A,B)
Output

  一个数,即有多少头牛被所有的牛认为是受欢迎的。

Sample Input

3 3

1 2

2 1

2 3
Sample Output

1
HINT

100%的数据N<=10000,M<=50000

这道题的题意已经很明显了,我们将每个强连通分量缩点后重建图,这时一定至少有一个没有有出边的点(否则被缩),然后找到没有出边的点(对于有向图而言,有出边说明还有奶牛不喜爱),如果这样的点有多个,这显然是不合法的,因为这说明这两个点之间没有边(为什么会有多个说明这是两个相互隔离的子图

AC Code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define rg register
#define il inline
#define maxn 500005
#define lid id << 1
#define rid (id << 1) | 1
#define ll long long
using namespace std;
il int read(){rg int x = 0 , w = 1;rg char ch = getchar();while (ch < '0' || ch > '9'){if (ch == '-') w = -1;ch = getchar();}while (ch >= '0' && ch <= '9'){x = (x << 3) + (x << 1) + ch - '0';ch = getchar();}return x * w;}
struct edge{
    int to , next;  
}e[maxn << 1];
struct edge2{
    int to , next;  
}e2[maxn << 1];
int head[maxn] , cnt , top , col , num[maxn] , dfn[maxn] , low[maxn] , belong[maxn] , idx , q[maxn];
bool vis[maxn] , inq[maxn];
int head2[maxn] , cnt2;
void add(int u,int v){
    e[++cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}
void add2(int u,int v){
    e2[++cnt2].to = v;
    e2[cnt2].next = head2[u];
    head2[u] = cnt2;
}
void dfs(int x){
    rg int now; 
    dfn[x] = low[x] = ++idx;
    vis[x] = inq[x] = 1;
    q[++top] = x;
    for (rg int i = head[x] ; i ; i = e[i].next){
        rg int to = e[i].to;
        if (!vis[to]){
            dfs(to);
            low[x] = min (low[x] , low[to]);    
        }
        else if (inq[to]) low[x] = min(low[x] , dfn[to]);
    }
    if (dfn[x] == low[x]){
        ++col;
        do{
            now = q[top--];
            belong[now] = col;
            inq[now] = 0;
            ++num[col]; 
        }while (x != now);
    }
}
void rebuild(int n){
    for (rg int now = 1 ; now <= n ; ++now){
        for (rg int i = head[now] ; i ; i =e[i].next){
            rg int to = e[i].to;
            if (belong[now] != belong[to]){
                add2(belong[now] , belong[to]); 
            }
        }
    }
}
void tarjan(int n){
    for (rg int i = 1 ; i <= n ; ++i) if (!vis[i]) dfs(i);
    rebuild(n);
}
int main(){
    rg int n = read() , m = read() , u , v;
    for (rg int i = 1 ; i <= m ; ++i){
        u = read() ,  v = read();
        add(u , v);
    }
    tarjan(n);
    rg int ans = 0;
    for (rg int now = 1 ; now <= col ; ++now){
        if (!head2[now]){
            if (ans){
                ans = 0;break;  
            }
            ans = num[now];
        }
    }
    printf("%d",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dreamstar_DS/article/details/82501143
今日推荐