QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#549440 | #906. 强连通分量 | foolnine | WA | 0ms | 3616kb | C++20 | 1.5kb | 2024-09-06 15:34:50 | 2024-09-06 15:34:50 |
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);
}
for (auto vec : ans) {
cout << vec.size();
for (auto x : vec) {
cout << " " << x;
}
cout << endl;
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3616kb
input:
6 7 1 4 5 2 3 0 5 5 4 1 0 3 4 2
output:
2 0 3 1 2 2 1 4 1 5
result:
wrong answer Integer 0 violates the range [1, 6]