QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#645032 | #6421. Degree of Spanning Tree | rush-from-behind# | WA | 97ms | 8120kb | C++23 | 2.9kb | 2024-10-16 16:37:14 | 2024-10-16 16:37:15 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "\33[32m[" << __LINE__ << "]\33[m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T &x, T y) {return x = max(x, y);}
template <typename T> T& chkmin(T &x, T y) {return x = min(x, y);}
template <typename T> T& read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = - f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= f;
}
const int N = 2e5 + 10;
int n, m, deg[N], fa[N], dd[N];
// vector <int> v[N];
pair <int, int> e[N], t[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);}
void zhk() {
cin >> n >> m;
F(i, 1, n) deg[i] = dd[i] = 0, fa[i] = i;
F(i, 1, m) {
cin >> e[i].first >> e[i].second;
if (e[i].first == e[i].second) {
i--;
m--;
continue;
}
deg[e[i].first]++;
deg[e[i].second]++;
// int x, y; cin >> x >> y;
}
// sort(e + 1, e + m + 1, [&] (pair <int, int> x, pair <int, int> y) {
// return deg[x.first] + deg[x.second] < deg[y.first] + deg[y.second];
// });
vector <pair <int, int>> ans;
F(i, 1, m) {
int x = find(e[i].first), y = find(e[i].second);
if (x == y) continue;
ans.push_back(e[i]);
fa[x] = y;
dd[e[i].first]++, dd[e[i].second]++;
}
// debug << dd[1] <<
int pos = 0;
F(i, 1, n)
if (dd[i] > n / 2) {
assert(pos == 0);
pos = i;
// cout << "No\n";
// return;
}
if (!pos) {
cout << "Yes\n";
for (auto [a, b]: ans) cout << a << ' ' << b << '\n';
} else {
F(i, 1, n) fa[i] = i;
vector <pair <int, int>> aa;
vector <int> w;
for (auto [a, b]: ans) {
if (a == pos || b == pos) {
if (a == pos) w.push_back(b);
else w.push_back(a);
continue;
}
fa[find(a)] = find(b);
aa.emplace_back(a, b);
}
for (int i: w) dd[i]--;
F(i, 1, m) {
int a = find(e[i].first), b = find(e[i].second);
if (a == pos || b == pos) continue;
aa.push_back(e[i]);
fa[a] = b;
dd[e[i].first]++;
dd[e[i].second]++;
if (--dd[pos] == n / 2) break;
}
// debug << pos << endl;
if (dd[pos] > n / 2) {
cout << "No\n";
return;
}
for (int i: w)
if (dd[i] + 1 <= n / 2 && find(i) != pos) {
fa[find(i)] = pos;
aa.emplace_back(pos, i);
}
if (aa.size() < n - 1) {
cout << "No\n";
return;
}
// assert(aa.size() == n - 1);
cout << "Yes\n";
for (auto [a, b]: aa) cout << a << ' ' << b << '\n';
// F(i, 1, n)
// if (i != pos && find(i) == i) {
// }
}
}
signed main() {
ios::sync_with_stdio(0); // don't use puts
cin.tie(0), cout.tie(0);
int _ = 1;
cin >> _;
while (_--) zhk();
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5604kb
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: 97ms
memory: 8120kb
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's output is not a tree