QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#208520#6421. Degree of Spanning TreersjWA 211ms37516kbC++141.7kb2023-10-09 18:21:022023-10-09 18:21:03

Judging History

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

  • [2023-10-09 18:21:03]
  • 评测
  • 测评结果:WA
  • 用时:211ms
  • 内存:37516kb
  • [2023-10-09 18:21:02]
  • 提交

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,cnt;
void get() {
	e.clear();
	d.clear();
	int n,m,u,v,i,j;
	cnt++;
	cin>>n>>m; if(cnt==32) cout<<n<<" "<<m<<endl;
	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(cnt==32) cout<<u<<" "<<v<<endl;
		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);
		}
	}
	if(T>5) return ;
	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() {
	cin>>T;
	while(T--) get();
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 211ms
memory: 37516kb

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:

7 12
6 7
7 5
3 7
6 3
6 5
6 4
2 3
1 7
4 3
3 2
3 2
5 4
No
No
Yes
1 2
Yes
1 2
Yes
1 2
No

result:

wrong answer Line "7 12" doesn't correspond to pattern "Yes|No"