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

题目地址:点击打开链接

题意:

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


思路:

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

最后判断拓扑排序最后一个强连通分量是否可行看到两种方法,白书是对最后一个连通分量再逆向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; //postorder
bool 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: 65536K
Total 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 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity. 

Source


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值