QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#207825 | #6421. Degree of Spanning Tree | rsj | TL | 0ms | 3684kb | C++14 | 1.5kb | 2023-10-08 21:07:01 | 2023-10-08 21:07:02 |
Judging History
answer
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
struct edge {
int to;
edge *nex;
}*head[N];
void add(int u,int v) {
edge *cur=new edge;
cur->to=v;
cur->nex=head[u];
head[u]=cur;
}
int f[N];
int find(int x) {
if(f[x]==x) return x;
return f[x]=find(f[x]);
}
void uni(int x,int y) {
f[find(x)]=find(y);
}
bool check(int x,int y) {
return find(x)==find(y);
}
int in[N];
set<pair<int,int>> e;
vector<pair<int,int>> ee;
pair<int,int> mp(int x,int y) {
if(x>y) swap(x,y);
return make_pair(x,y);
}
int get() {
int n,m,i,r=0,u,v;
cin>>n>>m;
for(i=1;i<=n;i++) head[i]=0,f[i]=i,in[i]=0;
for(i=1;i<=m;i++) {
cin>>u>>v;
if(u>v) swap(u,v);
ee.push_back({u,v});
add(u,v),add(v,u);
if(!check(u,v)) {
in[u]++,in[v]++,uni(u,v);
e.insert({u,v});
}
}
for(i=1;i<=n;i++) if(in[i]>n/2) r=i;
if(!r) {
cout<<"Yes\n";
for(auto x:e) {
tie(u,v)=x;
cout<<u<<" "<<v<<endl;
}
return 0;
}
for(auto x:ee) {
tie(u,v)=x;
if(u!=r&&v!=r&&e.find(x)==e.end()) {
if(e.find(mp(u,r))!=e.end() && e.find(mp(v,r))!=e.end()) {
if(in[u]<in[v]) swap(u,v);
e.erase(mp(u,r)); in[u]--,in[r]--;
e.insert(mp(u,v)); in[u]++,in[v]++;
}
}
}
if(in[i]>n/2) cout<<"No\n";
else {
cout<<"Yes\n";
for(auto x:e) {
tie(u,v)=x;
cout<<u<<" "<<v<<endl;
}
return 0;
}
return 0;
}
int main() {
int T; cin>>T;
while(T--) get();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3684kb
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
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 1 9 2 3 2 6 2 8 3 4 3 10 5 6 5 7 6 9 Yes 1 5 1 8 1 9 2 3 2 6 2 8 3 4 3 6 3 7 3 9 3 10 4 8 5 6 5 7 6 9 8 9 Yes 1 3 1 5 1 8 1 9 2 3 2 4 2 6 2 8 2 9 2 10 3 4 3 6 3 7 3 9 3 10 3 11 4 5 4 6 4 8 5 6 5 7 5 11 6 8 6 9 7 11 7 12 8 9 Yes 1 3 1 4 1 5 1 8 1 9 2 3 2 4 2 6 2 8 2 9 2 10 3 4 3 6 3 7 3 9 3 10 3 ...