QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#588200 | #906. 强连通分量 | shiqiaqiaya | WA | 446ms | 87856kb | C++17 | 3.6kb | 2024-09-25 06:23:36 | 2024-09-25 06:23:37 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define int long long
using ll = long long;
mt19937_64 rd(time(0));
template <class K, class C = less<>> using paring_heap = __gnu_pbds::priority_queue<K, C>;
template <class K, class V = null_type, class C = less<>> using rb_tree = tree<K, V, C, rb_tree_tag, tree_order_statistics_node_update>;
template <class T, class ... A> void debug(const T & t, const A & ... a) { cerr << "[" << t, ((cerr << ", " << a), ...), cerr << "]\n"; }
const ll mod = [](bool n) { return n ? 998244353 : 1e9 + 7; } (1);
template <ll mod = mod> ll binpow(ll a, ll b, ll res = 1) {
for (a %= mod; b; b >>= 1, (a *= a) %= mod) {
if (b & 1) (res *= a) %= mod;
}
return res;
}
template <class type = int, int len = 2> struct Tarjan_SC : vector<vector<signed>> {
vector<array<type, len>> e;
vector<signed> dfn, scc, low;
vector sc = {};
Tarjan_SC(int n, int m) : vector(n), e(m), dfn(n), scc(n), low(n) {}
void operator()(stack<signed> s = {}, int tot_dfn = 0) { // !! 会包含 0 , 可能 0 会成孤岛
vector<signed> vis(size());
for (int rt = 0; rt < size(); rt++) {
auto self = [&](auto && self, int u) -> void {
s.emplace(u), vis[u] = 1, dfn[u] = low[u] = ++tot_dfn;
for (auto & i : at(u)) {
int v = e[i][0] ^ e[i][1] ^ u;
if (!dfn[v]) self(self, v), low[u] = min(low[u], low[v]);
else if (vis[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) for (sc.emplace_back(); !sc.back().size() || sc.back().back() != u; s.pop()) {
scc[s.top()] = sc.size() - 1, vis[s.top()] = 0, sc.back().emplace_back(s.top());
}
};
if (!dfn[rt]) self(self, rt), s = {};
}
}
vector Topu_Rebuild() {
vector<signed> vis(size());
vector adj(sc.size());
for (int u = 0; u < sc.size(); u++) {
for (auto & tu : sc[u]) {
for (auto & i : at(tu)) {
int tv = e[i][0] ^ e[i][1] ^ tu, v = scc[tv];
if (v == u || vis[tv]) continue;
adj[u].emplace_back(v);
for (auto & t : sc[v]) {
vis[t] = 1;
}
}
}
}
return adj;
}
};
void QAQ() {
int n, m;
cin >> n >> m;
Tarjan_SC tar(n, m);
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v;
tar[u].push_back(i);
tar.e[i] = {u, v};
}
tar();
cout << tar.sc.size() << "\n";
auto adj = tar.Topu_Rebuild();
vector<signed> du(adj.size());
for (int u = 0; u < adj.size(); u++) {
for (auto & v : adj[u]) {
du[v]++;
}
}
queue<signed> q;
for (int u = 0; u < adj.size(); u++) {
if (du[u] == 0) q.emplace(u);
}
while (q.size()) {
auto u = q.front(); q.pop();
cout << tar.sc[u].size();
for (auto & v : tar.sc[u]) {
cout << " " << v;
}
cout << "\n";
for (auto & v : adj[u]) {
if (--du[v] == 0) q.emplace(v);
}
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int t = 1;
// cin >> t;
for (cout << fixed << setprecision(12); t--; QAQ());
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
input:
6 7 1 4 5 2 3 0 5 5 4 1 0 3 4 2
output:
4 2 3 0 2 4 1 1 5 1 2
result:
ok OK
Test #2:
score: -100
Wrong Answer
time: 446ms
memory: 87856kb
input:
500000 500000 389812 410922 255712 339244 274009 248522 347288 199231 235313 301629 469588 84917 364188 449407 392348 436920 26220 198288 162188 468965 274222 92196 463222 408478 231663 467768 382681 38083 412497 160479 280851 268689 101149 25450 62271 9177 38892 268598 273853 250782 191939 89247 40...
output:
499590 1 3 1 4 1 5 1 6 1 7 1 10 1 13 1 14 1 17 1 19 1 20 1 21 1 24 1 25 1 26 1 27 1 31 1 32 1 34 1 37 1 39 1 42 1 43 1 46 1 47 1 59 1 60 1 67 1 68 1 76 1 80 1 85 1 86 1 88 1 89 1 94 1 107 1 111 1 113 1 114 1 119 1 120 1 121 1 123 1 129 1 130 1 132 1 133 1 135 1 139 1 143 1 145 1 146 1 147 1 148 1 14...
result:
wrong answer 280851 is later than 268689, but there is an edge (280851, 268689)