poj 2186 Popular Cows(强连通分量,tarjan或Kosaraju)

来源:互联网 发布:淘宝懒人鞋阿迪 编辑:程序博客网 时间:2024/05/01 22:55

题目地址:点击打开链接

题意:

给定一些有向路,求有多少个点可以由其余的任意点到达


思路:

第一道强连通分量题,照着白书写的。

最后判断拓扑排序最后一个强连通分量是否可行看到两种方法,白书是对最后一个连通分量再逆向dfs一遍,验证是否所有点都能到达;看到网上大多的解法是计算每个连通分量的出度,若当且仅当出度为0的只有1个才符合。

白书:



白书上用的是Kosaraju算法,也可以用tarjan算法,感觉还是kosaraju更好理解一点..


Kosaraju代码:

#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 1e4+5;const int maxm = 5e4+5;vector<int> g[maxm];vector<int> rg[maxm];vector<int> vs; //postorderbool used[maxn];int top[maxn], du[maxn], n, m, k;void dfs(int u){    used[u] = 1;    for(int i = 0; i < g[u].size(); i++)    {        int v = g[u][i];        if(!used[v]) dfs(v);    }    vs.push_back(u);}void rdfs(int u, int k){    used[u] = 1;    top[u] = k;    for(int i = 0; i < rg[u].size(); i++)    {        int v = rg[u][i];        if(!used[v]) rdfs(v, k);    }}int scc(){    memset(used, 0, sizeof(used));    vs.clear();    for(int i = 1; i <= n; i++)        if(!used[i]) dfs(i);    memset(used, 0, sizeof(used));    k = 0;    for(int i = vs.size()-1; i >= 0; i--)        if(!used[vs[i]]) rdfs(vs[i], k++);    return k;}int main(void){    while(cin >> n >> m)    {        for(int i = 1; i < maxn; i++)            g[i].clear(), rg[i].clear();        for(int i = 1; i <= m; i++)        {            int u, v;            scanf("%d%d", &u, &v);            g[u].push_back(v);            rg[v].push_back(u);        }        int num = scc();        int ans = 0, u;        for(int i = 1; i <= n; i++)            if(top[i] == num-1) u = i, ans++;        //检查拓扑排序最后的强连通分量是否所有点都能到达//        memset(used, 0, sizeof(used));//        rdfs(u, 0); //检查是否所有点可达//        for(int i = 1; i <= n; i++)//            if(!used[i])//            {//                ans = 0;//                break;//            }//        printf("%d\n", ans);        //看强连通分量出度0的点是否唯一        memset(du, 0, sizeof(du));        for(int i = 1; i <= n; i++)            for(int j = 0; j < g[i].size(); j++)                if(top[i] != top[g[i][j]])                    du[top[i]]++;        int sum = 0;        for(int i = 0; i < num; i++)            if(!du[i]) sum++;        if(sum == 1) printf("%d\n", ans);        else printf("0\n");    }    return 0;}


tarjan代码:

#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 1e4+5;const int maxm = 5e4+5;vector<int> g[maxm];bool vis[maxn];int n, m, k, cnt, index, stack[maxn], dfn[maxn], low[maxn], top[maxn], du[maxn];void tarjan(int x){    low[x] = dfn[x] = cnt++;    stack[index++] = x;    vis[x] = 1;    for(int i = 0; i < g[x].size(); i++)    {        int to = g[x][i];        if(!dfn[to])        {            tarjan(to);            low[x] = min(low[x], low[to]);        }        else if(vis[to]) low[x] = min(low[x], dfn[to]);    }    if(low[x] == dfn[x])    {        do{            index--;            top[stack[index]] = k;            vis[stack[index]] = 0;        }while(stack[index] != x);        k++;    }}void judge(){    int sum = 0, ans = 0, imp;    for(int i = 1; i <= n; i++)        for(int j = 0; j < g[i].size(); j++)            if(top[i] != top[g[i][j]])                du[top[i]]++;//    for(int i = 1; i <= n; i++)//        printf("%d ", top[i]);    //检查出度为0的强连通分支是否唯一    for(int i = 1; i < k; i++)        if(!du[i]) sum++, imp = i;    if(sum != 1) puts("0\n");    else    {        for(int i = 1; i <= n; i++)            if(top[i] == imp) ans++;        printf("%d\n", ans);    }}int main(void){    while(cin >> n >> m)    {        memset(vis, 0, sizeof(vis));        memset(du, 0, sizeof(du));        memset(dfn, 0, sizeof(dfn));        for(int i = 0; i < maxm; i++)            g[i].clear();        for(int i = 1; i <= m; i++)        {            int u, v;            scanf("%d%d", &u, &v);            g[u].push_back(v);        }        cnt = index = k = 1;        for(int i = 1; i <= n; i++)            if(!dfn[i]) tarjan(i);        judge();    }    return 0;}



Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 32410 Accepted: 13213

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 31 22 12 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source

USACO 2003 Fall

0 0