QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152474#6421. Degree of Spanning TreeA_zjzjWA 129ms4016kbC++142.5kb2023-08-28 09:42:282023-08-28 09:42:29

Judging History

你现在查看的是最新测评结果

  • [2023-08-28 09:42:29]
  • 评测
  • 测评结果:WA
  • 用时:129ms
  • 内存:4016kb
  • [2023-08-28 09:42:28]
  • 提交

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,res;
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(),res.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);
	}
	for(auto e:E)if(e.first!=rt&&e.second!=rt){
		if(deg[rt]*2<=n)break;
		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:S)if(e.first!=rt&&e.second!=rt){
		if(merge(e.first,e.second))ans.push_back(e);
	}
	for(auto e:S){
		if(merge(e.first,e.second))ans.push_back(e);
	}
	assert(ans.size()==n-1);
	fill(deg+1,deg+1+n,0);
	for(auto e:ans)deg[e.first]++,deg[e.second]++;
	rt=max_element(deg+1,deg+1+n)-deg;
	if(deg[rt]*2<=n){
		puts("Yes");
		for(auto e:ans)printf("%d %d\n",e.first,e.second);
	}else{
		int u=0,v=0;
		for(auto e:P)if(deg[e.first]*2+1<n&&deg[e.second]*2+1<n){
			tie(u,v)=e;break;
		}
		if(!u){
			puts("No");
		}else{
			iota(fa,fa+1+n,0);
			merge(u,v);
			res.push_back({u,v});
			for(auto e:ans)if(deg[e.first]*2+1<n&&deg[e.second]*2+1<n){
				if(merge(e.first,e.second))res.push_back(e);
			}
			for(auto e:ans)if(deg[e.first]*2<=n&&deg[e.second]*2<=n){
				if(merge(e.first,e.second))res.push_back(e);
			}
			for(auto e:ans){
				if(merge(e.first,e.second))res.push_back(e);
			}
			puts("Yes");
			for(auto e:res)printf("%d %d\n",e.first,e.second);
		}
	}
}
int main(){
	for(scanf("%d",&T);T--;)get();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3732kb

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 5
4 6
1 2
1 3
1 4
No

result:

ok 2 cases

Test #2:

score: -100
Wrong Answer
time: 129ms
memory: 4016kb

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
9 6
5 7
6 5
3 10
9 1
4 3
2 3
2 8
6 2
Yes
3 7
3 9
3 6
5 1
2 8
1 8
8 9
4 8
Yes
5 11
7 12
11 3
3 1
4 6
7 11
6 8
4 5
10 2
2 4
9 2
Yes
6 7
6 2
7 5
4 2
4 3
4 8
1 4
Yes
4 3
2 9
2 3
8 7
6 5
5 7
5 9
5 1
Yes
1 9
8 10
4 6
6 1
1 7
10 2
2 6
3 2
2 5
Yes
5 4
2 6
1 3
5 7
7 1
6 7
Yes
12 3
7 8
8 2
10 6
9 10
5 4
2...

result:

wrong answer case 399, participant's output is not a tree