QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#765578 | #906. 强连通分量 | zhenghanyun# | WA | 1ms | 7736kb | C++14 | 1.4kb | 2024-11-20 14:42:42 | 2024-11-20 14:42:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5 + 5;
struct edge {
int to, Next;
edge() {}
edge(int to, int Next): to(to), Next(Next) {}
} G[N << 1];
int n, m, head[N], cnt;
inline void add_edge(int u, int v) {
G[++cnt] = edge(v, head[u]);
head[u] = cnt;
}
stack <int> st;
int dfn[N], low[N], tot;
bitset <N> vis;
vector <vector <int> > ans;
inline void Tarjan(int u) {
st.push(u);
vis[u] = true;
dfn[u] = low[u] = ++tot;
for (int i = head[u]; i; i = G[i].Next) {
int v = G[i].to;
if (!dfn[v]) {
Tarjan(v);
low[u] = min(low[u], low[v]);
} else if (vis[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
vector <int> vec;
while (!st.empty()) {
int v = st.top();
st.pop();
vis[v] = false;
vec.emplace_back(v);
if (u == v) {
break;
}
}
ans.emplace_back(vec);
}
}
int main() {
#ifdef LOCAL
assert(freopen("test.in", "r", stdin));
assert(freopen("test.out", "w", stdout));
#endif
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m;
while (m--) {
int u, v;
cin >> u >> v;
add_edge(u, v);
}
for (int i = 0; i < n; ++i) {
if (!dfn[i]) {
Tarjan(i);
}
}
cout << ans.size() << "\n";
for (auto vec: ans) {
cout << vec.size() << " ";
for (auto u: vec) {
cout << u << " ";
}
cout << "\n";
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 7736kb
input:
6 7 1 4 5 2 3 0 5 5 4 1 0 3 4 2
output:
4 2 3 0 1 2 2 4 1 1 5
result:
wrong answer 5 is later than 2, but there is an edge (5, 2)