QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#473641#6421. Degree of Spanning TreeLinxAC ✓514ms51736kbC++232.8kb2024-07-12 11:56:012024-07-12 11:56:01

Judging History

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

  • [2024-07-12 11:56:01]
  • 评测
  • 测评结果:AC
  • 用时:514ms
  • 内存:51736kb
  • [2024-07-12 11:56:01]
  • 提交

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],vs[200005],mn[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]++;
	}
}
void solve(){
	cin>>n>>m;
	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;
		vs[i]=0;
		mn[i]=0;
	}
	for(int i=1;i<=m;i++){
		int u,v;
		cin>>u>>v;
		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;
	}
	vector<pii>ano;
	for(int i=1;i<=n;i++){
		if(i==mx)continue;
		siz[find(i)]++;
		if(mp[i][mx]){
			if(mn[find(i)]==0||e[mn[find(i)]].size()>e[i].size()){
				mn[find(i)]=i;
			}

		}
	}
	for(int i=1;i<=n;i++){
		if(mn[i]){
			vis[find(mn[i])]=1;
			ano.push_back({mn[i],mx});
			vs[mn[i]]=1;
		}
	}
	for(int i=1;i<=n;i++)vis[i]=0;
	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(vs[j])t=j;
				}
			}
		}
	}
	for(int i=1;i<=n;i++){
		f[i]=i;
	}
	for(auto u:ano){
		con(u.first,u.second);
	}
	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(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;
    while(t--)solve();
    return 0;
}

详细

Test #1:

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

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: 0
Accepted
time: 242ms
memory: 24136kb

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

result:

ok 11140 cases

Test #3:

score: 0
Accepted
time: 378ms
memory: 51736kb

input:

5
100000 197799
37555 22723
91160 32701
6451 4416
43186 26432
9750 82955
28292 33907
91534 78442
17771 67061
40351 28116
21494 23114
34672 66640
72636 95093
13033 6538
53698 87837
79541 71230
53985 63500
84753 5378
67971 56748
90951 20169
4465 97293
18331 53564
41043 95738
48579 96439
90865 7526
391...

output:

Yes
1 79669
79669 46478
46478 20316
20316 84268
84268 40465
40465 14915
14915 68623
68623 92552
92552 69369
69369 83614
83614 6578
6578 13594
13594 28402
13594 88085
88085 31060
31060 44184
44184 6712
6712 7014
7014 76420
76420 44744
44744 99998
99998 22734
22734 46156
46156 58014
58014 17801
17801 ...

result:

ok 5 cases

Test #4:

score: 0
Accepted
time: 383ms
memory: 40748kb

input:

5
100000 100000
98195 31806
98195 70169
92153 98195
98195 46320
94369 56771
94369 49988
74295 98195
33796 98195
89903 94369
98195 1814
82388 98195
10189 94369
98195 6267
29845 98195
22425 94369
6241 98195
98195 33204
66516 98195
94369 71364
26277 94369
94369 94722
94369 25349
14629 98195
9329 98195
...

output:

Yes
88570 94369
4 94369
5 94369
7 94369
9 94369
12 94369
14 94369
15 94369
16 94369
20 94369
21 94369
22 94369
23 94369
24 94369
25 94369
26 94369
27 94369
28 94369
30 94369
31 94369
34 94369
36 94369
37 94369
38 94369
39 94369
42 94369
45 94369
46 94369
47 94369
50 94369
52 94369
53 94369
55 94369
...

result:

ok 5 cases

Test #5:

score: 0
Accepted
time: 514ms
memory: 43664kb

input:

5
100000 149998
50735 5447
50735 24875
15119 49666
50735 30352
44756 49555
26546 32695
98445 50735
71657 50735
92212 50735
50735 19382
30935 50735
43688 46767
54630 54562
31371 50735
48877 50735
78593 76833
74317 37258
50735 48236
67116 50735
36579 50735
37536 44353
50735 46602
35088 29568
86657 507...

output:

Yes
1 50735
2 50735
3 50735
4 50735
5 50735
6 50735
7 50735
8 50735
9 50735
10 50735
11 50735
12 50735
13 50735
14 50735
15 50735
16 50735
17 50735
18 50735
19 50735
20 50735
21 50735
22 50735
23 50735
24 50735
25 50735
26 50735
27 50735
28 50735
29 50735
30 50735
31 50735
32 50735
33 50735
34 50735...

result:

ok 5 cases

Test #6:

score: 0
Accepted
time: 276ms
memory: 37940kb

input:

11102
14 14
9 10
10 14
9 11
10 2
9 14
9 5
10 13
9 8
4 9
3 10
10 1
12 10
10 6
9 7
6 6
3 2
3 5
2 6
1 6
3 4
3 6
5 5
3 2
2 1
4 5
2 5
5 3
8 8
5 6
4 6
8 6
8 4
6 1
8 3
2 8
7 8
12 12
8 12
12 2
4 12
9 12
12 3
1 12
1 8
6 8
11 8
7 8
8 10
12 5
15 15
9 15
6 15
13 8
15 3
8 11
15 12
8 2
15 4
8 10
15 8
15 14
8 4
7 ...

output:

Yes
1 10
2 10
3 10
14 10
6 10
12 10
13 10
9 11
9 14
9 5
9 8
4 9
9 7
Yes
2 3
4 3
5 3
2 6
1 6
Yes
1 2
3 2
4 5
5 3
Yes
4 8
2 8
3 8
7 8
5 6
4 6
6 1
Yes
1 12
2 12
3 12
4 12
5 12
9 12
1 8
6 8
11 8
7 8
8 10
Yes
4 8
2 8
5 8
7 8
10 8
11 8
13 8
1 15
3 15
6 15
9 15
12 15
14 15
15 4
Yes
6 3
2 3
7 3
10 3
11 3
12...

result:

ok 11102 cases

Test #7:

score: 0
Accepted
time: 327ms
memory: 41580kb

input:

11102
14 19
9 3
3 6
8 3
2 14
7 3
11 3
13 9
3 14
3 5
2 3
7 4
1 10
13 3
4 3
3 1
3 10
12 8
11 6
3 12
11 15
9 11
10 7
1 2
9 6
11 2
11 10
8 11
11 5
3 8
11 4
11 3
11 7
1 11
5 4
6 11
5 6
1 2
4 3
3 5
1 3
5 4
3 2
12 16
8 11
12 10
12 3
5 7
12 5
1 12
9 4
12 2
6 12
12 8
10 1
12 11
7 12
12 9
3 2
4 12
6 7
3 1
6 3...

output:

Yes
1 3
2 3
4 3
5 3
6 3
8 3
9 3
2 14
13 9
7 4
1 10
12 8
11 6
Yes
1 11
3 11
4 11
6 11
7 11
10 7
1 2
9 6
3 8
5 4
Yes
1 3
4 3
1 2
5 4
Yes
1 12
2 12
4 12
5 12
6 12
8 12
8 11
5 7
9 4
10 1
3 2
Yes
1 3
2 3
4 3
4 6
5 1
Yes
2 1
3 1
6 1
4 3
2 5
Yes
1 6
3 6
4 6
5 6
8 6
1 2
7 5
4 11
3 10
8 9
Yes
1 5
2 5
3 5
4 1...

result:

ok 11102 cases

Test #8:

score: 0
Accepted
time: 208ms
memory: 19088kb

input:

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