QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#564071 | #6413. Classical Graph Theory Problem | A_programmer | RE | 235ms | 51032kb | C++17 | 5.0kb | 2024-09-14 19:42:27 | 2024-09-14 19:42:27 |
Judging History
answer
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <cassert>
using namespace std;
typedef pair<int, int> pii;
const int maxn = 2e5 + 5;
bool vis[maxn];
set<int> g[maxn];
int nxt[maxn], n, m;
priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<int> h[maxn], vec, ansS, ansT, reS, reT, reC, dot;
vector<pii> p[maxn];
int bel[maxn];
void dfs(int u)
{
vis[u] = 1; dot.emplace_back(u);
for (auto [v, w] : p[u]) if (!vis[v]) dfs(v);
}
void work()
{
cin >> n >> m; vec.clear(); ansS.clear(), ansT.clear();
for (int i = 1; i <= n; i++) g[i].clear(), h[i].clear(), p[i].clear(), vis[i] = nxt[i] = bel[i] = 0;
for (int i = 1; i <= m; i++)
{
int u, v; cin >> u >> v;
g[u].insert(v); g[v].insert(u);
h[u].emplace_back(v), h[v].emplace_back(u);
}
while (pq.size()) pq.pop();
for (int i = 1; i <= n; i++)
if (g[i].size() == 1) pq.push(make_pair(g[i].size(), i));
while (pq.size())
{
int u = pq.top().second; pq.pop();
if (vis[u]) continue;
vis[u] = 1; bool Fl = false;
for (int v : g[u])
if (!vis[v])
{
vis[v] = 1;
Fl = true;
nxt[u] = v, nxt[v] = u;
for (int x : g[u]) if (x != v) g[x].erase(u);
for (int x : g[v]) if (x != u) g[x].erase(v);
break;
}
if (!Fl) vec.emplace_back(u);
}
for (int i = 1; i <= n; i++) if (!vis[i]) pq.push(make_pair(g[i].size(), i));
while (pq.size())
{
int u = pq.top().second; pq.pop();
if (vis[u]) continue;
vis[u] = 1; int pos = 0;
for (int v : g[u])
if (!vis[v])
{
if (!pos) pos = v;
else if (g[v].size() < g[pos].size()) pos = v;
}
if (!pos) { vec.emplace_back(u); continue; }
vis[pos] = 1; nxt[u] = pos, nxt[pos] = u;
for (int x : g[u]) if (!vis[x]) g[x].erase(u), pq.push(make_pair(g[x].size(), x));
for (int x : g[pos]) if (!vis[x]) g[x].erase(pos), pq.push(make_pair(g[x].size(), x));
}
for (int u : vec)
{
int pos1 = 0, pos2 = 0;
for (int v : h[u])
if (nxt[v])
{
if (!pos1) pos1 = pos2 = v;
else pos2 = v;
}
p[pos1].emplace_back(make_pair(pos2, u));
if (pos1 != pos2) p[pos2].emplace_back(make_pair(pos1, u));
}
for (int i = 1; i <= n; i++) vis[i] = 0;
for (int i = 1; i <= n; i++)
{
if (!nxt[i] || vis[i]) continue;
if (!p[i].size())
{
if (!p[nxt[i]].size()) bel[i] = 1, bel[nxt[i]] = 2, vis[i] = vis[nxt[i]] = 1;
continue;
}
reS.clear(), reT.clear(), reC.clear(), dot.clear(), dfs(i);
sort(dot.begin(), dot.end());
for (int u : dot)
{
int cnt1 = 0, cnt2 = 0;
for (auto [v, w] : p[u])
{
if (v > u) continue;
if (bel[v] == 1) cnt1++;
else if (bel[v] == 2) cnt2++;
}
if (cnt1 < cnt2 || (cnt1 == cnt2 && reS.size() < reT.size()))
{
bel[u] = 1;
for (auto [v, w] : p[u])
{
if (v > u) continue;
if (bel[v] == 1) reS.emplace_back(w);
else reC.emplace_back(w);
}
}
else
{
bel[u] = 2;
for (auto [v, w] : p[u])
{
if (v > u) continue;
if (bel[v] == 2) reT.emplace_back(w);
else reC.emplace_back(w);
}
}
}
while (reC.size())
{
if (reS.size() < reT.size()) reS.emplace_back(reC.back()), reC.pop_back();
else reT.emplace_back(reC.back()), reC.pop_back();
}
if ((ansS.size() < ansT.size()) ^ (reS.size() < reT.size()))
{
for (int x : reS) ansS.emplace_back(x);
for (int x : reT) ansT.emplace_back(x);
}
else
{
for (int u : dot) bel[u] = 3 - bel[u];
for (int x : reS) ansT.emplace_back(x);
for (int x : reT) ansS.emplace_back(x);
}
}
for (int i = 1; i <= n; i++)
if (nxt[i])
{
if (!bel[i]) bel[i] = 3 - bel[nxt[i]];
if (bel[i] == 1) ansT.emplace_back(i);
else ansS.emplace_back(i);
}
assert(abs((int)ansS.size() - (int)ansT.size()) <= 1);
if (ansS.size() == n / 2) for (int x : ansS) cout << x << " ";
else for (int x : ansT) cout << x << " "; cout << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T; cin >> T; while (T--) work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 23236kb
input:
2 6 7 1 2 1 3 2 3 3 4 4 5 4 6 5 6 3 2 1 2 2 3
output:
2 4 6 2
result:
ok ok (2 test cases)
Test #2:
score: 0
Accepted
time: 51ms
memory: 23328kb
input:
10000 2 1 1 2 29 28 13 19 16 5 21 7 22 10 10 2 1 18 27 13 10 3 11 23 12 22 11 7 7 17 29 17 9 1 28 21 2 18 13 9 4 25 20 16 5 14 20 7 14 4 12 8 8 24 17 19 15 1 11 6 26 9 13 12 13 9 12 2 6 12 9 11 5 2 8 10 6 10 3 10 7 1 7 5 8 9 4 1 12 11 10 6 2 8 12 4 5 10 11 1 3 1 10 1 12 9 9 1 8 3 7 1 35 35 13 8 34 1...
output:
2 23 5 6 7 10 13 15 18 22 24 25 26 28 29 8 1 2 3 9 12 9 6 1 2 4 5 5 26 3 9 13 15 18 23 24 28 29 30 31 32 33 34 35 8 18 4 7 10 11 15 16 17 2 3 4 26 32 45 4 7 10 11 13 15 22 24 27 28 31 35 37 40 42 43 44 46 47 48 49 50 21 27 35 30 1 7 8 12 13 15 18 19 23 26 29 31 33 36 12 14 3 4 5 6 9 6 2 5...
result:
ok ok (10000 test cases)
Test #3:
score: 0
Accepted
time: 58ms
memory: 23724kb
input:
1000 337 338 164 11 138 75 114 262 170 298 166 241 269 24 9 134 233 60 50 222 231 253 296 242 173 18 93 223 116 151 312 150 82 236 180 20 297 184 268 70 334 162 217 135 258 321 80 209 212 208 18 163 227 104 334 135 77 118 17 230 307 105 307 335 29 24 111 177 324 24 85 3 214 191 310 182 22 171 202 21...
output:
188 324 293 122 310 319 257 178 267 285 72 265 228 286 78 329 79 194 39 2 4 11 12 19 21 24 28 30 31 37 40 41 42 46 51 52 62 63 67 75 76 80 85 90 99 102 104 105 108 110 117 118 119 120 121 124 126 128 131 132 137 142 145 146 148 149 151 152 153 156 160 161 167 169 170 172 173 177 180 181 182 183 186 ...
result:
ok ok (1000 test cases)
Test #4:
score: 0
Accepted
time: 73ms
memory: 25652kb
input:
100 1038 1044 206 546 372 853 526 57 777 72 645 866 15 716 254 707 366 753 635 809 850 407 616 149 839 175 320 770 649 686 857 798 1027 40 988 566 315 500 187 615 100 523 867 708 51 381 858 9 177 55 310 54 355 215 78 26 740 570 523 797 828 693 930 981 208 185 663 957 298 523 235 496 622 174 285 247 ...
output:
1003 490 502 300 832 713 826 381 847 877 309 821 946 976 461 298 985 547 335 303 295 546 487 220 923 346 274 656 814 774 752 741 675 199 938 630 89 311 824 137 474 486 538 1018 936 951 481 92 952 455 179 842 928 214 1019 764 519 244 468 829 958 376 984 998 227 101 592 395 879 2 6 12 13 23 33 40 42 4...
result:
ok ok (100 test cases)
Test #5:
score: 0
Accepted
time: 134ms
memory: 36356kb
input:
10 1380 1393 960 647 1319 708 57 1128 751 148 1291 602 835 921 942 406 622 616 967 91 555 545 871 10 447 471 1140 306 149 121 587 165 1179 936 256 787 332 374 729 129 631 481 976 86 1128 1300 477 776 460 313 538 632 1210 275 355 470 1324 885 870 1325 389 979 468 532 41 416 1026 243 1153 152 948 323 ...
output:
994 1203 663 599 557 1041 1296 399 1117 226 1353 1360 14 997 859 299 1224 1141 675 1336 888 973 661 571 639 1088 765 1277 618 99 224 1033 1036 1256 1164 1245 669 736 1380 1284 880 488 500 1303 498 513 244 1251 958 1322 1216 1016 1142 227 915 1144 1037 934 938 1075 412 999 450 606 770 1337 1068 608 4...
result:
ok ok (10 test cases)
Test #6:
score: 0
Accepted
time: 235ms
memory: 51032kb
input:
1 200000 201978 69113 28513 94227 164392 56849 195513 22579 149089 195084 193248 121765 162768 135432 101508 107443 89723 12337 87598 173450 107835 13160 161882 18965 179808 53739 23609 114567 23456 195251 178048 61586 87664 179364 25594 90158 169714 30104 161354 143346 4279 177208 87389 122480 1269...
output:
94666 113824 152297 103629 108034 83423 67047 46414 131998 159722 103755 150601 184869 108316 169791 155708 54936 9397 129014 164346 145493 73093 88455 164584 193898 101131 45635 132634 181133 135384 8399 162939 42818 144044 187255 155685 9819 133004 195970 191893 58863 37585 90745 89146 69480 16763...
result:
ok ok (1 test case)
Test #7:
score: 0
Accepted
time: 60ms
memory: 23312kb
input:
10000 41 44 18 29 38 6 7 4 34 27 40 37 12 40 18 38 11 18 30 39 2 21 10 34 33 2 8 12 30 23 6 2 12 21 15 7 17 1 36 15 31 36 15 21 38 31 1 11 4 30 16 33 19 32 21 30 32 35 1 3 27 9 1 34 11 5 26 25 22 5 34 24 23 32 28 2 20 33 13 15 31 21 38 41 26 3 13 14 14 33 11 11 3 1 9 11 6 3 8 1 7 2 4 3 10 2 9 2 5 4 ...
output:
6 35 20 1 5 7 12 14 16 19 21 25 27 28 29 30 34 36 38 40 2 5 6 8 11 5 7 9 11 13 14 15 16 33 11 14 17 19 21 22 23 24 25 26 27 28 30 31 32 34 5 6 7 8 2 5 1 6 8 10 11 12 16 17 7 2 6 8 13 5 2 4 9 10 11 4 6 7 8 4 2 6 4 6 7 8 2 4 5 6 29 3 8 9 10 11 12 14 15 20 23 24 27 28 17 5 7 8 11 13 15 1...
result:
ok ok (10000 test cases)
Test #8:
score: 0
Accepted
time: 71ms
memory: 23272kb
input:
10000 11 13 6 3 9 4 10 4 9 6 10 7 1 5 2 11 2 8 10 6 2 9 6 7 2 5 5 11 3 2 2 1 2 3 2 1 2 1 12 14 12 11 10 7 5 6 2 5 5 8 8 3 8 1 3 12 12 7 2 10 10 11 6 4 11 2 9 3 4 4 1 2 1 3 4 3 2 3 11 13 3 7 1 5 1 6 8 5 9 7 1 2 1 11 2 4 10 9 10 1 7 2 8 3 8 6 2 1 1 2 15 18 3 11 2 10 7 14 14 4 7 3 6 11 15 12 5 11 2 7 7...
output:
1 2 6 9 10 2 2 5 6 8 9 10 12 2 4 1 4 5 7 10 2 13 1 8 9 10 11 14 61 8 10 13 14 15 20 21 22 26 27 31 33 37 39 42 43 45 46 49 50 51 53 55 56 57 58 59 60 62 63 65 66 6 7 9 10 11 13 8 2 5 6 38 8 11 12 15 16 17 20 23 24 25 27 30 31 32 34 35 37 39 81 46 2 9 10 11 21 29 33 37 38 40 41 42 43 44 4...
result:
ok ok (10000 test cases)
Test #9:
score: -100
Runtime Error
input:
10000 10 14 4 9 5 10 1 10 7 6 8 6 9 6 8 3 8 7 4 6 5 3 10 4 10 2 4 8 1 9 6 8 1 2 5 2 5 1 3 4 5 3 6 5 2 3 3 1 3 3 2 1 3 2 3 1 18 26 18 3 10 11 2 4 17 4 8 12 14 15 1 12 13 12 15 7 13 15 14 2 17 5 1 13 11 16 9 3 13 9 6 12 11 14 3 4 3 11 7 11 8 2 8 4 15 6 12 10 12 18 24 35 18 4 22 10 1 21 22 6 23 7 6 14 ...
output:
5 6 8 9 10 2 4 6 1 18 8 9 12 13 14 15 16 17 11 13 14 15 16 17 19 20 21 22 23 24 66 4 6 17 24 29 31 33 34 35 38 39 40 42 43 44 45 46 48 50 51 53 54 55 56 58 59 60 61 62 63 64 65 6 7 8 9 11 12 2 7 11 14 17 18 20 22 23 25 26 27 28 31 32 33 1 4 6 7 5 6 8 9 10 13 72 78 77 5 8 11 16 26 28 29 33 ...