QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#207833#6421. Degree of Spanning TreersjWA 363ms51112kbC++141.6kb2023-10-08 21:12:222023-10-08 21:12:23

Judging History

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

  • [2023-10-08 21:12:23]
  • 评测
  • 测评结果:WA
  • 用时:363ms
  • 内存:51112kb
  • [2023-10-08 21:12:22]
  • 提交

answer

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
struct edge {
	int to;
	edge *nex;
}*head[N];
void add(int u,int v) {
	edge *cur=new edge;
	cur->to=v;
	cur->nex=head[u];
	head[u]=cur;
}
int f[N];
int find(int x) {
	if(f[x]==x) return x;
	return f[x]=find(f[x]);
}
void uni(int x,int y) {
	f[find(x)]=find(y);
}
bool check(int x,int y) {
	return find(x)==find(y);
}
int in[N];
set<pair<int,int>> e;
vector<pair<int,int>> ee;
pair<int,int> mp(int x,int y) {
	if(x>y) swap(x,y);
	return make_pair(x,y);
}
int get() {
	e.clear(); ee.clear();
	int n,m,i,r=0,u,v;
	cin>>n>>m;
	for(i=1;i<=n;i++) head[i]=0,f[i]=i,in[i]=0;
	for(i=1;i<=m;i++) {
		cin>>u>>v;
		if(u==v) continue;
		if(u>v) swap(u,v); 
		ee.push_back({u,v});
		add(u,v),add(v,u);
		if(!check(u,v)) {
			in[u]++,in[v]++,uni(u,v);
			e.insert({u,v});
		}
	}
	for(i=1;i<=n;i++) if(in[i]>n/2) r=i;
	if(!r) {
		cout<<"Yes\n";
		for(auto x:e) {
			tie(u,v)=x;
			cout<<u<<" "<<v<<endl; 
		}
		return 0;
	}
	for(auto x:ee) {
		tie(u,v)=x;
		if(u!=r&&v!=r&&e.find(x)==e.end()) {
			if(e.find(mp(u,r))!=e.end() && e.find(mp(v,r))!=e.end()) {
				if(in[u]<in[v]) swap(u,v);
				if(in[v]+1>n/2) continue;
				e.erase(mp(u,r)); in[u]--,in[r]--;
				e.insert(mp(u,v)); in[u]++,in[v]++;
			}
		}
	}
	if(in[r]>n/2) cout<<"No\n";
	else {
		cout<<"Yes\n";
		for(auto x:e) {
			tie(u,v)=x;
			cout<<u<<" "<<v<<endl; 
		}
		return 0;
	}
	return 0;
}
int main() {
	// freopen("2.cpp","r",stdin);
	int T; cin>>T;
	while(T--) get();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3668kb

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: 363ms
memory: 51112kb

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

result:

wrong answer case 99, participant does not find a solution but the jury does