poj 2186 DAG+缩点+tarjan模板 (模板待消化

题目:
Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 33895 Accepted: 13803

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



给一个有向图,问其中有几个点是其他所有点都可以到达的


分析:

首先,如果暴力搜索,从每一个点出发dfs该点可以到达的所有点 最后统计有几个点的到达次数为n-1,复杂度为O(NM),超时

由于给定的图是有向图,此时我们想到有一个性质:

对于一个有向无环图来说,其中有且仅有一个点出度为零,那么这个特殊的点,可以由其他任何点到达。

因此可以对给定的图进行缩点,消除所有的环。如果缩点之后得到的是多个DAG(有向无环图),说明原图本来就不连通,输出零 这种情况可以通过统计缩点之后的图中有几个出度为零的点来判断;

否则,输出唯一的DAG中最后面那个点包含的顶点数


tarjan算法入门:

http://blog.csdn.net/acmmmm/article/details/16361033

https://www.byvoid.com/zhs/blog/scc-tarjan

https://comzyh.com/blog/archives/517/

http://blog.miskcoo.com/2016/07/tarjan-algorithm-strongly-connected-components



代码:

#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <ctime>
#include <cmath>
using namespace std;
const int maxv = 10000;
const int INF = 0x3f3f3f3f;
int V;
vector<int> g[maxv];

int dfn_idx = 0;//第几轮dfs
int totv = 0;//缩点之后的点数
int dfn[maxv];//遍历到i点是时第几次fs
int low[maxv];
int belong[maxv];//缩成哪个点
bool in_st[maxv];
vector<int> st;//模拟栈

void scc(int v) {// scc  强连通分量问题
    dfn[v] = low[v] = dfn_idx++;
    st.push_back(v);
    in_st[v] = true;
    for (size_t i = 0; i < g[v].size(); i++) {
        const int u = g[v][i];
        if (dfn[u] == -1) {
            scc(u);
            low[v] = min(low[v], low[u]);
        }
        else if (in_st[u]) {
            low[v] = min(low[v], dfn[u]);
        }
    }
    if (dfn[v] == low[v]) {
        int k;
        do {
            k = st.back(); st.pop_back();
            in_st[k] = false;
            belong[k] = totv;
        } while (k != v);
        totv++;
    }
}

void tarjan() {
    st.clear();
    fill(dfn, dfn + V, -1);
    fill(low, low + V, INF);
    dfn_idx = 0;//第零轮dfs
    totv = 0;
    for (int v = 0; v < V; v++) {
        if (dfn[v] == -1) {//前几轮dfs还没有搜到这个点
            scc(v);
        }
    }
}

int solve() {
    tarjan();
    vector<int> out(totv, 0);//存储结点的出度
    for (int v = 0; v < V; v++) {
        for (int i = 0; i < g[v].size(); i++) {//size_t表示无符号整数for (size_t i = 0; i < g[v].size(); i++)
            int u = g[v][i];
            if (belong[v] != belong[u]) {//没有缩到一个点,缩点之后依然有边
                out[belong[v]]++;//
            }
        }
    }
    int lastvidx = -1, lastvnum = 0;
    for (int c = 0; c < totv; c++) {
        if (out[c] == 0) {
            lastvidx = c;
            lastvnum++;
        }
    }
    if (lastvnum != 1) {//多个DAG
        return 0;
    }
    int ans = 0;
    for (int v = 0; v < V; v++) {
        if (belong[v] == lastvidx) {
            ans++;//最后一个点包含的顶点数
        }
    }
    return ans;
}

int main() {//344 ms
    int N, M;
    scanf("%d %d", &N, &M);
    for (int i = 0; i < M; i++) {
        int u, v;
        scanf("%d %d", &u, &v); u--; v--;
        g[u].push_back(v);
    }
    V = N;//
    printf("%d\n", solve());
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值