QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#208530 | #6421. Degree of Spanning Tree | rsj | WA | 218ms | 37524kb | C++14 | 2.0kb | 2023-10-09 18:28:17 | 2023-10-09 18:28:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5+15;
int in[N];
struct edge {
int to;
edge *nex;
}*head[N];
int vis[N];
int f[N];
vector<pair<int,int>> e;
vector<pair<int,int>> d;
int find(int x) {
if(f[x]==x) return x;
return f[x]=find(f[x]);
}
void uni(int x,int y) {
if(vis[find(x)]) swap(x,y);
f[find(x)]=find(y);
}
bool check(int x,int y) {
return find(x)==find(y);
}
void add(int u,int v) {
edge *cur=new edge;
cur->to=v;
cur->nex=head[u];
head[u]=cur;
}
int T,cnt;
void get() {
e.clear();
d.clear();
int n,m,u,v,i,j;
cnt++;
cin>>n>>m; if(cnt==120) cout<<n<<" "<<m<<endl;
for(i=1;i<=n;i++) in[i]=vis[i]=0,head[i]=0,f[i]=i;
for(i=1;i<=m;i++) {
cin>>u>>v; if(cnt==120) cout<<u<<" "<<v<<endl;
if(!check(u,v)) {
uni(u,v);
in[u]++;
in[v]++;
add(u,v);
add(v,u);
d.emplace_back(u,v);
} else {
e.emplace_back(u,v);
}
}
if(T>5) return ;
int r=0;
for(i=1;i<=n;i++) if(in[i]>n/2) r=i;
if(!r) {
cout<<"Yes\n";
for(auto x:d) {
tie(u,v)=x;
cout<<u<<" "<<v<<endl;
}
return ;
}
for(i=1;i<=n;i++) f[i]=i;
for(auto x:d) {
tie(u,v)=x;
if(u==r) vis[v]=1;
else if(v==r) vis[u]=1;
else uni(u,v);
// cout<<"pre in"<<u<<" "<<v<<endl;
}
for(auto x:d) {
tie(u,v)=x;
if(u==r) ;
else if(v==r) ;
else uni(u,v);
}
for(auto x:e) {
tie(u,v)=x;
if(u!=r&&v!=r&&!check(u,v)) {
if(in[u]+1>n/2) continue;
if(in[v]+1>n/2) continue;
in[u]++,in[v]++;
i=find(u),j=find(v);
if(in[i]<in[j]) swap(i,j);
in[i]--,in[r]--;
//assert(vis[i]);
//assert(vis[j]);
vis[i]=0; uni(i,j);
d.emplace_back(u,v);
}
}
if(in[r]>n/2) {
cout<<"No\n";
} else {
cout<<"Yes\n";
for(auto x:d) {
tie(u,v)=x;
if(u==r||v==r) continue;
cout<<u<<" "<<v<<endl;
}
for(i=1;i<=n;i++) if(vis[i]) cout<<r<<" "<<i<<endl;
return ;
}
}
int main() {
// freopen("1.in","r",stdin);
cin>>T;
while(T--) get();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 9448kb
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: 218ms
memory: 37524kb
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:
5 12 2 5 2 4 4 1 2 2 4 2 1 4 2 3 2 2 2 5 2 3 4 2 5 4 No No Yes 1 2 Yes 1 2 Yes 1 2 No
result:
wrong answer Line "5 12" doesn't correspond to pattern "Yes|No"