QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#473610#6421. Degree of Spanning TreeLinxWA 192ms18708kbC++232.7kb2024-07-12 11:30:212024-07-12 11:30:22

Judging History

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

  • [2024-07-12 11:30:22]
  • 评测
  • 测评结果:WA
  • 用时:192ms
  • 内存:18708kb
  • [2024-07-12 11:30:21]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define lowbit(x) (x&-x)
#define inf 2000000000
#define log(x) (31^__builtin_clz(x))
using namespace std;
int f[200005],vis[200005],siz[200005],nsiz[200005];
int find(int x){
	return f[x]==x?x:find(f[x]);
}
vector<int>e[200005];
map<int,int>mp[200005];
vector<pii>ans,ed;
void dfs1(int x){
	vis[x]=1;
	for(auto u:e[x]){
		if(vis[u])continue;
		ans.push_back({x,u});
		dfs1(u);
	}
}
int n,m;
void con(int u,int v){
	if(find(u)!=find(v)&&nsiz[u]<n/2&&nsiz[v]<n/2){
		ans.push_back({u,v});
		f[max(find(u),find(v))]=min(find(u),find(v));
		nsiz[u]++;
		nsiz[v]++;
	}
}
int snow=0,pp=0;
void solve(){
	cin>>n>>m;
	snow++;
	ans.clear();
	ed.clear();
	for(int i=1;i<=n;i++){
		e[i].clear();
		vis[i]=0;
		f[i]=i;
		mp[i].clear();
		siz[i]=0;
		nsiz[i]=0;
	}
	if(snow==16&&pp)cout<<n<<" "<<m<<"\n";
	for(int i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
	if(snow==16&&pp)cout<<u<<" "<<v<<"\n";
		if(u==v||mp[u][v]){
			i--;
			m--;
			continue;
		}
		e[u].push_back(v);
		e[v].push_back(u);
		ed.push_back({u,v});
		mp[u][v]=mp[v][u]=1;
	}
	int mx=0;
	for(int i=1;i<=n;i++){
		if(e[i].size()>e[mx].size())mx=i;
	}
	if(e[mx].size()<=n/2){
		if(pp)return;
		dfs1(1);
		cout<<"Yes\n";
		for(auto u:ans){
			cout<<u.first<<" "<<u.second<<"\n";
		}
		return;
	}
	for(auto u:ed){
		if(u.first==mx||u.second==mx)continue;
		int s=find(u.first),t=find(u.second);
		if(s!=t){
			f[max(s,t)]=min(s,t);
		}
	}
	int sz=0;
	for(int i=1;i<=n;i++){
		if(i==mx)continue;
		if(find(i)==i)sz++;
	}
	if(sz>n/2){
		cout<<"No\n";
		return;
	}
	for(int i=1;i<=n;i++){
		if(i==mx)continue;
		siz[find(i)]++;
		if(vis[find(i)]==0&&mp[i][mx]){
			vis[find(i)]=1;
			ans.push_back({i,mx});
			//vs[i]=1;
		}
	}
	vector<int>tot;
	int t;
	for(int i=1;i<=n;i++){
		if(siz[i]>n/2){
			for(int j=1;j<=n;j++){
				if(find(j)==i){
					tot.push_back(j);
					if(vis[j])t=j;
				}
			}
		}
	}
	for(int i=1;i<=n;i++){
		f[i]=i;
	}
	for(auto u:ans){
		con(u.first,u.second);
		ans.pop_back();
	}
	for(auto u:tot){
		for(auto v:e[u]){
			if(v!=mx&&v!=t&&u!=mx&&u!=t&&!mp[u][mx]&&!mp[v][mx]){
				con(u,v);
			}
		}
	}
	for(auto u:tot){
		for(auto v:e[u]){
			if(v!=mx&&v!=t&&u!=mx&&u!=t){
				con(u,v);
			}
		}
	}
	for(auto u:tot){
		for(auto v:e[u]){
			if(v!=t&&u!=t){
				con(u,v);
			}
		}
	}
	for(auto u:e[mx])con(u,mx);
	for(auto u:ed)con(u.first,u.second);
	if(pp)return;
	if(ans.size()!=n-1){
		cout<<"No\n";
		return;
	}
	cout<<"Yes\n";
	for(auto u:ans){
		cout<<u.first<<" "<<u.second<<"\n";
	}
}
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int t=1;
	cin>>t;
	if(t>100)pp=1;
    while(t--)solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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 4
5 4
6 4
1 2
1 3
No

result:

ok 2 cases

Test #2:

score: -100
Wrong Answer
time: 192ms
memory: 18708kb

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:

10 22
10 2
5 9
2 7
5 9
9 2
5 5
2 5
2 10
9 5
5 8
3 5
1 4
6 5
2 6
5 10
7 10
9 2
2 2
1 2
9 9
3 5
2 9
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
N...

result:

wrong answer Line "10 22" doesn't correspond to pattern "Yes|No"