tarjan但0pt(恼火)(蠕动)(大叫)

P8436 【模板】边双连通分量

LonginusMonkey @ 2023-07-21 15:56:38

5 7
4 2
5 4
4 2
3 2
1 2
1 1
2 1
3
1 3
1 5
3 1 2 4
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int> vec[500100];
vector<int> ans[500100];
bool vis[500100];
int sum = 0;
int low[500100], dfn[500100], ti = 0;
void dfs(int index, int back) {
    low[index] = dfn[index] = ++ti;
    for(int i=0; i<vec[index].size(); ++i) {
        if(vec[index][i] == back) continue;
        if(low[vec[index][i]]!=0) {
            low[index] = min(low[index], low[vec[index][i]]);
            continue;
        }
        dfs(vec[index][i], index);
        low[index] = min(low[index], low[vec[index][i]]);
    }
}
signed main() {
    ios::sync_with_stdio(0); cin.tie(0); 
    int n, m; cin >> n >> m;
    for(int i=1; i<=m; ++i) {
        int u, v; cin >> u >> v;
        vec[u].push_back(v), vec[v].push_back(u);
    }
    for(int i=1; i<=n; ++i) {
        if(!low[i]) {
            dfs(i, 0);
        }
    }
    for(int i=1; i<=n; ++i) {
        if(!vis[i]) {
            vis[i] = 1;
            sum++;
            ans[low[i]].push_back(i);
        }
    }
    cout << sum << endl;
    for(int i=1; i<=n; ++i) {
        if(ans[i].size() == 0) continue;
        cout << ans[i].size() << " ";
        for(int j=0; j<ans[i].size(); ++j) {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}

代码如上


by LonginusMonkey @ 2023-07-21 15:58:06

这不对啊,难道不是

5
1 1
1 2
1 3
1 4
1 5


by dyyzy @ 2023-08-03 17:36:58

有重边QAQ


by Masna_Kimoyo @ 2023-08-31 17:37:44

wc你都有hack数据了还不去自己调?

你这写法也是鬼怪的很啊,实在不会可以先去看题解


|