QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#473608#6421. Degree of Spanning TreeLinxWA 232ms21780kbC++232.7kb2024-07-12 11:28:472024-07-12 11:28:47

Judging History

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

  • [2024-07-12 11:28:47]
  • 评测
  • 测评结果:WA
  • 用时:232ms
  • 内存:21780kb
  • [2024-07-12 11:28:47]
  • 提交

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,p=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&&p)cout<<n<<" "<<m<<"\n";
	for(int i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
	if(snow==16&&p)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){
		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(p)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)p=1;
    while(t--)solve();
    return 0;
}

詳細信息

Test #1:

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

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: 232ms
memory: 21780kb

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

result:

wrong answer Integer 11 violates the range [1, 9]