QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#208537#6421. Degree of Spanning TreersjWA 207ms37524kbC++142.1kb2023-10-09 18:34:362023-10-09 18:34:36

Judging History

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

  • [2023-10-09 18:34:36]
  • 评测
  • 测评结果:WA
  • 用时:207ms
  • 内存:37524kb
  • [2023-10-09 18:34:36]
  • 提交

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==127) 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==127) 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;
		else if(v==r) vis[u]=1;
		else uni(u,v);
		// cout<<"pre in"<<u<<" "<<v<<endl;
	}
	for(auto x:d) {
		tie(u,v)=x;
		if(u==r) ;
		else if(v==r) ;
		else uni(u,v);
	}
	for(auto x:e) {
		tie(u,v)=x;
		if(u!=r&&v!=r&&!check(u,v)) {
			
			in[u]++,in[v]++;
			i=find(u),j=find(v);
			if(in[i]<in[j]) swap(i,j);
			in[i]--,in[r]--;
			
			if(in[u]>n/2||in[v]>n/2) {
				in[u]--,in[v]--;
				in[r]++,in[i]++;
				continue;
			}
			
			if(vis[i]==0) {
				cout<<u<<" "<<v<<endl;
				cout<<i<<" "<<j<<endl; 
				cout<<vis[i]<<" "<<vis[j]<<endl;
			}
			
			//assert(vis[i]);
			//assert(vis[j]);
			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: 9572kb

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

5 15
4 5
1 3
4 1
3 5
5 3
4 5
4 2
1 5
3 2
3 4
1 5
5 2
3 2
1 4
3 4
No
No
Yes
1 2
Yes
1 2
Yes
1 2
No

result:

wrong answer Line "5 15" doesn't correspond to pattern "Yes|No"