QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#621780 | #6421. Degree of Spanning Tree | gates_orz | TL | 3ms | 3716kb | C++20 | 3.2kb | 2024-10-08 16:53:25 | 2024-10-08 16:53:26 |
Judging History
answer
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;
//#define int LL
#define inl inline
const int N = 3e5 + 10;
const int M = N * 2;
//const int mod=998244353;
const int mod = 1000000007;
const double eps = 1e-8;
//const int mod=1e9+7;
typedef pair<int, int> PII;
//const int INF=0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
int n, m;
void become_faster() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
struct Node {
int u, v;
};
Node edge[N];
struct DSU {
vector<int> fa, siz;
DSU() {
}
DSU(int n) {
init(n);
}
void init(int n) {
fa.resize(n);
iota(fa.begin(), fa.end(), 0);
siz.assign(n, 1);
}
int find(int x) {
while (x != fa[x]) {
x = fa[x] = fa[fa[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];
fa[y] = x;
return true;
}
int size(int x) {
return siz[find(x)];
}
};
int getRand(int min, int max) {
return (rand() % (max - min + 1)) + min;
}
void solve() {
cin >> n >> m;
vector<vector<int> > g(n + 1);
for (int i = 1; i <= m; i++) {
auto &[u,v] = edge[i];
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> d(n + 1, 0);
DSU dsu(n+1);
int sign=1,cnt=0,root;
auto dfs = [&](auto &self, int u, int fa)-> void {
if(!sign)return;
if (fa) {
d[u]++;
d[fa]++;
if(d[u]>n/2||d[fa]>n/2)sign=0;
}
for(auto v:g[u]) {
if(v==fa)continue;
if(dsu.same(u,v))continue;
if(d[u]+1>n/2||d[v]+1>n/2)continue;
dsu.merge(u,v);
cnt++;
self(self,v,u);
}
};
auto output=[&](auto &self,int u,int fa)->void {
if(fa) {
d[u]++;
d[fa]++;
cout<<u<<" "<<fa<<"\n";
}
for(auto v:g[u]) {
if(v==fa)continue;
if(dsu.same(u,v))continue;
if(d[u]+1>n/2||d[v]+1>n/2)continue;
dsu.merge(u,v);
self(self,v,u);
}
};
auto work = [&]() {
for (int i = 1; i <= n; i++)shuffle(g[i].begin(), g[i].end(), std::mt19937{std::random_device{}()});
srand(time(0));
root = getRand(1, n);
d.assign(n+1,0);
dsu.init(n+1);
sign=1;
cnt=0;
dfs(dfs,root,0);
return (sign==1&&cnt==n-1);
};
for (int i = 1; i <= 200; i++) {
if (work()) {
dsu.init(n+1);
d.assign(n+1,0);
cout<<"Yes"<<"\n";
output(output,root,0);
return;
}
}
cout << "No" << endl;
}
signed main() {
become_faster();
int T = 1;
//T=read();
cin >> T;
//for(int i=1;i<=100;i++)solve(i);
while (T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 3716kb
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 4 6 3 4 1 3 2 1 5 4 No
result:
ok 2 cases
Test #2:
score: -100
Time Limit Exceeded
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 3 10 4 3 2 3 8 2 6 2 9 6 1 9 7 9 5 7 Yes 3 6 4 3 8 4 1 8 5 1 9 8 2 8 7 4 Yes 12 6 7 12 11 7 5 11 3 5 1 3 4 5 2 4 9 2 10 2 8 6 Yes 4 2 1 4 3 4 5 3 7 5 8 7 6 7 Yes 7 6 5 7 2 5 3 2 4 3 9 2 1 2 8 7 Yes 2 10 6 2 4 6 1 4 9 1 7 1 3 2 5 2 8 10 Yes 5 7 3 5 1 3 4 3 2 4 6 2 Yes 2 8 6 2 10 6 1 10 12 1 3 12 ...