QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#857376 | #9875. Don't Detect Cycle | hos_lyric | TL | 1229ms | 4760kb | C++14 | 6.9kb | 2025-01-15 16:47:47 | 2025-01-15 16:47:48 |
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;
break;
}
}
for (const auto &e : bi.ess[x]) {
--deg[e.first];
--deg[e.second];
}
if (~em.first) break;
}
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:
1 2 3 4
result:
ok Correct
Test #2:
score: 0
Accepted
time: 0ms
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 1 2 3 7 9 10 4 2 8 6 5 3 1 -1
result:
ok Correct
Test #3:
score: 0
Accepted
time: 1229ms
memory: 4760kb
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:
1852 765 575 358 759 2677 1367 2593 1273 1324 579 149 1602 2409 723 2130 349 1058 312 920 418 2226 1580 166 2352 1565 1688 707 2882 2883 2814 2361 633 2722 1184 194 2204 1841 2050 1555 278 1677 2161 521 1232 1344 610 1374 1097 2322 42 2305 2198 919 486 519 1671 2840 2162 1819 629 360 1279 2822 744 1...
result:
ok Correct
Test #4:
score: 0
Accepted
time: 23ms
memory: 4388kb
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:
86 13 45 68 9 4 21 30 90 67 46 31 24 96 89 26 19 76 85 39 84 18 65 91 32 78 36 41 61 70 95 29 25 104 87 51 1 100 40 10 20 60 62 83 6 77 88 35 93 15 80 50 102 59 92 2 49 5 64 22 44 81 7 54 47 82 74 34 73 75 55 103 56 94 37 42 16 28 38 58 8 43 52 99 97 69 17 12 11 79 71 33 98 23 14 63 27 53 72 66 57 1...
result:
ok Correct
Test #5:
score: 0
Accepted
time: 1063ms
memory: 4676kb
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:
1896 1083 811 501 1157 1198 184 557 1834 2206 1285 235 1128 1169 367 1859 957 720 1762 1670 12 658 1269 1150 1428 55 479 384 623 497 285 1242 66 1232 1580 1663 1207 198 1525 840 2002 642 835 2250 560 209 130 941 83 1307 1039 1863 1024 717 211 1007 241 1484 1987 378 425 761 1743 1467 1245 1975 773 15...
result:
ok Correct
Test #6:
score: 0
Accepted
time: 503ms
memory: 4352kb
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:
434 651 348 1495 1725 286 1352 635 1767 704 812 864 1566 1030 537 589 454 543 1115 244 834 217 800 1021 1363 1676 909 352 706 784 87 912 1260 934 1405 1458 1004 25 1752 1007 433 485 1527 122 1463 504 1335 528 322 1491 1155 257 928 877 983 1576 878 182 1438 620 180 817 1547 1263 576 1099 985 600 970 ...
result:
ok Correct
Test #7:
score: 0
Accepted
time: 275ms
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 9 4 3 13 10 8 12 2 15 14 16 17 11 1 18 5 6 17 10 18 6 13 9 16 12 1 2 4 11 19 15 5 7 8 3 14 20 21 5 3 17 12 10 14 23 19 11 9 18 22 8 20 2 13 7 16 4 15 25 1 24 6 15 2 19 20 1 13 4 14 11 6 7 8 3 18 16 10 5 12 21 17 9 11 6 14 18 17 2 19 16 3 13 10 9 5 8 4 7 20 1 12 15 15 14 9 12 3 20 4 1 13 7 17 11...
result:
ok Correct
Test #8:
score: 0
Accepted
time: 2ms
memory: 3968kb
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 9 4 3 13 10 8 12 2 15 14 16 17 11 1 18 5 6 17 10 18 6 13 9 16 12 1 2 4 11 19 15 5 7 8 3 14 20 21 5 3 17 12 10 14 23 19 11 9 18 22 8 20 2 13 7 16 4 15 25 1 24 6 15 2 19 20 1 13 4 14 11 6 7 8 3 18 16 10 5 12 21 17 9 11 6 14 18 17 2 19 16 3 13 10 9 5 8 4 7 20 1 12 15 15 14 9 12 3 20 4 ...
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 9 4 3 13 10 8 12 2 15 14 16 17 11 1 18 5 6 17 10 18 6 13 9 16 12 1 2 4 11 19 15 5 7 8 3 14 20 21 5 3 17 12 10 14 23 19 11 9 18 22 8 20 2 13 7 16 4 15 25 1 24 6 15 2 19 20 1 13 4 14 11 6 7 8 3 18 16 10 5 12 21 17 9 11 6 14 18 17 2 19 16 3 13 10 9 5 8 4 7 20 1 12 15 15 14 9 12 3 20...
result:
ok Correct
Test #10:
score: 0
Accepted
time: 1ms
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 9 4 3 13 10 8 12 2 15 14 16 17 11 1 18 5 6 17 10 18 6 13 9 16 12 1 2 4 11 19 15 5 7 8 3 14 20 21 5 3 17 12 10 14 23 19 11 9 18 22 8 20 2 13 7 16 4 15 25 1 24 6 15 2 19 20 1 13 4 14 11 6 7 8 3 18 16 10 5 12 21 17 9 11 6 14 18 17 2 19 16 3 13 10 9 5 8 4 7 20 1 12 15 15 14 9 1...
result:
ok Correct
Test #11:
score: 0
Accepted
time: 4ms
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 9 4 3 13 10 8 12 2 15 14 16 17 11 1 18 5 6 17 10 18 6 13 9 16 12 1 2 4 11 19 15 5 7 8 3 14 20 21 5 3 17 12 10 14 23 19 11 9 18 22 8 20 2 13 7 16 4 15 25 1 24 6 15 2 19 20 1 13 4 14 11 6 7 8 3 18 16 10 5 12 21 17 9 11 6 14 18 17 2 19 16 3 13 10 9 5 8 4 7 20 1 12 15 15 14 9 12 3...
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...