QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#857375 | #9875. Don't Detect Cycle | hos_lyric | TL | 1425ms | 4920kb | C++14 | 6.9kb | 2025-01-15 16:43:43 | 2025-01-15 16:43:45 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
// gg: bipartite graph between {vertex} and {biconnected component}
// (|gg| - n) biconnected components
// isolated point: not regarded as biconnected component (==> isolated in gg)
// f: DFS out-forest
// ess: edges in biconnected component
// (u, v) with dis[u] <= dis[v]
// self-loop at isolated point: not included in ess
struct Biconnected {
int n, m;
vector<vector<int>> g, f, gg;
vector<vector<pair<int, int>>> ess;
vector<int> par, rs;
int zeit;
vector<int> dis, fin, low;
Biconnected() {}
explicit Biconnected(int n_) : n(n_), m(0), g(n_) {}
void ae(int u, int v) {
++m;
assert(0 <= u); assert(u < n);
assert(0 <= v); assert(v < n);
g[u].push_back(v);
if (u != v) g[v].push_back(u);
}
int stackVLen, stackELen;
vector<int> stackV;
vector<pair<int, int>> stackE;
vector<int> cntPar;
void dfs(int u) {
stackV[stackVLen++] = u;
dis[u] = low[u] = zeit++;
for (const int v : g[u]) {
if (par[u] == v && !cntPar[u]++) continue;
if (~dis[v]) {
if (dis[u] >= dis[v]) stackE[stackELen++] = std::make_pair(v, u);
if (low[u] > dis[v]) low[u] = dis[v];
} else {
f[u].push_back(v);
par[v] = u;
rs[v] = rs[u];
const int stackEPos = stackELen;
stackE[stackELen++] = std::make_pair(u, v);
dfs(v);
if (low[u] > low[v]) low[u] = low[v];
if (dis[u] <= low[v]) {
const int x = gg.size();
gg.emplace_back();
ess.emplace_back();
for (; ; ) {
const int w = stackV[--stackVLen];
gg[w].push_back(x);
gg[x].push_back(w);
if (w == v) break;
}
gg[u].push_back(x);
gg[x].push_back(u);
for (; stackELen > stackEPos; ) ess[x].push_back(stackE[--stackELen]);
}
}
}
fin[u] = zeit;
}
void build() {
f.assign(n, {});
gg.assign(n, {});
ess.assign(n, {});
par.assign(n, -1);
rs.assign(n, -1);
zeit = 0;
dis.assign(n, -1);
fin.assign(n, -1);
low.assign(n, -1);
stackV.resize(n);
stackE.resize(m);
cntPar.assign(n, 0);
for (int u = 0; u < n; ++u) if (!~dis[u]) {
stackVLen = stackELen = 0;
rs[u] = u;
dfs(u);
}
}
// Returns true iff u is an articulation point
// <=> # of connected components increases when u is removed.
inline bool isArt(int u) const {
return (gg[u].size() >= 2);
}
// Returns w s.t. w is a child of u and a descendant of v in the DFS forest.
// Returns -1 instead if v is not a proper descendant of u
// O(log(deg(u))) time
int dive(int u, int v) const {
if (dis[u] < dis[v] && dis[v] < fin[u]) {
int j0 = 0, j1 = f[u].size();
for (; j0 + 1 < j1; ) {
const int j = (j0 + j1) / 2;
((dis[f[u][j]] <= dis[v]) ? j0 : j1) = j;
}
return f[u][j0];
} else {
return -1;
}
}
// Returns true iff there exists a v-w path when u is removed.
// O(log(deg(u))) time
bool isStillReachable(int u, int v, int w) const {
assert(0 <= u); assert(u < n);
assert(0 <= v); assert(v < n);
assert(0 <= w); assert(w < n);
assert(u != v);
assert(u != w);
if (rs[v] != rs[w]) return false;
if (rs[u] != rs[v]) return true;
const int vv = dive(u, v);
const int ww = dive(u, w);
if (~vv) {
if (~ww) {
return (vv == ww || (dis[u] > low[vv] && dis[u] > low[ww]));
} else {
return (dis[u] > low[vv]);
}
} else {
if (~ww) {
return (dis[u] > low[ww]);
} else {
return true;
}
}
}
};
////////////////////////////////////////////////////////////////////////////////
int N, M;
vector<int> A, B;
bool solve() {
vector<int> ans(M, -1);
vector<int> del(M, 0);
for (int h = M; --h >= 0; ) {
Biconnected bi(N);
for (int i = 0; i < M; ++i) if (!del[i]) bi.ae(A[i], B[i]);
bi.build();
// adj. biconn. comp. with cycle
vector<int> xs(N, -1);
for (int u = 0; u < N; ++u) {
for (const int x : bi.gg[u]) if (bi.ess[x].size() >= 2) {
xs[u] = (~xs[u]) ? -2 : x;
}
}
// cerr<<"gg = "<<bi.gg<<", xs = "<<xs<<endl;
pair<int, int> em(-1, -1);
vector<int> deg(N, 0);
for (int x = N; x < (int)bi.gg.size(); ++x) {
for (const auto &e : bi.ess[x]) {
++deg[e.first];
++deg[e.second];
}
auto check = [&](int u) -> bool {
if (~xs[u] && xs[u] != x) return false;
if (deg[u] >= 3) return false;
return true;
};
for (const auto &e : bi.ess[x]) {
if (check(e.first) && check(e.second)) {
em = e;
}
}
for (const auto &e : bi.ess[x]) {
--deg[e.first];
--deg[e.second];
}
}
for (int i = 0; i < M; ++i) if (!del[i]) {
if (em == make_pair(A[i], B[i]) || em == make_pair(B[i], A[i])) {
ans[h] = i;
del[i] = 1;
goto found;
}
}
return false;
found:{}
}
for (int h = 0; h < M; ++h) {
if (h) printf(" ");
printf("%d", ans[h] + 1);
}
puts("");
return true;
}
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d%d", &N, &M);
A.resize(M);
B.resize(M);
for (int i = 0; i < M; ++i) {
scanf("%d%d", &A[i], &B[i]);
--A[i];
--B[i];
}
if (!solve()) {
puts("-1");
}
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3840kb
input:
1 4 4 1 2 2 3 3 4 4 2
output:
3 4 1 2
result:
ok Correct
Test #2:
score: 0
Accepted
time: 1ms
memory: 3840kb
input:
4 4 5 1 2 2 3 3 4 3 1 1 4 5 3 1 2 2 3 3 4 9 10 3 5 1 8 5 8 4 9 6 7 7 9 1 2 1 4 2 4 4 6 8 10 1 4 3 8 2 5 3 4 1 5 5 8 2 8 5 7 4 5 3 7
output:
-1 3 2 1 3 1 2 6 5 10 9 8 4 7 -1
result:
ok Correct
Test #3:
score: 0
Accepted
time: 1425ms
memory: 4880kb
input:
50 3214 2907 970 1929 2860 3033 1322 2296 931 1192 861 2505 831 2469 231 2549 1 2306 1765 1842 999 3171 177 2007 1798 1894 827 3180 673 1738 1163 1573 2213 2781 2766 3200 1663 2197 1797 2281 315 2637 442 2689 558 2874 1520 2591 651 1923 1133 2920 1747 2412 1104 1528 313 2487 632 3124 660 2182 1581 2...
output:
593 8 283 109 881 959 126 1585 1972 1985 768 904 2779 1690 1547 2110 614 2774 788 1629 606 2196 892 1586 2761 1859 532 321 2313 1959 2006 1337 1792 1923 2070 520 1478 835 1885 1233 2457 689 1115 79 359 1260 165 981 1141 2782 77 721 1682 696 458 661 2665 1080 636 2256 274 1371 2185 2821 2569 2770 153...
result:
ok Correct
Test #4:
score: 0
Accepted
time: 21ms
memory: 4276kb
input:
48 732 104 388 425 176 558 7 695 504 507 163 705 204 456 139 432 104 716 535 582 254 682 70 278 77 385 600 680 373 564 197 653 335 569 81 579 339 604 407 580 253 383 480 549 145 308 52 373 426 525 268 359 408 595 47 397 479 569 268 403 477 663 434 660 330 343 56 692 376 450 200 553 299 713 114 584 1...
output:
3 48 101 57 66 72 53 27 63 14 23 98 33 71 79 11 12 17 69 97 99 52 43 8 58 38 28 16 42 37 94 56 103 55 75 73 34 74 82 47 54 7 81 44 22 64 5 49 2 92 59 102 50 80 15 93 35 88 77 6 83 62 60 20 10 40 100 1 51 87 104 25 29 95 70 61 41 36 78 32 91 65 18 84 39 85 76 19 26 89 96 24 31 46 67 90 30 21 4 9 68 4...
result:
ok Correct
Test #5:
score: 0
Accepted
time: 1115ms
memory: 4920kb
input:
24 3635 2454 724 2161 994 3233 30 278 2047 3627 693 1048 112 2609 9 1552 889 946 987 2538 923 1911 53 1198 2429 3200 1338 3544 504 2644 1116 3446 815 877 245 3601 2177 3180 212 1638 1140 3241 159 2455 2447 2460 957 1585 980 2338 1254 3014 382 3596 510 595 1408 2300 2053 2276 2177 3415 1051 3353 136 ...
output:
308 414 416 388 1634 1346 1771 7 742 1937 1059 2161 1498 144 697 2254 26 596 1287 1171 1807 2114 371 631 2292 80 519 2054 1511 75 1451 865 266 1742 637 1299 1703 1600 1175 1376 1947 1449 2074 2424 539 1469 1100 1441 1591 43 920 1564 1300 1611 901 57 740 1838 1760 2169 964 1520 74 610 507 2219 552 43...
result:
ok Correct
Test #6:
score: 0
Accepted
time: 531ms
memory: 4736kb
input:
56 2367 1768 132 2148 1280 2214 473 2270 78 2126 374 2080 777 1617 74 152 46 125 36 1136 1340 2010 1536 1801 291 619 610 1567 1688 2303 1005 2308 1101 1988 1695 2257 1056 1405 1134 1579 1819 2281 1281 1952 2065 2102 1984 2353 215 1994 984 2258 1916 2059 1128 2198 966 1048 965 1424 866 932 227 543 33...
output:
1049 1609 1274 123 425 762 478 147 28 325 676 798 1439 86 1184 151 1348 866 904 117 978 1186 488 1524 358 163 938 413 513 1419 1592 1243 520 331 601 1413 862 1740 266 625 666 860 953 1259 1741 267 7 1084 1279 1564 130 427 1122 497 1141 579 1223 215 149 429 17 1745 566 521 682 594 291 778 1471 705 14...
result:
ok Correct
Test #7:
score: 0
Accepted
time: 310ms
memory: 4608kb
input:
56 1804 2031 215 520 41 228 505 1449 1202 1467 175 474 583 1684 127 1013 11 1132 251 1009 1333 1516 22 633 168 1160 866 1584 1501 1510 425 1494 563 1764 1341 1646 76 114 541 943 163 166 103 184 455 1225 708 1649 836 1551 551 1381 570 1509 125 221 371 1117 436 1012 392 732 76 379 1040 1359 119 1405 1...
output:
-1 7 15 12 10 13 8 3 6 1 14 11 18 17 16 4 5 9 2 7 5 2 16 18 3 10 14 8 20 9 13 6 1 12 4 11 19 15 17 19 11 9 10 12 17 5 23 14 21 3 18 6 15 2 13 7 16 25 4 1 20 24 22 8 9 17 6 19 1 18 16 12 4 10 20 7 8 3 5 11 21 13 15 2 14 19 16 2 17 18 6 14 1 8 7 20 12 4 13 10 15 9 11 3 5 9 13 2 14 15 1 19 16 8 5 7 18 ...
result:
ok Correct
Test #8:
score: 0
Accepted
time: 2ms
memory: 3840kb
input:
38 17 122 7 11 1 8 2 13 5 6 6 10 9 17 6 13 10 12 2 9 12 14 14 15 3 8 8 12 3 16 3 17 6 16 5 12 4 11 11 16 5 13 5 17 1 4 1 10 8 15 2 16 3 10 6 7 5 7 2 17 10 17 7 12 3 6 9 11 6 17 4 6 9 16 1 16 12 15 7 17 9 10 1 5 10 15 7 10 3 13 1 14 8 14 4 5 4 17 1 17 8 17 7 8 1 2 10 13 11 15 15 16 2 12 2 11 3 7 8 9 ...
output:
-1 -1 -1 -1 -1 7 15 12 10 13 8 3 6 1 14 11 18 17 16 4 5 9 2 7 5 2 16 18 3 10 14 8 20 9 13 6 1 12 4 11 19 15 17 19 11 9 10 12 17 5 23 14 21 3 18 6 15 2 13 7 16 25 4 1 20 24 22 8 9 17 6 19 1 18 16 12 4 10 20 7 8 3 5 11 21 13 15 2 14 19 16 2 17 18 6 14 1 8 7 20 12 4 13 10 15 9 11 3 5 9 13 2 14 15 1 19 ...
result:
ok Correct
Test #9:
score: 0
Accepted
time: 4ms
memory: 3968kb
input:
61 12 66 11 12 5 8 9 12 4 9 2 9 6 12 2 11 1 2 3 6 3 12 6 10 5 6 2 12 10 12 8 12 7 8 7 9 2 8 3 11 3 9 3 10 8 11 2 6 5 12 5 9 4 7 4 5 4 6 5 11 1 3 5 7 1 7 7 10 5 10 6 7 4 12 3 5 4 8 2 3 1 8 6 11 4 11 3 7 1 5 3 4 9 11 1 10 4 10 6 9 7 11 1 4 8 9 10 11 1 11 7 12 1 9 9 10 1 12 6 8 8 10 2 10 2 5 3 8 2 7 1 ...
output:
-1 -1 -1 -1 -1 -1 7 15 12 10 13 8 3 6 1 14 11 18 17 16 4 5 9 2 7 5 2 16 18 3 10 14 8 20 9 13 6 1 12 4 11 19 15 17 19 11 9 10 12 17 5 23 14 21 3 18 6 15 2 13 7 16 25 4 1 20 24 22 8 9 17 6 19 1 18 16 12 4 10 20 7 8 3 5 11 21 13 15 2 14 19 16 2 17 18 6 14 1 8 7 20 12 4 13 10 15 9 11 3 5 9 13 2 14 15 1 ...
result:
ok Correct
Test #10:
score: 0
Accepted
time: 2ms
memory: 3968kb
input:
18 51 1255 24 43 42 51 4 36 29 31 41 42 43 48 10 26 30 40 4 51 25 42 24 42 2 6 3 24 6 21 34 46 5 10 2 37 12 41 19 25 1 2 18 22 1 20 45 49 3 22 14 25 16 25 26 31 25 48 36 45 24 29 34 39 26 29 6 37 18 38 2 51 10 22 15 26 30 33 1 15 10 37 17 33 11 22 28 32 32 39 13 17 21 28 8 23 20 46 8 38 5 44 5 30 4 ...
output:
-1 -1 -1 -1 -1 -1 -1 -1 7 15 12 10 13 8 3 6 1 14 11 18 17 16 4 5 9 2 7 5 2 16 18 3 10 14 8 20 9 13 6 1 12 4 11 19 15 17 19 11 9 10 12 17 5 23 14 21 3 18 6 15 2 13 7 16 25 4 1 20 24 22 8 9 17 6 19 1 18 16 12 4 10 20 7 8 3 5 11 21 13 15 2 14 19 16 2 17 18 6 14 1 8 7 20 12 4 13 10 15 9 11 3 5 9 13 2 14...
result:
ok Correct
Test #11:
score: 0
Accepted
time: 3ms
memory: 3968kb
input:
61 22 223 1 22 10 22 2 7 19 20 13 17 17 21 18 19 15 16 9 17 5 19 5 8 12 18 4 17 10 20 2 10 4 15 7 11 16 19 5 20 3 14 3 17 7 12 3 21 4 11 17 22 10 17 8 21 9 20 6 11 2 20 5 7 3 18 9 22 13 22 6 14 14 19 5 12 4 22 2 3 14 17 12 16 7 20 5 10 4 7 4 13 1 19 10 13 1 20 13 19 4 6 11 19 3 11 9 14 8 15 3 16 2 8...
output:
-1 -1 -1 -1 -1 -1 -1 7 15 12 10 13 8 3 6 1 14 11 18 17 16 4 5 9 2 7 5 2 16 18 3 10 14 8 20 9 13 6 1 12 4 11 19 15 17 19 11 9 10 12 17 5 23 14 21 3 18 6 15 2 13 7 16 25 4 1 20 24 22 8 9 17 6 19 1 18 16 12 4 10 20 7 8 3 5 11 21 13 15 2 14 19 16 2 17 18 6 14 1 8 7 20 12 4 13 10 15 9 11 3 5 9 13 2 14 15...
result:
ok Correct
Test #12:
score: -100
Time Limit Exceeded
input:
1 4000 4000 1248 3248 260 3260 344 1017 843 3949 451 1483 275 1413 231 3477 264 940 567 1383 1072 3173 830 3445 437 2322 929 1624 1221 2034 3297 3458 1412 1642 837 2505 1918 3259 554 2070 3630 3807 1217 3188 3149 3199 949 1179 2697 3656 802 2039 2496 3757 1073 2857 765 2310 178 3862 1385 2597 1870 2...