QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#661497 | #9453. Graph Generator | ucup-team004 | AC ✓ | 160ms | 8496kb | C++23 | 2.4kb | 2024-10-20 16:33:09 | 2024-10-20 16:33:11 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
struct DSU {
std::vector<int> f, siz;
DSU() {}
DSU(int n) {
init(n);
}
void init(int n) {
f.resize(n);
std::iota(f.begin(), f.end(), 0);
siz.assign(n, 1);
}
int find(int x) {
while (x != f[x]) {
x = f[x] = f[f[x]];
}
return x;
}
bool same(int x, int y) {
return find(x) == find(y);
}
bool merge(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
return false;
}
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x) {
return siz[find(x)];
}
};
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<std::vector<int>> adj(n);
for (int i = 0; i < m; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
std::vector<int> p(n), invp(n);
std::iota(p.begin(), p.end(), 0);
std::sort(p.begin(), p.end(),
[&](int i, int j) {
return adj[i].size() < adj[j].size();
});
for (int i = 0; i < n; i++) {
invp[p[i]] = i;
}
std::vector<std::vector<int>> a(n);
DSU dsu(n);
std::vector<bool> vis(n);
i64 e = 0;
for (auto x : p) {
for (auto y : adj[x]) {
if (invp[y] < invp[x] && !vis[y]) {
vis[y] = true;
a[x].push_back(y);
e += dsu.size(y);
dsu.merge(x, y);
}
}
for (auto y : adj[x]) {
if (invp[y] < invp[x]) {
if (dsu.find(y) != x) {
std::cout << "No\n";
return;
}
}
}
}
if (e != m) {
std::cout << "No\n";
return;
}
std::cout << "Yes\n";
for (auto x : p) {
std::cout << x + 1 << " " << a[x].size();
for (auto y : a[x]) {
std::cout << " " << y + 1;
}
std::cout << "\n";
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3884kb
input:
3 3 0 4 4 1 2 2 3 3 4 2 4 5 5 1 2 2 3 3 4 4 5 2 4
output:
Yes 1 0 2 0 3 0 Yes 1 0 3 0 4 1 3 2 2 1 4 No
result:
ok 3 cases passed
Test #2:
score: 0
Accepted
time: 160ms
memory: 8496kb
input:
4176 10 15 1 4 9 1 3 7 8 1 2 1 5 4 5 1 10 1 7 1 10 7 10 3 6 4 3 1 6 1 9 2 7 10 6 7 1 7 1 4 7 2 4 2 5 2 3 1 1 6 2 6 5 1 6 8 5 2 3 1 4 6 2 1 5 1 1 4 6 2 6 1 9 15 5 1 4 2 7 1 1 8 2 3 5 8 1 9 4 3 6 5 9 2 3 1 4 1 7 2 9 7 1 6 6 11 1 2 3 5 3 1 3 2 4 3 1 6 4 2 1 4 5 4 5 1 5 2 6 6 1 6 6 3 1 3 1 5 4 2 2 1 10 ...
output:
Yes 8 0 2 0 5 0 6 0 9 1 2 3 0 4 2 5 6 7 1 3 10 1 7 1 4 4 9 8 10 No No No Yes 6 0 2 0 3 1 2 4 1 3 5 1 4 1 2 6 5 No No Yes 2 0 3 0 7 1 3 4 0 5 1 4 6 1 5 1 3 6 2 7 Yes 4 0 5 0 7 0 8 0 9 0 6 0 10 0 3 2 6 10 2 5 5 7 9 8 3 1 2 2 4 Yes 9 0 4 0 6 0 7 0 8 0 5 2 7 8 2 2 5 6 3 1 2 1 2 3 4 Yes 3 0 4 0 5 1 4 6 0...
result:
ok 4176 cases passed
Extra Test:
score: 0
Extra Test Passed