QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#152443 | #6421. Degree of Spanning Tree | qzez# | WA | 1ms | 3636kb | C++14 | 1.6kb | 2023-08-28 08:39:09 | 2023-08-28 08:39:10 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
if(x.empty())return out<<"[]";
out<<'['<<x[0];
for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
cerr<<x<<' ',debug(y...);
}
const int N=1e5+10;
int T,n,m;
vector<pair<int,int> >E,S,P,ans;
int fa[N];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool merge(int x,int y){
x=find(x),y=find(y);
if(x==y)return 0;
return fa[x]=y,1;
}
int deg[N];
void get(){
E.clear(),S.clear(),P.clear(),ans.clear();
scanf("%d%d",&n,&m);
iota(fa,fa+1+n,0);
fill(deg+1,deg+1+n,0);
for(int u,v;m--;){
scanf("%d%d",&u,&v);
P.push_back({u,v});
if(!merge(u,v))E.push_back({u,v});
else S.push_back({u,v}),deg[u]++,deg[v]++;
}
int rt=max_element(deg+1,deg+1+n)-deg;
iota(fa,fa+1+n,0);
for(auto e:S)if(e.first!=rt&&e.second!=rt){
merge(e.first,e.second);
}
// debug(ary(fa,1,n));
for(auto e:E)if(e.first!=rt&&e.second!=rt){
if(deg[rt]*2<=n)break;
// debug(e.first,e.second);
if(merge(e.first,e.second))ans.push_back(e),deg[rt]--;
}
if(deg[rt]*2>n){
puts("No");return;
}
iota(fa,fa+1+n,0);
for(auto e:ans)merge(e.first,e.second);
for(auto e:P)if(merge(e.first,e.second))ans.push_back(e);
puts("Yes");
for(auto e:ans)printf("%d %d\n",e.first,e.second);
}
int main(){
for(scanf("%d",&T);T--;)get();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3636kb
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 Yes 1 2 1 3
result:
wrong answer case 2, paticipant's deg[1] = 2 is too large