QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#745897 | #6421. Degree of Spanning Tree | exxqfu | WA | 49ms | 11816kb | C++14 | 2.9kb | 2024-11-14 12:18:13 | 2024-11-14 12:18:13 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, d, u) for(int i = d; i <= u; ++i)
#define dep(i, u, d) for(int i = u; i >= d; --i)
#define cep(n) while(n--)
#define gep(i, a) for(int i = firs[a]; i; i = neig[i])
int ured() {
int ch, re = 0;
do {
ch = getchar();
} while('9' < ch || ch < '0');
do {
re = re * 10 + (ch ^ '0');
} while('0' <= (ch = getchar()) && ch <= '9');
return re;
}
void uwit(int da) {
int ch[21], cn = 0;
do {
ch[++cn] = da - da / 10 * 10;
} while(da /= 10);
do {
putchar('0' ^ ch[cn]);
} while(--cn);
}
const int _maxn = 500011, _maxm = 1000011;
int t, n, m, u[_maxn], v[_maxn], fath[_maxn], indu[_maxn], cnts, atno, btno, ctno, ated[_maxn];
bool pdif[_maxn];
int find(int at) {
return fath[at] == at ? at : (fath[at] = find(fath[at]));
}
int main() {
t = ured();
cep(t) {
n = ured(), m = ured(), cnts = 1, atno = btno = 0;
rep(i, 1, n) {
fath[i] = i, indu[i] = 0;
}
rep(i, 1, m) {
u[i] = ured(), v[i] = ured();
if(find(u[i]) == find(v[i])) {
pdif[i] = 0;
} else {
pdif[i] = 1, ++cnts, ++indu[u[i]], ++indu[v[i]], fath[find(u[i])] = find(v[i]);
}
}
if(cnts < n - 1) {
puts("No");
continue;
}
rep(i, 1, n) {
if(indu[i] << 1 > n) {
atno = i;
break;
}
}
if(!atno) {
puts("Yes");
rep(i, 1, m) {
if(pdif[i]) {
uwit(u[i]), putchar(' '), uwit(v[i]), putchar('\n');
}
}
continue;
}
rep(i, 1, n) {
fath[i] = i;
}
rep(i, 1, m) {
if(pdif[i] && u[i] != atno && v[i] != atno) {
fath[find(u[i])] = find(v[i]);
}
}
rep(i, 1, m) {
if(pdif[i] && (u[i] == atno || v[i] == atno)) {
ated[find(u[i] ^ v[i] ^ atno)] = i;
}
}
rep(i, 1, m) {
if(!pdif[i] && u[i] != atno && v[i] != atno) {
pdif[ated[find(u[i])]] = 0, pdif[i] = 1, --indu[u[ated[find(u[i])]]], --indu[v[ated[find(u[i])]]];
++indu[u[i]], ++indu[v[i]], fath[find(u[i])] = find(v[i]);
if(indu[atno] << 1 <= n) {
break;
}
}
}
if(indu[atno] << 1 > n) {
puts("No");
continue;
}
rep(i, 1, n) {
if(indu[i] << 1 > n) {
btno = i;
break;
}
}
if(!atno) {
puts("Yes");
rep(i, 1, m) {
if(pdif[i]) {
uwit(u[i]), putchar(' '), uwit(v[i]), putchar('\n');
}
}
continue;
}
fath[atno] = fath[btno] = 0;
rep(i, 1, m) {
if(pdif[i]) {
if(!fath[u[i]] && !fath[v[i]]) {
ctno = i;
} else if(!fath[v[i]]) {
fath[u[i]] = v[i];
} else if(!fath[u[i]]) {
fath[v[i]] = u[i];
}
}
}
rep(i, 1, m) {
if(!pdif[i] && fath[u[i]] && fath[v[i]] && fath[u[i]] != fath[v[i]]) {
pdif[i] = 1, pdif[ctno] = 0;
break;
}
}
if(pdif[ctno]) {
puts("No");
} else {
puts("Yes");
rep(i, 1, m) {
if(pdif[i]) {
uwit(u[i]), putchar(' '), uwit(v[i]), putchar('\n');
}
}
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11780kb
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: -100
Wrong Answer
time: 49ms
memory: 11816kb
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 9 6 5 7 6 5 2 3 3 10 9 1 2 8 4 3 6 2 Yes 3 7 3 9 2 8 3 6 5 1 1 8 8 9 4 8 Yes 10 2 2 4 5 11 9 2 7 12 11 3 3 1 4 6 7 11 6 8 4 5 Yes 4 2 4 3 4 8 6 7 6 2 1 4 7 5 Yes 6 5 5 7 5 9 4 3 2 9 2 3 8 7 5 1 Yes 10 2 2 6 3 2 1 9 8 10 4 6 6 1 2 5 1 7 Yes 5 7 5 4 7 1 2 6 6 7 1 3 Yes 12 3 1 13 7 8 8 2 10 6 1 6 1...
result:
wrong answer case 19, participant does not find a solution but the jury does