Code For [POI 2006] PRO-Professor Szu

· · 题解

/* The code is from koukilee*/
#include <bits/stdc++.h>

using i32 = int; using i64 = int;
constexpr i64 MAXN = 1010100, mod = 1e9 + 7, INF = 992009100720100812; 

static char buf[1000000], *p1 = buf, *p2 = buf, obuf[1000000], *p3 = obuf; 
#define getchar() p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++
#define putchar(x) (p3 - obuf < 1000000) ? (*p3++ = x) : (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf, *p3++ = x)
inline void read (i64 &sum) noexcept {
    i64 tf = 0; char ch = getchar(); sum = 0;
    while (ch < '0' || ch > '9') tf = ch == '-' ? 1 : 0, ch = getchar();
    while (ch >= '0' && ch <= '9') sum = (sum << 1) + (sum << 3) + (ch ^ 48), ch = getchar();
    (tf) && (sum =- sum);
}template <typename i64,typename ...Args>
inline void read (i64 &tmp, Args &...tmps) noexcept{read(tmp); read(tmps...);}
void printt (i64 x) noexcept {if(x >= 10) printt(x / 10); putchar(x % 10 + '0');} 
inline void print (i64 x) noexcept{x < 0 ? putchar('-'), printt(-x) : printt(x);}
inline void put (i64 x) noexcept{print(x), putchar('\n');}

struct edge {
    i64 first[MAXN] = {0}, cnt = 0; struct edg {i64 v, nex;} g[MAXN];
    inline void update (i64 u, i64 v) noexcept {
        g[++cnt] = (edg) {v, first[u]}, first[u] = cnt;
    }
} G, F;

i64 n, m, dfn[MAXN], low[MAXN], id, vis[MAXN], c, pos[MAXN], H, ans;
std::vector <i64> scc[MAXN];
std::set <i64> AN, cir;
std::stack <i64> p;
std::queue <i64> q;

void tarjan (i64 u) noexcept {
    dfn[u] = low[u] = ++id, vis[u] = 1;
    p.push (u);

    for (i32 i = G.first[u]; i; i = G.g[i].nex) {
        if (!dfn[G.g[i].v])
            tarjan (G.g[i].v), low[u] = std::min (low[u], low[G.g[i].v]);
        else if (vis[G.g[i].v])
            low[u] = std::min (low[u], dfn[G.g[i].v]);
    }

    if (low[u] == dfn[u]) {
        i64 x; ++c;
        do {
            x = p.top (); p.pop (); vis[x] = 0; scc[c].push_back (x); 
            pos[x] = c;
        } while (x != u);
    }
}

int main() noexcept{
    read (n, m); n++;

    for (i32 i = 1; i <= m; i++) {
        i64 x, y; read (x, y);
        G.update (x, y);
    }

    for (i32 i = 1; i <= n; i++)
        if (!dfn[i])
            tarjan (i);
    for (i32 i = 1; i <= n; i++)
        dfn[i] = low[i] = 0;

    for (i32 i = 1; i <= n; i++) {
        for (i32 j = G.first[i]; j; j = G.g[j].nex) {
            if (i == G.g[j].v || scc[pos[i]].size () > 1)
                vis[pos[i]] = 1;
            if (pos[i] != pos[G.g[j].v]) 
                F.update (pos[G.g[j].v], pos[i]), dfn[pos[i]]++;
        }
    }

    for (i32 i = 1; i <= c; i++) {
        if (i == pos[n] || !dfn[i])
            q.push (i), dfn[i] = 0;
    }
    low[pos[n]] = 1; 

    while (!q.empty ()) {
        i64 nex = q.front (); q.pop ();

        if (vis[nex] && low[nex]) {
            H += scc[nex].size ();
            for (auto it : scc[nex])
                cir.insert (it);
        }

        low[nex] = std::min (low[nex], 36501);

        for (i32 i = F.first[nex]; i; i = F.g[i].nex) {
            low[F.g[i].v] = std::min (36501, low[nex] + low[F.g[i].v]);
            if (low[nex]) vis[F.g[i].v] |= vis[nex];
            if (!(--dfn[F.g[i].v]))
                q.push (F.g[i].v);
        }

        ans = std::max (ans, low[nex]);
    }

    for (i32 i = 1; i <= c; i++) 
        if (low[i] == ans)
            AN.insert (scc[i][0]);

    if (ans <= 36500 && !H) put (ans);
    else {
        puts ("zawsze");
        if (ans <= 36500) AN.clear ();
        for (auto it : cir)
            AN.insert (it);
    }

    if (AN.find (n) != AN.end ())
        AN.erase (n);

    put (AN.size ());
    for (auto it : AN)
        print (it), putchar (' ');
    fwrite(obuf,p3-obuf,1,stdout);
    return 0;
}