QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#344284#995. 桥pangwtRE 155ms29324kbC++203.1kb2024-03-03 22:38:192024-03-03 22:38:20

Judging History

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

  • [2024-03-03 22:38:20]
  • 评测
  • 测评结果:RE
  • 用时:155ms
  • 内存:29324kb
  • [2024-03-03 22:38:19]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
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++;
        }
    }

    bool isCutEdge(int x, int y) {
    	return low[y] > dfn[x];
    }
    
    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;
    }
};

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int n, m;
    std::cin >> n >> m;
    
    EBCC g(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        std::cin >> u >> v;
        u--, v--;
        g.addEdge(u, v);
    }
    
    auto bel = g.work();
    auto graph = g.compress();

    // for (auto [x, y] : graph.edges) {
    // 	cout << x + 1 << " " << y + 1 << "\n";
    // }
    for (auto [x, y] : E) {
    	if (x < y && g.isCutEdge(x, y)) {
    		cout << x + 1 << " " << y + 1 << "\n";
    	}
    }
    
    return 0;
}
// * `n`: 图的顶点数。
// * `edges`: 图的边的信息,每一对(i, j)表示原始图中从i到j的一条边。
// * `siz`: 压缩后的图的大小数组,每个元素bel[i]对应的位置上记录了原始图中从i出发的边的数量。
// * `cnte`: 压缩后的图的连通分量数组,表示原始图中每个连通分量的大小。

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 155ms
memory: 29324kb

input:

24942 387166
12556 21443
22404 16376
11073 24296
1535 11968
23745 2818
5073 12731
22550 14761
24118 12008
22695 18979
15118 13639
2080 8721
692 22578
22581 15267
9278 4127
7457 21674
17693 23448
10949 23429
9700 6009
14140 5064
7742 15164
17336 1662
18903 9760
17645 19575
6540 11942
11 4937
15282 10...

output:


result:

ok 0 lines

Test #2:

score: 0
Accepted
time: 25ms
memory: 9968kb

input:

10599 87159
4698 4000
60 4705
1476 5947
7273 1716
8004 3018
10094 1717
3092 3493
2613 9681
5427 3723
2510 6343
10113 6322
9257 634
4996 10197
9918 5582
6348 9561
10536 9777
8133 540
1917 7838
6666 2220
7951 815
2873 977
9397 4991
3529 1395
1426 3874
6379 9098
9504 3437
9076 9134
4321 3879
10252 9390...

output:


result:

ok 0 lines

Test #3:

score: -100
Runtime Error

input:

43236 126833
40500 20695
21481 27642
28098 41772
412 7750
5862 39561
21777 22303
7868 21217
31658 34299
18532 10934
21931 31023
42926 15624
40332 18017
12484 8663
21927 7910
12504 17943
4379 10252
3523 21794
18641 1965
18633 25061
14639 10800
35958 1441
35044 20249
31491 38161
5749 4468
13403 8413
3...

output:


result: