QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#408232 | #6421. Degree of Spanning Tree | Nelofus# | AC ✓ | 162ms | 14184kb | C++20 | 3.7kb | 2024-05-09 21:31:16 | 2024-05-09 21:31:17 |
Judging History
answer
// Code by Heratino & Nelofus
// Narcissus & どうか安寧な記憶を
#include <bits/stdc++.h>
using i64 = long long;
//{{{星光闪耀
template<typename Ta, typename Tb>
inline void chkmax(Ta &a, const Tb &b) {if (a < b) a = b;}
template<typename Ta, typename Tb>
inline void chkmin(Ta &a, const Tb &b) {if (a > b) a = b;}
char buf[1 << 20], *P1, *P2;
inline char gc() {
if (P1 == P2)
P2 = (P1 = buf) + fread(buf, 1, 1 << 20, stdin);
return P1 == P2 ? EOF : *P1++;
}
template<typename T>
inline void read(T &ans) {
ans = 0;
T w = 1;
char c = gc();
while (!isdigit(c)) {
if (c == '-') w = -1;
c = gc();
}
while (isdigit(c)) {
ans = (ans << 3) + (ans << 1) + (c ^ 48);
c = gc();
}
ans *= w;
}
template<typename T, typename ...Ts>
void read(T &a, Ts&... other) {
read(a);
read(other...);
}
//}}}
constexpr int N = 2e5 + 10;
std::pair<int, int> E[N];
bool isAnswer[N];
int deg[N];
std::vector<int> G[N];
int n, m;
inline int get(int u, int x) {return u ^ E[x].first ^ E[x].second;}
struct DSU_ {
int fa[N], siz[N];
void init(int _n) {
for (int i = 1; i <= _n; i++)
fa[i] = i, siz[i] = 1;
}
inline int find(int x) {
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
inline bool merge(int u, int v) {
u = find(u), v = find(v);
if (u == v)
return false;
if (siz[u] < siz[v])
siz[v] += siz[u], fa[u] = v;
else
siz[u] += siz[v], fa[v] = u;
return true;
}
} DSU;
int tot = 0;
void clear() {
tot = 0;
DSU.init(n);
memset(isAnswer + 1, 0, m * sizeof(bool));
memset(deg + 1, 0, n * sizeof(int));
for (int i = 1; i <= n; i++)
G[i].clear();
}
void dfs(int u, int fa) {
for (const int &i : G[u]) {
int v = get(u, i);
if (v == fa)
continue;
DSU.fa[v] = DSU.fa[u];
dfs(v, u);
}
}
void solve() {
read(n, m);
clear();
for (int i = 1; i <= m; i++) {
int u, v;
read(u, v);
if (u == v)
continue;
if (u > v)
std::swap(u, v);
E[++tot] = std::make_pair(u, v);
}
std::sort(E + 1, E + 1 + tot);
m = std::unique(E + 1, E + 1 + tot) - E - 1;
for (int i = 1; i <= m; i++) {
const auto &[u, v] = E[i];
if (DSU.merge(u, v)) {
G[u].push_back(i);
G[v].push_back(i);
isAnswer[i] = true;
deg[u]++, deg[v]++;
}
}
int rt = 0;
for (int i = 1; i <= n; i++)
if (deg[i] > n / 2)
rt = i;
if (!rt) {
std::cout << "Yes\n";
for (int i = 1; i <= m; i++) {
const auto &[u, v] = E[i];
if (isAnswer[i])
std::cout << u << ' ' << v << '\n';
}
return ;
}
DSU.init(n);
for (const int &i : G[rt]) {
dfs(get(rt, i), rt);
}
for (int i = 1; i <= m; i++) {
if (isAnswer[i]) {
continue;
}
auto [u, v] = E[i];
if (u == rt || v == rt || DSU.find(u) == DSU.find(v))
continue;
int sonu = DSU.find(u);
int sonv = DSU.find(v);
deg[rt]--, deg[u]++, deg[v]++, deg[sonu]--;
if (deg[u] > n / 2 || deg[v] > n / 2) {
std::swap(u, v);
std::swap(sonu, sonv);
deg[sonu]--, deg[sonv]++;
}
if (deg[u] > n / 2 || deg[v] > n / 2) {
deg[u]--, deg[v]--, deg[sonu]++, deg[rt]++;
continue;
}
DSU.fa[sonu] = DSU.fa[sonv];
isAnswer[i] = true;
auto pr = std::make_pair(std::min(rt, sonu), std::max(rt, sonu));
int t = std::lower_bound(E + 1, E + 1 + m, pr) - E;
assert(E[t] == pr);
isAnswer[t] = false;
if (deg[rt] <= n / 2)
break;
}
if (deg[rt] <= n / 2) {
std::cout << "Yes\n";
for (int i = 1; i <= m; i++) {
if (isAnswer[i])
std::cout << E[i].first << ' ' << E[i].second << '\n';
}
} else {
std::cout << "No\n";
}
}
int main() {
#ifdef HeratinoNelofus
freopen("input.txt", "r", stdin);
#endif
int T;
read(T);
while (T--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 5636kb
input:
2 6 9 1 2 1 3 1 4 2 3 2 4 3 4 4 5 4 6 4 6 3 4 1 3 2 3 3 3 1 2
output:
Yes 1 2 1 3 1 4 4 5 4 6 No
result:
ok 2 cases
Test #2:
score: 0
Accepted
time: 100ms
memory: 9552kb
input:
11140 10 15 9 6 5 7 6 5 2 3 7 5 7 5 3 10 9 7 5 5 9 1 7 5 2 8 7 5 4 3 6 2 9 19 3 7 3 9 2 8 2 8 3 6 5 1 1 8 8 9 8 3 4 8 5 5 3 1 4 3 1 3 8 6 1 3 7 4 4 3 8 8 12 20 10 2 5 5 2 4 3 3 3 3 5 11 9 2 5 5 7 12 11 3 3 3 3 5 5 3 3 1 4 6 7 11 6 8 4 5 6 12 6 5 8 18 4 2 4 3 2 4 2 4 4 3 4 8 2 2 6 7 2 4 6 2 1 4 8 7 4...
output:
Yes 1 9 2 3 2 6 2 8 3 4 3 10 5 6 5 7 6 9 Yes 1 3 1 5 1 8 2 8 3 6 3 7 3 9 4 7 Yes 1 3 2 4 2 9 2 10 3 5 3 11 4 5 4 6 6 8 6 12 7 11 Yes 1 4 2 4 2 6 2 7 3 4 3 5 4 8 Yes 1 2 1 5 2 3 2 9 3 4 5 6 5 7 7 8 Yes 1 4 1 6 1 7 1 9 2 3 2 5 2 6 2 10 8 10 Yes 1 3 1 7 2 3 2 4 2 6 3 5 Yes 1 4 1 6 1 10 1 11 1 12 1 13 2...
result:
ok 11140 cases
Test #3:
score: 0
Accepted
time: 149ms
memory: 14044kb
input:
5 100000 197799 37555 22723 91160 32701 6451 4416 43186 26432 9750 82955 28292 33907 91534 78442 17771 67061 40351 28116 21494 23114 34672 66640 72636 95093 13033 6538 53698 87837 79541 71230 53985 63500 84753 5378 67971 56748 90951 20169 4465 97293 18331 53564 41043 95738 48579 96439 90865 7526 391...
output:
Yes 1 1295 1 34457 1 45338 1 51912 1 79669 1 97056 2 23589 2 58889 3 6936 3 29964 3 44515 3 51002 3 80631 4 19083 4 35140 4 42887 4 92524 5 54719 5 57297 5 81078 5 95985 6 484 6 5405 6 86612 6 87773 7 65534 7 98458 8 4586 8 30625 8 32122 8 35804 8 48558 8 87681 9 6925 9 69470 9 79888 9 92777 10 6204...
result:
ok 5 cases
Test #4:
score: 0
Accepted
time: 93ms
memory: 14184kb
input:
5 100000 100000 98195 31806 98195 70169 92153 98195 98195 46320 94369 56771 94369 49988 74295 98195 33796 98195 89903 94369 98195 1814 82388 98195 10189 94369 98195 6267 29845 98195 22425 94369 6241 98195 98195 33204 66516 98195 94369 71364 26277 94369 94369 94722 94369 25349 14629 98195 9329 98195 ...
output:
Yes 1 98195 2 98195 3 98195 4 94369 5 94369 6 98195 7 94369 8 98195 9 94369 10 98195 11 98195 12 94369 13 98195 14 94369 15 94369 16 94369 17 98195 18 98195 19 98195 20 94369 21 94369 22 94369 23 94369 24 94369 25 94369 26 94369 27 94369 28 94369 29 98195 30 94369 31 94369 32 98195 33 98195 34 94369...
result:
ok 5 cases
Test #5:
score: 0
Accepted
time: 162ms
memory: 14152kb
input:
5 100000 149998 50735 5447 50735 24875 15119 49666 50735 30352 44756 49555 26546 32695 98445 50735 71657 50735 92212 50735 50735 19382 30935 50735 43688 46767 54630 54562 31371 50735 48877 50735 78593 76833 74317 37258 50735 48236 67116 50735 36579 50735 37536 44353 50735 46602 35088 29568 86657 507...
output:
Yes 1 35019 1 50735 2 15015 2 50735 3 50735 3 60958 4 40777 4 50735 5 50200 5 50735 6 22341 6 50735 7 50735 7 89814 8 2615 8 50735 9 50735 9 63844 10 25168 10 50735 11 1279 11 50735 12 27968 12 50735 13 36941 13 50735 14 50735 14 90189 15 50735 15 52058 16 41994 16 50735 17 11154 17 50735 18 50735 1...
result:
ok 5 cases
Test #6:
score: 0
Accepted
time: 83ms
memory: 13724kb
input:
11102 14 14 9 10 10 14 9 11 10 2 9 14 9 5 10 13 9 8 4 9 3 10 10 1 12 10 10 6 9 7 6 6 3 2 3 5 2 6 1 6 3 4 3 6 5 5 3 2 2 1 4 5 2 5 5 3 8 8 5 6 4 6 8 6 8 4 6 1 8 3 2 8 7 8 12 12 8 12 12 2 4 12 9 12 12 3 1 12 1 8 6 8 11 8 7 8 8 10 12 5 15 15 9 15 6 15 13 8 15 3 8 11 15 12 8 2 15 4 8 10 15 8 15 14 8 4 7 ...
output:
Yes 1 10 2 10 3 10 4 9 5 9 6 10 7 9 8 9 9 10 9 11 9 14 10 12 10 13 Yes 1 6 2 3 2 6 3 4 3 5 Yes 1 2 2 3 3 5 4 5 Yes 1 6 2 8 3 8 4 6 4 8 5 6 7 8 Yes 1 8 1 12 2 12 3 12 4 12 5 12 6 8 7 8 8 10 8 11 9 12 Yes 1 15 2 8 3 15 4 8 4 15 5 8 6 15 7 8 8 10 8 11 8 13 9 15 12 15 14 15 Yes 1 9 2 3 3 6 3 7 3 10 3 11...
result:
ok 11102 cases
Test #7:
score: 0
Accepted
time: 117ms
memory: 14092kb
input:
11102 14 19 9 3 3 6 8 3 2 14 7 3 11 3 13 9 3 14 3 5 2 3 7 4 1 10 13 3 4 3 3 1 3 10 12 8 11 6 3 12 11 15 9 11 10 7 1 2 9 6 11 2 11 10 8 11 11 5 3 8 11 4 11 3 11 7 1 11 5 4 6 11 5 6 1 2 4 3 3 5 1 3 5 4 3 2 12 16 8 11 12 10 12 3 5 7 12 5 1 12 9 4 12 2 6 12 12 8 10 1 12 11 7 12 12 9 3 2 4 12 6 7 3 1 6 3...
output:
Yes 1 3 1 10 2 3 2 14 3 5 3 7 3 11 3 12 3 13 4 7 6 11 8 12 9 13 Yes 1 2 1 11 3 8 3 11 4 5 4 11 6 9 6 11 7 10 7 11 Yes 1 2 1 3 3 5 4 5 Yes 1 10 1 12 2 3 2 12 4 9 4 12 5 7 5 12 6 12 8 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