QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#549444 | #906. 强连通分量 | foolnine | WA | 0ms | 3604kb | C++20 | 1.5kb | 2024-09-06 15:36:04 | 2024-09-06 15:36:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<int>> e(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
e[u].push_back(v);
}
//f值相同的点属于同一个强连通分量
int dfncnt = 0, flag = 0;
vector<int> dfn(n + 1, 0), low(n + 1, 0), f(n + 1, 0);
stack<int> st;
auto tarjan = [&](auto self, int u) -> void {
st.push(u);
dfn[u] = low[u] = ++dfncnt;
for (auto v : e[u]) {
if (dfn[v] == 0) {
self(self, v);
low[u] = min(low[u], low[v]);
} else if (f[v] == 0) {
low[u] = min(low[u], low[v]);
}
}
if (dfn[u] == low[u]) {
++flag;
while (st.top() != u) {
f[st.top()] = flag;
st.pop();
}
st.pop();
f[u] = flag;
}
};
for (int i = 0; i < n; i++) {
if (dfn[i] == 0) {
tarjan(tarjan, i);
}
}
vector<vector<int>> ans(flag);
for (int i = 0; i < n; i++) {
ans[f[i] - 1].push_back(i);
}
cout << flag << endl;
for (auto vec : ans) {
cout << vec.size();
for (auto x : vec) {
cout << " " << x;
}
cout << endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3604kb
input:
6 7 1 4 5 2 3 0 5 5 4 1 0 3 4 2
output:
4 2 0 3 1 2 2 1 4 1 5
result:
wrong answer 5 is later than 2, but there is an edge (5, 2)