QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#245312#2683. British MenuFyind#WA 0ms3844kbC++205.0kb2023-11-09 20:33:412023-11-09 20:33:42

Judging History

你现在查看的是最新测评结果

  • [2023-11-09 20:33:42]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3844kb
  • [2023-11-09 20:33:41]
  • 提交

answer

#include <bits/stdc++.h>
#define sz(x) ((x).size())
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

struct Tarjan {
    // G 编号从1开始 , no multiple edges
    int n, m, timer, cnt;
    vector<vector<int>> oG, bcc, G; // original graph, G: out graph, bcc[i] 第i个强连通分量所有的点
    vector<int> dfn, low, low2,belong, cutv, iscut; // belong: 原图的点缩点后的编号, cutv: cut vertex
    vector<pii> cute; 
    vector<bool> ins;
    stack<int> s;
    
    Tarjan(int n=0) : n(n), m(0), timer(0), cnt(0), oG(n+1), 
        bcc(1),G(1),dfn(n+1,-1),low(n+1),low2(n+1),belong(n+1),iscut(n+1),ins(n+1) {}
    
    void addedge(int a, int b) {
        oG[a].push_back(b);
    }

    void dfs(int x, int fa) {
        dfn[x] = low[x] = low2[x] = ++timer;
        s.push(x); ins[x] = 1;
        int tot = 0;
        for (auto v : oG[x]) {
            if (!~dfn[v]) {
                dfs(v, x);
                if (x == fa) tot++;
                low[x] = min(low[x], low[v]);
                low2[x] = min(low2[x], low2[v]);
                if (low[v] >= dfn[x] && x != fa) {
                    iscut[x] = 1;
                }
                if (low2[v] > dfn[x]) cute.push_back({x, v});
            } else if (ins[v]) low[x] = min(low[x], dfn[v]);
            if (v != fa) low2[x] = min(low2[x], dfn[v]);
        }
        if (tot > 1) iscut[x] = 1;
        if (low[x] == dfn[x]) {
            int now; bcc.push_back(vector<int>()); G.push_back(vector<int>());
            int cur = ++cnt;
            do {
                now = s.top(); s.pop(); ins[now] = 0;
                belong[now] = cur;
                bcc.back().push_back(now);
            } while (now != x);
            sort(bcc.back().begin(), bcc.back().end());
        }
    }

    void calc() {
        for (int i = 1;i <= n; ++i) if (!~dfn[i]) dfs(i, i);  
    }
};

int main() {
    #ifdef LLOCAL
    freopen("a.txt", "r", stdin);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<pii> e(m + 1);
    Tarjan tarjan(n);
    for (int i = 1; i <= m; ++i) {
        cin >> e[i].first >> e[i].second;
        tarjan.addedge(e[i].first, e[i].second);
    }
    tarjan.calc();
    vector<pii> pos(n + 1);
    for (int i = 1; i <= tarjan.cnt; ++i) {
        for (int j = 0; j < tarjan.bcc[i].size(); ++j)
            pos[tarjan.bcc[i][j]] = {i, j};
    }

    vector<vector<vector<int>>> g(tarjan.cnt + 1);
    vector<int> siz(tarjan.cnt + 1);
    for (int i = 1; i <= tarjan.cnt; ++i) {
        siz[i] = tarjan.bcc[i].size();
        vector<vector<int>> gg(siz[i], vector<int>(siz[i], 0));
        g[i] = vector<vector<int>>(siz[i], vector<int>(siz[i], -1));
        for (int u : tarjan.bcc[i])
            for (int v : tarjan.oG[u])
                if (pos[v].first == i)
                    gg[pos[u].second][pos[v].second] = g[i][pos[u].second][pos[v].second] = 1;
        for (int j = 0; j < siz[i]; ++j)
            g[i][j][j] = 0;
        vector<bool> vis(siz[i]);
        function<void(int, int, int)> dfs = [&](int u, int s, int d) {
            vis[u] = true;
            g[i][s][u] = max(g[i][s][u], d);
            for (int v = 0; v < siz[i]; ++v)
                if (!vis[v] && gg[u][v])
                    dfs(v, s, d + 1);
            vis[u] = false;
        };
        for (int j = 0; j < siz[i]; ++j)
            dfs(j, j, 0);
    }

    int nn = tarjan.cnt;
    vector<vector<pair<int, pair<int, int>>>> ng(nn + 1);
    vector<int> in(nn + 1);
    for (int i = 1; i <= m; ++i)
        if (pos[e[i].first].first != pos[e[i].second].first)
            ng[pos[e[i].first].first].push_back({pos[e[i].second].first, {pos[e[i].first].second, pos[e[i].second].second}}), ++in[pos[e[i].second].first];
    queue<int> q;
    for (int i = 1; i <= nn; ++i)
        if (in[i] == 0)
            q.push(i);
    vector<vector<int>> f(nn + 1);
    for (int i = 1; i <= nn; ++i)
        f[i] = vector<int>(siz[i]);
    vector<bool> vis(nn + 1);
    int ans = 0;
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        // cout << u << endl;
        for (int i = 0; i < siz[u]; ++i)
            for (int j = 0; j < siz[u]; ++j)
                ans = max(ans, g[u][i][j] + f[u][i]);
        for (auto [v, kk] : ng[u]) {
            int x = kk.first, y = kk.second;
            int mx = 0;
            for (int i = 0; i < siz[u]; ++i)
                mx = max(mx, g[u][i][x] + f[u][i]);
            f[v][y] = max(f[v][y], mx + 1);
            if (!vis[v])
                q.push(v), vis[v] = true;
        }
    }
    // cout << nn << endl;
    // for (int i = 1; i <= nn; ++i) {
    //     cout << i << ':' << endl;
    //     for (int x : tarjan.bcc[i])
    //         cout << x << ' ';
    //     cout << endl;
    //     for (int j = 0; j < siz[i]; ++j)
    //         cout << f[i][j] << ' ';
    //     cout << endl;
    // }
    cout << ans + 1 << endl;
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3844kb

input:

10 6
7 8
4 2
8 6
5 1
4 1
4 5

output:

3

result:

ok single line: '3'

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3832kb

input:

10 10
1 3
8 9
6 10
1 2
7 9
10 9
5 1
2 5
8 6
7 8

output:

4

result:

wrong answer 1st lines differ - expected: '5', found: '4'