QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#864876#4809. Maximum RangeMisty7WA 90ms41368kbC++208.1kb2025-01-21 10:31:162025-01-21 10:31:17

Judging History

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

  • [2025-01-21 10:31:17]
  • 评测
  • 测评结果:WA
  • 用时:90ms
  • 内存:41368kb
  • [2025-01-21 10:31:16]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

std::set<std::pair<int, int>> E;

struct EBCC {
    int n;
    std::vector<std::vector<int>> adj;
    std::vector<int> stk;
    std::vector<int> dfn, low, bel;
    int cur, cnt;
    
    EBCC() {}
    EBCC(int n) {
        init(n);
    }
    
    void init(int n) {
        this->n = n;
        adj.assign(n, {});
        dfn.assign(n, -1);
        low.resize(n);
        bel.assign(n, -1);
        stk.clear();
        cur = cnt = 0;
    }
    
    void addEdge(int u, int v) {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    
    void dfs(int x, int p) {
        dfn[x] = low[x] = cur++;
        stk.push_back(x);
        
        for (auto y : adj[x]) {
            if (y == p) {
                continue;
            }
            if (dfn[y] == -1) {
                E.emplace(x, y);
                dfs(y, x);
                low[x] = std::min(low[x], low[y]);
            } else if (bel[y] == -1 && dfn[y] < dfn[x]) {
                E.emplace(x, y);
                low[x] = std::min(low[x], dfn[y]);
            }
        }
        
        if (dfn[x] == low[x]) {
            int y;
            do {
                y = stk.back();
                bel[y] = cnt;
                stk.pop_back();
            } while (y != x);
            cnt++;
        }
    }
    
    std::vector<int> work() {
        dfs(0, -1);
        return bel;
    }
    
    struct Graph {
        int n;
        std::vector<std::pair<int, int>> edges;
        std::vector<int> siz;
        std::vector<int> cnte;
    };
    Graph compress() {
        Graph g;
        g.n = cnt;
        g.siz.resize(cnt);
        g.cnte.resize(cnt);
        for (int i = 0; i < n; i++) {
            g.siz[bel[i]]++;
            for (auto j : adj[i]) {
                if (bel[i] < bel[j]) {
                    g.edges.emplace_back(bel[i], bel[j]);
                } else if (i < j) {
                    g.cnte[bel[i]]++;
                }
            }
        }
        return g;
    }
};

template<class T>
struct MaxFlow {
    struct _Edge {
        int to;
        T cap;
        _Edge(int to, T cap) : to(to), cap(cap) {}
    };
    
    int n;
    std::vector<_Edge> e;
    std::vector<std::vector<int>> g;
    std::vector<int> cur, h;
    
    MaxFlow() {}
    MaxFlow(int n) {
        init(n);
    }
    
    void init(int n) {
        this->n = n;
        e.clear();
        g.assign(n, {});
        cur.resize(n);
        h.resize(n);
    }
    
    bool bfs(int s, int t) {
        h.assign(n, -1);
        std::queue<int> que;
        h[s] = 0;
        que.push(s);
        while (!que.empty()) {
            const int u = que.front();
            que.pop();
            for (int i : g[u]) {
                auto [v, c] = e[i];
                if (c > 0 && h[v] == -1) {
                    h[v] = h[u] + 1;
                    if (v == t) {
                        return true;
                    }
                    que.push(v);
                }
            }
        }
        return false;
    }
    
    T dfs(int u, int t, T f) {
        if (u == t) {
            return f;
        }
        auto r = f;
        for (int &i = cur[u]; i < int(g[u].size()); ++i) {
            const int j = g[u][i];
            auto [v, c] = e[j];
            if (c > 0 && h[v] == h[u] + 1) {
                auto a = dfs(v, t, std::min(r, c));
                e[j].cap -= a;
                e[j ^ 1].cap += a;
                r -= a;
                if (r == 0) {
                    return f;
                }
            }
        }
        return f - r;
    }
    void addEdge(int u, int v, T c) {
        g[u].push_back(e.size());
        e.emplace_back(v, c);
        g[v].push_back(e.size());
        e.emplace_back(u, 0);
    }
    void addEdgeDir(int u, int v, T c) {
        g[u].push_back(e.size());
        e.emplace_back(v, c);
        g[v].push_back(e.size());
        e.emplace_back(u, c);
    }
    T flow(int s, int t) {
        T ans = 0;
        while (bfs(s, t)) {
            cur.assign(n, 0);
            ans += dfs(s, t, std::numeric_limits<T>::max());
        }
        return ans;
    }
    
    std::vector<bool> minCut() {
        std::vector<bool> c(n);
        for (int i = 0; i < n; i++) {
            c[i] = (h[i] != -1);
        }
        return c;
    }
    
    struct Edge {
        int from;
        int to;
        T cap;
        T flow;
    };
    std::vector<Edge> edges() {
        std::vector<Edge> a;
        for (int i = 0; i < e.size(); i += 2) {
            Edge x;
            x.from = e[i + 1].to;
            x.to = e[i].to;
            x.cap = e[i].cap + e[i + 1].cap;
            x.flow = e[i + 1].cap;
            a.push_back(x);
        }
        return a;
    }
};

constexpr int inf = 1E9 + 1;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, m;
    std::cin >> n >> m;

    EBCC ebcc(n);
    std::vector<std::map<int, int>> mp(n);
    for (int i = 0; i < m; i++) {
        int x, y, w;
        std::cin >> x >> y >> w;
        x--, y--;

        ebcc.addEdge(x, y);
        mp[x][y] = mp[y][x] = w;
    }

    ebcc.work();

    std::vector<int> mina(n, -1), minb(n, -1), maxa(n, -1), maxb(n, -1);
    std::vector<int> mine(n, inf), maxe(n, -inf);
    for (int i = 0; i < n; i++) {
        for (auto [j, w] : mp[i]) {
            if (ebcc.bel[i] == ebcc.bel[j]) {
                if (w < mine[ebcc.bel[i]]) {
                    mine[ebcc.bel[i]] = w;
                    mina[ebcc.bel[i]] = i;
                    minb[ebcc.bel[i]] = j;
                }
                if (w > maxe[ebcc.bel[i]]) {
                    maxe[ebcc.bel[i]] = w;
                    maxa[ebcc.bel[i]] = i;
                    maxb[ebcc.bel[i]] = j;
                }
            }
        }
    }

    int best = -1;
    for (int i = 0; i < n; i++) {
        if (best == -1 || maxe[i] - mine[i] >= maxe[best] - mine[best]) {
            best = i;
        }
    }

    MaxFlow<int> flow(n + 2);
    int s = n, t = s + 1;

    for (int i = 0; i < n; i++) {
        for (int j : ebcc.adj[i]) {
            if ((i == maxa[best] && j == maxb[best]) || (i == mina[best] && j == minb[best])) {
                continue;
            }
            if ((i == maxb[best] && j == maxa[best]) || (i == minb[best] && j == mina[best])) {
                continue;
            }
            if (ebcc.bel[i] == best && ebcc.bel[j] == best && i < j) {
                // std::cerr << i << " " << j << "\n";
                flow.addEdgeDir(i, j, 1);
            }
        }
    }
    flow.addEdge(s, mina[best], 1);
    flow.addEdge(s, minb[best], 1);
    flow.addEdge(maxa[best], t, 1);
    flow.addEdge(maxb[best], t, 1);

    int fl = flow.flow(s, t);
    assert(fl == 2);

    auto edges = flow.edges();

    std::vector<std::vector<int>> adj(n);
    for (auto [from, to, cap, flow] : edges) {
        if (flow == 2 && from != s && to != t) {
            adj[from].push_back(to);
            adj[to].push_back(from);
            if (n >= 50000) {
                std::cout << from << " " << to << "\n";
            }
        }
    }

    adj[mina[best]].push_back(minb[best]);
    adj[minb[best]].push_back(mina[best]);
    // std::cerr << mina[best] << " " << minb[best] << "\n";
    adj[maxa[best]].push_back(maxb[best]);
    adj[maxb[best]].push_back(maxa[best]);
    // std::cerr << maxa[best] << " " << maxb[best] << "\n";

    std::vector<std::map<int, int>> vis(n);

    std::vector<int> stk;
    auto dfs = [&](auto &&self, int x) -> void {
        for (int y : adj[x]) {
            if (vis[x][y]) {
                continue;
            }
            vis[x][y] = vis[y][x] = 1;
            self(self, y);
        }
        stk.push_back(x);
    };
    dfs(dfs, mina[best]);

    std::cout << maxe[best] - mine[best] << "\n";
    std::cout << stk.size() << "\n";
    for (int i = 0; i < stk.size(); i++) {
        std::cout << stk[i] + 1 << " \n"[i == stk.size() - 1];
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3712kb

input:

5 7
1 2 1
1 3 -2
2 3 1
3 4 3
4 5 1
1 5 -1
2 5 2

output:

5
4
5 4 3 1

result:

ok ok

Test #2:

score: -100
Wrong Answer
time: 90ms
memory: 41368kb

input:

99997 100000
12238 99016 352755196
99016 25485 -412473602
25485 2440 991507552
2440 31171 -181894654
36970 2440 -800167579
2440 41865 -148191946
96629 31171 847888506
36970 95740 395546542
27992 2440 647886610
99016 29557 369124914
80795 27992 -673871966
36970 3509 573208857
57672 29557 874406776
41...

output:

2439 25484
3462 72824
4696 96047
22073 72088
25484 99015
29556 57671
30287 68522
34882 46300
37693 88288
46300 96777
51490 87711
54072 84996
57671 69258
67965 84406
72088 76021
74676 86273
76021 86664
84996 89627
86273 94990
86664 92616
87711 96229
1959330954
2
95092 31171

result:

wrong answer Integer 1959330954 violates the range [1, 99997]