CodeForces Gym101158H Animal Companion in Maze

Source:2016-2017 ACM-ICPC, Asia Tsukuba Regional Contest

Problem: Give a graph with one-way edges and two-way edges. One limitation of the two-way side is that when a two-way side is used from A - > B, it cannot be used immediately. If there are rings in the graph, output INF, otherwise output the longest path length.

Idea: If two-way edges are regarded as two one-way edges, the strongly connected contraction points of the whole graph are regarded as a tree. If there are no rings, every connected set must be composed of two-way edges. So the title is transformed into DAG + tree DP. Tree DP can run on both sides of DFS, the first time from bottom to top update, each point stored a maximum value and a sub-maximum value, so that it can be updated again from top to bottom.

Code:

#include<bits/stdc++.h>
using namespace std;

#define lson o<<1
#define rson o<<1|1
#define fi first
#define se second
#define pb push_back
#define CLR(A, X) memset(A, X, sizeof(A))
#define bitcount(X) __builtin_popcountll(X)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-10;
const double PI = acos(-1.0);
const auto INF = 0x3f3f3f3f;
int dcmp(double x) { if(fabs(x) < eps) return 0; return x<0?-1:1; }

const int MAXN = 1e5+10;

struct Node {
    int v, a, b;
};

bool inf = 0;
int dfs_clock, scc_cnt;
int low[MAXN], dfn[MAXN], sccno[MAXN];
int in[MAXN], dp[MAXN][2], root[MAXN];
stack<int> S;
vector<Node> H[MAXN];
vector<PII> G[MAXN];

void dfs(int u) {
    low[u] = dfn[u] = ++dfs_clock; S.push(u);
    for(PII x:G[u]) {
        int v = x.fi;
        if(!dfn[v]) { dfs(v); low[u] = min(low[u], low[v]); }
        else if(!sccno[v]) low[u] = min(low[u], dfn[v]);
    }
    if(low[u] == dfn[u]) {
        scc_cnt++;
        for(;;) {
            int x = S.top(); S.pop();
            sccno[x] = scc_cnt;
            root[scc_cnt] = x;
            if(x == u) break;
        }
    }
}

void check(int u, int fa) {
    if(dfn[u]) inf = 1;
    if(inf) return;
    dfn[u] = 1;
    for(PII x:G[u]) {
        int v = x.fi;
        if(sccno[v] != sccno[u]) continue;
        if(x.se == 1) inf = 1;
        if(v == fa) continue;
        check(v, u);
    }
}

void dfs1(int u, int fa) {
    for(PII x:G[u]) if(sccno[x.fi] == sccno[u]) {
        int v = x.fi;
        if(v == fa) continue;
        dfs1(v, u);
        if(dp[v][0]+1 > dp[u][0]) {
            dp[u][1] = dp[u][0];
            dp[u][0] = dp[v][0]+1;
        }
        else dp[u][1] = max(dp[u][1], dp[v][0]+1);
    }
}

void dfs2(int u, int fa) {
    for(PII x:G[u]) if(sccno[x.fi]==sccno[u]) {
        int v = x.fi;
        if(v == fa) continue;
        int t = dp[u][0]-1==dp[v][0]?dp[u][1]+1:dp[u][0]+1;
        if(t > dp[v][0]) {
            dp[v][1] = dp[v][0];
            dp[v][0] = t;
        }
        else dp[v][1] = max(dp[v][1], t);
        dfs2(v, u);
    }
}

int main() {
    int n, m, u, v, op;
    scanf("%d%d", &n, &m);
    while(m--) {
        scanf("%d%d%d", &u, &v, &op);
        G[u].pb({v, op});
        if(op == 2) G[v].pb({u, 2});
    }
    dfs_clock = scc_cnt = 0;
    for(int i = 1; i <= n; i++) if(!dfn[i]) {
        dfs(i);
    }
    CLR(dfn, 0);
    for(int i = 1; i <= n; i++) if(!dfn[i]) {
        check(i, -1);
        if(inf) {
            puts("Infinite");
            return 0;
        }
    }
    for(int i = 1; i <= n; i++) {
        int u = sccno[i];
        for(PII x:G[i]) {
            int v = sccno[x.fi];
            if(u != v) {
                H[u].pb((Node){v, i, x.fi});
                in[v]++;
            }
        }
    }
    queue<int> Q;
    for(int i = 1; i <= scc_cnt; i++) {
        if(!in[i]) Q.push(i);
    }
    while(!Q.empty()) {
        int u = Q.front(); Q.pop();
        dfs1(root[u], -1);
        dfs2(root[u], -1);
        for(Node t:H[u]) {
            in[t.v]--;
            if(!in[t.v]) Q.push(t.v);
            dp[t.b][0] = max(dp[t.b][0], dp[t.a][0]+1);
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        ans = max(ans, dp[i][0]);
    }
    printf("%d\n", ans);
    return 0;
}

Posted by dcuellar on Fri, 08 Feb 2019 07:09:18 -0800