QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#480364 | #906. 强连通分量 | emsger | WA | 0ms | 3580kb | C++20 | 1.5kb | 2024-07-16 14:47:19 | 2024-07-16 14:47:19 |
Judging History
answer
#include <algorithm>
#include <iostream>
#include <vector>
using i64 = long long;
using i128 = __int128_t;
using u32 = unsigned int;
using u64 = unsigned long long;
using u128 = __uint128_t;
int main()
{
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < m; i++) {
int a, b;
std::cin >> a >> b;
adj[a].emplace_back(b);
}
std::vector<int> dfn(n, -1), low(n, -1);
int cnt = 0;
std::vector<int> stk;
std::vector<bool> instk(n);
int scc = 0;
std::vector<int> belong(n, -1);
auto dfs = [&](auto &&self, int u) -> void
{
dfn[u] = low[u] = cnt++;
stk.emplace_back(u);
instk[u] = true;
for (auto v : adj[u]) {
if (dfn[v] == -1) {
self(self, v);
low[u] = std::min(low[u], low[v]);
} else if (instk[v]) {
low[u] = std::min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
while (true) {
int x = stk.back();
stk.pop_back();
instk[x] = false;
belong[x] = scc;
if (x == u) break;
}
scc++;
}
};
for (int i = 0; i < n; i++) {
if (dfn[i] == -1) dfs(dfs, i);
}
std::vector<std::vector<int>> group(scc);
for (int i = 0; i < n; i++) {
group[belong[i]].emplace_back(i);
}
std::cout << scc << std::endl;
for (const auto &i : group) {
std::cout << i.size() << " ";
for (auto j : i) {
std::cout << j << " ";
}
std::cout << std::endl;
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3580kb
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)