QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#356414 | #6421. Degree of Spanning Tree | PorNPtree | AC ✓ | 856ms | 39736kb | C++14 | 3.6kb | 2024-03-17 18:57:51 | 2024-03-17 18:57:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<int> G[N], tG[N];
vector< pair<int, int> > T, E;
map< pair<int, int>, int> M;
mt19937 rnd;
int du[N];
void dfs(int x, int fa = -1)
{
shuffle(G[x].begin(), G[x].end(), rnd);
for (int i = 0; i < (int)G[x].size(); ++i) {
int v = G[x][i];
if (!du[v]) {
T.push_back(make_pair(min(x, v), max(x, v)));
tG[x].push_back(v);
tG[v].push_back(x);
++du[x], ++du[v];
dfs(v, x);
} else if (v != fa) {
if (!M.count(make_pair(min(x, v), max(x, v)))) {
E.push_back(make_pair(min(x, v), max(x, v)));
M[make_pair(min(x, v), max(x, v))] = 1;
}
}
}
return;
}
int col[N];
void color(int x, int c, int fa)
{
col[x] = c;
for (int i = 0; i < (int)tG[x].size(); ++i) {
int v = tG[x][i];
if (v != fa) {
color(v, c, x);
}
}
return;
}
int fa[N];
int find(int x)
{
return (fa[x] == x ? x : fa[x] = find(fa[x]));
}
signed main()
{
int TT;
scanf("%d", &TT);
while (TT--) {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
G[i].clear();
tG[i].clear();
du[i] = 0;
}
M.clear();
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
if (x != y && !M.count(make_pair(min(x, y), max(x, y)))) {
M[make_pair(min(x, y), max(x, y))] = 1;
G[x].push_back(y);
G[y].push_back(x);
}
}
if (n == 2) {
puts("Yes");
puts("1 2");
continue;
}
if (n == 3) {
puts("No");
continue;
}
M.clear();
T.clear(), E.clear();
dfs(1);
int flag = 0;
for (int i = 1; i <= n; ++i) {
if (du[i] > n / 2) {
flag = i;
break;
}
}
if (!flag) {
puts("Yes");
for (int i = 0; i < (int)T.size(); ++i) {
printf("%d %d\n", T[i].first, T[i].second);
}
continue;
}
col[flag] = 0;
for (int i = 0; i < (int)tG[flag].size(); ++i) {
color(tG[flag][i], i + 1, flag);
fa[i + 1] = i + 1;
}
set< pair<int, int> > res;
for (int i = 0; i < (int)T.size(); ++i) {
res.insert(T[i]);
}
for (int i = 0; i < (int)E.size() && du[flag] > n / 2; ++i) {
int x = E[i].first, y = E[i].second;
if (x == flag || y == flag || find(col[x]) == find(col[y])) {
continue;
}
res.insert(E[i]);
++du[x], ++du[y];
int c1 = tG[flag][find(col[x]) - 1], c2 = tG[flag][find(col[y]) - 1], wh = du[c1] > du[c2] ? c1 : c2;
res.erase(make_pair(min(flag, wh), max(flag, wh)));
--du[flag], --du[wh];
if (wh == c1) {
fa[find(col[x])] = find(col[y]);
} else {
fa[find(col[y])] = find(col[x]);
}
}
if (du[flag] <= n / 2) {
puts("Yes");
for (set< pair<int, int> >::iterator it = res.begin(); it != res.end(); ++it) {
printf("%d %d\n", (*it).first, (*it).second);
}
} else {
puts("No");
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 14940kb
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 2 4 4 5 4 6 No
result:
ok 2 cases
Test #2:
score: 0
Accepted
time: 321ms
memory: 18480kb
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 6 9 2 6 2 3 3 10 3 4 2 8 5 6 5 7 Yes 1 5 1 8 4 8 4 7 3 7 3 6 3 9 2 8 Yes 1 3 3 11 5 11 4 5 2 4 2 10 2 9 4 6 6 8 6 12 7 12 Yes 1 4 2 4 2 6 6 7 7 8 5 7 3 5 Yes 1 5 5 6 6 7 7 9 2 9 2 3 3 4 7 8 Yes 1 4 4 6 2 6 2 5 2 3 2 10 8 10 1 7 1 9 Yes 1 3 3 4 4 6 6 7 5 7 2 6 Yes 1 12 6 12 6 10 8 10 5 8 4 5 ...
result:
ok 11140 cases
Test #3:
score: 0
Accepted
time: 704ms
memory: 39736kb
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 97056 60331 97056 60331 99295 5802 99295 5802 71988 71988 82598 65965 82598 13671 65965 13671 35686 35686 38597 38597 45612 24602 45612 24602 34368 34368 41605 38620 41605 38620 73737 36447 73737 36447 93423 11597 93423 11597 31335 31335 99570 53610 99570 30251 53610 30251 67166 67166 91329 27...
result:
ok 5 cases
Test #4:
score: 0
Accepted
time: 402ms
memory: 31508kb
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 24334 98195 42490 98195 81525 98195 87398 98195 64041 98195 56368 98195 2692 98195 38781 98195 50171 98195 9809 98195 58283 98195 46160 98195 51567 98195 82152 98195 93900 98195 27432 98195 54213 98195 17315 98195 18128 98195 46929 98195 57785 98195 26460 98195 46840 98195 90840 98195 41...
result:
ok 5 cases
Test #5:
score: 0
Accepted
time: 856ms
memory: 34848kb
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 5 50200 6 22341 7 50735 7 89814 8 2615 9 50735 9 63844 10 25168 11 1279 11 50735 12 27968 13 36941 13 50735 14 50735 14 90189 15 50735 15 52058 16 41994 16 50735 17 11154 18 50735 18 99130 19 50735 19 58578 20 6935 20 50735 21 55906 22 5200...
result:
ok 5 cases
Test #6:
score: 0
Accepted
time: 250ms
memory: 29876kb
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 10 13 9 10 9 14 9 11 7 9 8 9 4 9 5 9 3 10 10 12 6 10 Yes 1 6 2 3 2 6 3 4 3 5 Yes 1 2 2 3 3 5 4 5 Yes 1 6 5 6 4 6 4 8 3 8 2 8 7 8 Yes 1 8 8 12 9 12 2 12 4 12 3 12 5 12 6 8 8 11 7 8 8 10 Yes 1 15 14 15 4 15 4 8 2 8 8 10 8 11 5 8 7 8 8 13 6 15 12 15 3 15 9 15 Yes 1 9 2 3 3 6 3 7 3 10 3 11...
result:
ok 11102 cases
Test #7:
score: 0
Accepted
time: 463ms
memory: 32620kb
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 10 3 10 3 14 2 14 3 7 4 7 3 6 6 11 3 5 3 13 9 13 3 12 8 12 Yes 1 2 2 11 8 11 3 8 9 11 6 9 10 11 7 10 5 11 4 5 Yes 1 2 1 3 3 4 4 5 Yes 1 10 1 12 2 3 3 12 4 9 4 12 5 7 5 12 6 12 8 11 8 12 Yes 1 5 3 5 2 3 3 6 4 6 Yes 1 2 2 5 1 4 3 4 1 6 Yes 1 2 1 6 3 10 4 6 4 11 5 6 5 7 6 9 6 10 8 9 Yes 1 4 4 5 2...
result:
ok 11102 cases
Test #8:
score: 0
Accepted
time: 192ms
memory: 13516kb
input:
100000 5 7 2 5 1 4 2 1 1 3 5 1 4 2 2 3 5 7 2 4 4 3 2 1 4 5 2 5 1 4 3 2 5 7 5 1 4 2 4 5 4 3 4 1 3 1 2 1 5 7 4 1 4 3 3 5 2 5 4 5 2 4 1 5 5 7 5 2 2 1 5 4 2 4 4 3 3 2 1 4 5 7 2 4 5 1 1 3 2 1 2 3 4 1 2 5 5 7 5 4 4 2 5 2 3 5 4 1 5 1 4 3 5 7 1 5 2 3 2 5 2 4 3 5 4 5 1 2 5 7 1 3 5 2 2 4 1 2 5 3 2 3 4 3 5 7 3...
output:
Yes 1 3 1 4 2 3 2 5 Yes 1 4 4 5 2 5 2 3 Yes 1 2 1 5 3 4 4 5 Yes 1 4 3 4 3 5 2 5 Yes 1 2 2 5 3 4 4 5 Yes 1 4 1 5 2 3 2 5 Yes 1 5 2 5 2 4 3 4 Yes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