P2002 message diffusion problem solution

P2002 message diffusion problem solution

Topic Link

Very bare a shrinking turned a deaf ear, it is clear that each strongly connected component to a point where the message is enough, after shrinking End point it is clear that we have to discuss every point of penetration. For in-degree zero point (no other points give him the news), we have to give it a message, so the number becomes equal to zero points. Due to the size of the data, so we do not need to re-build map.

It is worth mentioning that a small detail. Multiple edges and loopback. I started with a heavy edge map results sentenced t a point (this point is also possible that there is self-loop qaq), and then delete the special judge sentenced map directly from the ring too, and it seems as though the heavy side effects on this question nothing ? ? (Fog

Code:Almost bare of title templates, template title also seems more than a simple point

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <vector>
#include <map>
#define N 100005
#define ll long long
using namespace std;

inline int read()
{
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
    while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}
vector < int > a[N];
stack < int > s;
map < pair < int, int >, int > mp;
int n, m, times, dfn[N], low[N], instc[N], vis[N], mtot, du[N], ans;
//vector存图可能会慢
void tarjan(int x)
{
    dfn[x] = low[x] = ++times;
    instc[x] = 1;
    s.push(x);
    for (int i = 0; i < a[x].size(); i++)
    {
        int u = a[x][i];
        if (!dfn[u])
        {
            tarjan(u);
            low[x] = min(low[x], low[u]);
        }
        else if (instc[u])
            low[x] = min(low[x], dfn[u]);
    }
    if (dfn[x] == low[x])
    {
        ++mtot;
        vis[x] = mtot;
        instc[x] = false;
        while (s.top() != x)
        {
            vis[s.top()] = mtot;
            instc[s.top()] = 0;
            s.pop();
        }
        s.pop();
    }
}

int main()
{
    n = read(), m = read();
    for (int i = 1; i <= m; i++) { int a1 = read(), a2 = read(); if (a1 != a2) a[a1].push_back(a2); }
    for (int i = 1; i <= n; i++)
        if (!dfn[i]) tarjan(i);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j < a[i].size(); j++)
        {
            int u = a[i][j];
            if (vis[u] != vis[i])
            {
                du[vis[u]]++; //更新入度 (不在乎是否重复)
            }
        }
    }
    for (int i = 1; i <= mtot; i++)
        if (du[i] == 0) ans++; //统计入度累加答案
    cout << ans << endl;
    return 0;
}

Finally, I recommend several similar questions, do tarjan will brush:

P1262 spy network

P2341 [HAOI2006] popular cattle | [template] strongly connected component

P3627 [APIO2009] looting plan

The last Tucao why so bare tarjan are blue title, because the popularity of the group not test it qaq

19.09.05

Guess you like

Origin www.cnblogs.com/YuanqiQHFZ/p/11622349.html