QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152459#6421. Degree of Spanning Treeqzez#WA 138ms4384kbC++142.4kb2023-08-28 09:28:332023-08-28 09:28:35

Judging History

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

  • [2023-08-28 09:28:35]
  • 评测
  • 测评结果:WA
  • 用时:138ms
  • 内存:4384kb
  • [2023-08-28 09:28:33]
  • 提交

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:S)if(e.first==rt||e.second!=rt){
	// 	merge(e.first,e.second);
	// }
	// debug(ary(deg,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);
	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){
		// debug(1);
		puts("Yes");
		for(auto e:ans)printf("%d %d\n",e.first,e.second);
	}else{
		// debug(ary(deg,1,n));
		iota(fa,fa+1+n,0);
		for(auto e:ans)if(e.first!=rt&&e.second!=rt){
			merge(e.first,e.second);
		}
		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;
		}
		// debug(u,v);
		if(!u){
			puts("No");
		}else{
			iota(fa,fa+1+n,0);
			merge(u,v);
			res.push_back({u,v});
			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: 3908kb

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: 138ms
memory: 4384kb

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

result:

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