QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#787463#6421. Degree of Spanning TreePhangAC ✓374ms21284kbC++142.7kb2024-11-27 11:58:462024-11-27 11:58:51

Judging History

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

  • [2024-11-27 11:58:51]
  • 评测
  • 测评结果:AC
  • 用时:374ms
  • 内存:21284kb
  • [2024-11-27 11:58:46]
  • 提交

answer

#include<bits/stdc++.h>
#define rep1(i, a, b) for(int i = a; i <= b; ++i)
#define rep2(i, a, b) for(int i = a; i >= b; --i)
#define ft first
#define sd second
#define pii pair <int, int>
#define ll long long
#define ld long double
#define pb push_back
#define debug puts("--------------------")
const int N = 2e5 + 10;
using namespace std;
int T, rt, d[N], pa[N], vis[N], id[N]; pii ed[N];
vector <pii> e[N];
int find(int x) {return x == pa[x] ? x : pa[x] = find(pa[x]);}
void hb(int u, int v, int i) {
    int x = find(u), y = find(v);
    if(x == y) return ;
//    cout << i << ' ' << u << ' ' << v << '\n';
//    ++d[u]; ++d[v];
    vis[i] = 1; pa[x] = y;
}
void dfs(int u, int fa) {
//	cout << u << ' ' << fa << '\n'; 
    for(auto v : e[u]) if(v.ft ^ fa && vis[v.sd]) {
        if(u ^ rt) hb(v.ft, u, v.sd);
        dfs(v.ft, u);
    }
}
int main() {
    cin >> T;
    int cnt = 0;
    while(T--) {
        int n, m; cin >> n >> m;
        rep1(i, 1, n) pa[i] = i, id[i] = d[i] = 0, e[i].clear();
        rep1(i, 1, m) {
            int u, v; cin >> u >> v;
            ed[i] = {u, v}; vis[i] = 0;
            e[u].pb({v, i}); e[v].pb({u, i});
            if(find(u) ^ find(v)) ++d[u], ++d[v];
			hb(u, v, i);
        }
//        if(++cnt == 11122) {
//            puts("Yes");
//            cout << n << ',' << m << '|';
//            rep1(i, 1, m) {
//                cout << ed[i].ft << ',' << ed[i].sd << '|';
//            } cout << '\n'; continue;
//        }
        rt = 0;
//        debug;
        rep1(i, 1, n) {
            pa[i] = i;
            if(d[i] > d[rt]) rt = i;
        } dfs(rt, 0);
//        debug;
//        rep1(i, 1, n) cout << d[i] << " \n"[i == n];
        for(auto v : e[rt]) if(vis[v.sd]) id[v.ft] = v.sd;
        rep1(i, 1, m) {
            if(d[rt] <= n / 2) break;
            if(vis[i]) continue;
            int u = ed[i].ft, v = ed[i].sd;
            int x = find(u), y = find(v);
            if(u == rt || v == rt) continue;
            if(x == y) continue;
            if(d[x] > d[y]) swap(x, y);
            vis[i] = 1; ++d[u]; ++d[v];
            vis[id[y]] = 0; --d[y]; --d[rt];
            pa[y] = x;
        }
        if(d[rt] > n / 2 || n == 3) {puts("No"); continue;}
        puts("Yes");
        rep1(i, 1, m) if(vis[i]) cout << ed[i].ft << ' ' << ed[i].sd << '\n';
    }
    return 0;
}
/*
1
9 19
3 8
3 7
3 4
9 8
6 4
3 4
1 3
2 6
4 8
3 9
3 6
3 9
3 2
3 6
5 3
6 3
1 3
3 4
7 9

1
5 9
5 2
4 3
3 2
4 5
3 4
1 2
1 3
4 2
2 5

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

1
5 12
2 5
2 4
4 1
2 2
4 2
1 4
2 3
2 2
2 5
2 3
4 2
5 4

*/

详细

Test #1:

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

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: 0
Accepted
time: 256ms
memory: 15536kb

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

result:

ok 11140 cases

Test #3:

score: 0
Accepted
time: 374ms
memory: 21284kb

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
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
39102 81376
166...

result:

ok 5 cases

Test #4:

score: 0
Accepted
time: 230ms
memory: 20268kb

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
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
98195 38219
...

result:

ok 5 cases

Test #5:

score: 0
Accepted
time: 336ms
memory: 19868kb

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
50735 5447
15119 49666
50735 30352
44756 49555
26546 32695
98445 50735
92212 50735
50735 19382
43688 46767
54630 54562
48877 50735
78593 76833
74317 37258
36579 50735
37536 44353
50735 46602
35088 29568
86657 50735
50735 61268
37159 50735
35719 50735
78147 8985
50735 58204
5089 21674
3651 54202
...

result:

ok 5 cases

Test #6:

score: 0
Accepted
time: 199ms
memory: 18172kb

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

result:

ok 11102 cases

Test #7:

score: 0
Accepted
time: 255ms
memory: 18448kb

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

result:

ok 11102 cases

Test #8:

score: 0
Accepted
time: 223ms
memory: 11500kb

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

output:

Yes
2 5
1 4
1 3
4 2
Yes
4 3
2 1
4 5
2 5
Yes
5 1
4 2
4 3
3 1
Yes
4 1
4 3
3 5
2 5
Yes
5 2
2 1
5 4
4 3
Yes
2 4
5 1
1 3
2 3
Yes
4 2
5 2
3 5
4 1
Yes
1 5
2 3
2 4
3 5
Yes
1 3
5 2
2 4
5 3
Yes
3 1
2 1
5 4
5 3
Yes
5 3
2 4
1 2
1 5
Yes
5 4
2 1
5 3
2 4
Yes
1 3
5 4
5 2
3 4
Yes
4 2
4 3
5 2
5 1
Yes
1 2
4 3
3 1
5 4
...

result:

ok 100000 cases

Test #9:

score: 0
Accepted
time: 173ms
memory: 12476kb

input:

1000
5 970
3 1
4 3
3 1
1 3
2 3
2 3
3 2
2 3
2 3
2 3
3 2
3 2
3 1
2 3
3 2
3 2
2 3
2 3
3 2
3 2
2 3
3 2
2 3
2 3
3 5
2 3
2 3
2 3
3 2
3 2
5 3
2 3
3 2
2 3
3 2
3 2
3 1
3 2
3 5
3 2
2 1
2 3
3 5
2 3
2 3
5 3
3 2
3 1
3 2
3 2
1 3
4 3
3 1
4 3
5 3
3 2
3 4
2 3
3 5
2 3
3 1
3 2
2 3
4 3
2 3
2 3
1 3
3 1
2 3
3 2
3 2
2 3
2...

output:

Yes
4 3
3 5
2 1
5 2
Yes
3 1
3 4
5 2
4 5
Yes
3 5
1 5
2 4
4 3
Yes
4 2
4 1
5 3
5 1
Yes
1 3
4 3
5 2
5 1
Yes
1 5
3 1
2 4
2 3
Yes
4 3
4 5
2 1
1 5
Yes
3 1
1 2
4 5
4 2
Yes
4 2
5 4
1 3
5 3
Yes
5 1
3 1
2 4
5 2
Yes
3 5
3 2
1 4
4 5
Yes
5 2
5 3
4 1
4 3
Yes
1 2
3 2
4 5
1 4
Yes
5 4
5 3
1 2
3 2
Yes
5 2
3 2
4 1
3 1
...

result:

ok 1000 cases

Test #10:

score: 0
Accepted
time: 190ms
memory: 11772kb

input:

100000
5 5
5 1
1 3
3 5
2 3
4 1
5 5
3 1
3 4
1 5
5 2
5 3
5 5
2 5
1 2
4 5
4 3
2 4
5 5
1 2
4 3
2 4
4 1
5 1
5 5
5 2
3 2
1 4
1 2
3 1
5 5
2 5
1 3
4 5
1 2
5 1
5 5
1 5
5 2
4 5
1 4
3 4
5 5
4 2
1 2
1 4
5 1
3 4
5 5
1 3
5 2
5 4
5 1
1 4
5 5
2 3
4 5
2 4
3 4
1 3
5 5
1 4
4 5
2 5
3 4
5 3
5 5
4 1
4 5
4 3
3 2
1 3
5 5
3...

output:

Yes
5 1
3 5
2 3
4 1
Yes
3 1
3 4
1 5
5 2
Yes
2 5
1 2
4 5
4 3
Yes
1 2
4 3
2 4
5 1
Yes
5 2
3 2
1 4
3 1
Yes
2 5
1 3
4 5
1 2
Yes
1 5
5 2
1 4
3 4
Yes
4 2
1 2
5 1
3 4
Yes
1 3
5 2
5 4
1 4
Yes
2 3
4 5
2 4
1 3
Yes
1 4
2 5
3 4
5 3
Yes
4 1
4 5
3 2
1 3
Yes
3 2
1 5
4 1
5 2
Yes
4 2
2 1
5 1
3 5
Yes
2 1
3 4
5 1
5 3
...

result:

ok 100000 cases