poj 2186 强连通分量Tarjan

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 27786 Accepted: 11192

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M 

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source


题目链接:http://poj.org/problem?id=2186

赛前临时突击...#- -|||

题目大意:给定一个有向图,求有多少定点是由任何顶点出发都可达的。

思路:
有向无环图中唯一出度为0的点,一定可以由任何点出发均可达(由于无环,所以从任何点出发往前走,必然终止于一个出度为0的点).
所以用Tarjan算法求出所有的强连通分量,然后进行缩点,转换成一个DAG图,然后找强连通分量出度为0的那个,如果有多个,则输出0,如果只有一个,就输出该强连通分量所包含的顶点个数。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <limits.h>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
#define pll pair<LL, LL>
#define pii pair<int, int>
#define X first
#define Y second
#define MAXN 10010//点数
#define MAXM 50010//边数
#define lson l, mid, (rt << 1)
#define rson mid + 1, r, (rt << 1 | 1)
const double eps = 1e-10;
const LL inf = 1000000000000000010;
int n, m;
struct Edge {
    int to, next;
} edge[MAXM];
int head[MAXN], tot;
int Low[MAXN], DFN[MAXN], Stack[MAXN], Belong[MAXN];//1~scc
int Index, top;
int scc;
bool Instack[MAXN];
int out[MAXN];
int num[MAXN];//各个强连通分量包含的点的个数
void addedge(int u, int v) {
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}
void Tarjan(int u) {
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    Instack[u] = true;
    for(int i = head[u]; i != -1; i = edge[i].next) {
        v = edge[i].to;
        if(!DFN[v]) {
            Tarjan(v);
            if(Low[u] > Low[v]) Low[u] = Low[v];
        } else if(Instack[v] && Low[u] > DFN[v])
            Low[u] = DFN[v];
    }
    if(Low[u] == DFN[u]) {
        scc++;
        do {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc;
            num[scc]++;
        }
        while(v != u);
    }
}
void init() {
    tot = 0;
    memset(head, -1, sizeof head);
    memset(out, 0, sizeof out);
}
void solve(int N) {
    memset(DFN, 0, sizeof DFN);
    memset(Instack, false, sizeof Instack);
    memset(num, 0, sizeof num);
    Index = scc = top = 0;
    for(int i = 1; i <= N; i++) {
        if(!DFN[i]) Tarjan(i);
    }
}
int main() {
    while(~scanf("%d%d", &n, &m)) {
        init();
        int a, b;
        for(int i = 0; i < m; i++) {
            scanf("%d%d", &a, &b);
            addedge(a, b);
        }
        solve(n);
        for(int u = 1; u <= n; u++) {
            for(int i = head[u]; i != -1; i = edge[i].next) {
                int v = edge[i].to;
                if(Belong[u] == Belong[v]) continue;
                out[Belong[u]]++;
            }
        }
        int ans = 0, isok = 0;
        for(int i = 1; i <= scc; i++) {
            if(out[i] == 0) {
                ans = num[i]; isok++;
            }
            if(isok > 1) {
                ans = 0; break;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

PS:

什么是强连通分量?在这之前先定义一个强连通性(strong connectivity)的概念:有向图中,如果一个顶点s到t有一条路径,t到s也有一条路径,即s与t互相可达,那么我们说s与t是强连通的。那么在有向图中,由互相强连通的顶点构成的分量,称作强连通分量。
首先说一些离散数学相关的结论,由强连通性的概念可以发现,这是一个等价关系。

证明:
一,按照有向图的约定,每个顶点都有到达自身的路径,即自环,即任意顶点s到s可达,满足自反性;
二,如果s与t是强连通的,则s到t存在路径,t到s存在路径,显然t与s也是强连通的,满足对称性;
三,如果r与s强连通,s与t强连通,则r与s互相可达,s与t互相可达,显然r与t也互相可达,满足传递性。
因此,强连通关系可导出一个等价类,这就是强连通分量。进一步的利用这结论可以知道,两个强连通分量之间没有交集(这个结论很重要)。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值