QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#791451 | #906. 强连通分量 | Mr_Min | WA | 0ms | 3592kb | C++14 | 1.2kb | 2024-11-28 18:43:26 | 2024-11-28 18:43:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector < vector < int > > es(n), res(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
es[u].push_back(v);
res[v].push_back(u);
}
vector < int > d(n), b(n, -1), ord;
auto dfs1 = [&](auto &self, int u) -> void {
if (d[u]) return ;
d[u] = 1;
for (int v : es[u]) self(self, v);
ord.push_back(u);
};
auto dfs2 = [&](auto &self, int u, int bv) -> void {
if (~b[u]) return ;
b[u] = bv;
for (int v : res[u]) self(self, v, bv);
};
for (int i = 0; i < n; i++)
if (!d[i]) dfs1(dfs1, i);
reverse(ord.begin(), ord.end());
for (int i : ord)
if (!~b[i]) dfs2(dfs2, i, i);
vector < vector < int > > tans(n);
for (int i = 0; i < n; i++) tans[b[i]].push_back(i);
vector < vector < int > > ans;
for (int i = 0; i < n; i++)
if (!tans[i].empty()) {
ans.push_back({});
for (int j : tans[i]) ans.back().push_back(j);
}
cout << ans.size() << '\n';
for (auto it : ans) {
cout << it.size() << ' ';
for (int i : it) cout << i << ' ';
cout << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3592kb
input:
6 7 1 4 5 2 3 0 5 5 4 1 0 3 4 2
output:
4 2 0 3 2 1 4 1 2 1 5
result:
wrong answer 5 is later than 2, but there is an edge (5, 2)