QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#208518#6421. Degree of Spanning TreersjWA 315ms37524kbC++141.7kb2023-10-09 18:19:382023-10-09 18:19:39

Judging History

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

  • [2023-10-09 18:19:39]
  • 评测
  • 测评结果:WA
  • 用时:315ms
  • 内存:37524kb
  • [2023-10-09 18:19:38]
  • 提交

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;
void get() {
	e.clear();
	d.clear();
	int n,m,u,v,i,j;
	cin>>n>>m;
	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(!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);
		}
	}
	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;
		if(v==r) vis[u]=1;
	}
	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]--;
			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: 9796kb

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: 315ms
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:

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 32, participant's output is not a tree