QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#134922#6634. Central SubsetVengeful_Spirit#ML 66ms31504kbC++202.0kb2023-08-05 10:02:292023-08-05 10:02:35

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-05 10:02:35]
  • 评测
  • 测评结果:ML
  • 用时:66ms
  • 内存:31504kb
  • [2023-08-05 10:02:29]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

const int MN = 2e5 + 5;

vector<int> G[MN];
vector<int> ans;
int fa[MN];
int getf(int x) {
    return fa[x] == x ? x : fa[x] = getf(fa[x]);
}
int UP;

// bool vis[MN];

int len[MN];
void dfs(int x, int f) {
    len[x] = 0;
    for(auto &y : G[x]) {
        if(y == f) continue;
        dfs(y, x);
        len[x] = max(len[x], len[y] + 1);
    }
    if(len[x] == UP) {
        ans.push_back(x);
        len[x] = 0;
    }
    if(f == 0 && len[x] > 0) ans.push_back(x);
}

void solve() {
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; ++i) G[i].clear(), fa[i] = i;

    // vector<pair<int,int>> e;

    for(int i = 1; i <= m; ++i) {
        int x, y;
        cin >> x >> y;
        if(getf(x) != getf(y)) {
            G[x].push_back(y);
            G[y].push_back(x);
            fa[x] = y;
        }
        // G[x].push_back(y);
        // G[y].push_back(x);
    }

    UP = (int)sqrt(n);
    while(1ll * UP * UP < n) ++UP;
    ans.clear();
    // cerr << UP << endl;
    
    // for(int i = 1; i <= n; ++i) vis[i] = 0;

    // vector<int> ans;

    dfs(1, 0);

    // for(int i = 1; i <= n; ++i) if(!vis[i]) {
    //     ans.push_back(i);
    //     queue<pair<int, int>> q;
    //     q.push({i, 0});
    //     while(!q.empty()) {
    //         auto [x, d] = q.front();
    //         q.pop();
    //         if(d < UP) {
    //             for(auto &y : G[x]) {
    //                 if(!vis[y]) {
    //                     vis[y] = 1;
    //                     q.push({y ,d + 1});
    //                 }
    //             }
    //         }
    //     }
    // }

    // assert(ans.size() <= UP);
    sort(ans.begin(), ans.end());
    if(ans.size() > UP) {
        cout << "-1\n";
        return;
    }
    cout << ans.size() << "\n";
    for(auto &x : ans) cout << x << " " ; 
    cout << "\n";
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;
    while(T--) {
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
4 3
1 2
2 3
3 4
6 7
1 2
2 3
3 1
1 4
4 5
5 6
6 4

output:

2
1 2 
1
1 

result:

ok correct (2 test cases)

Test #2:

score: 0
Accepted
time: 8ms
memory: 9672kb

input:

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

output:

4
1 3 7 11 
1
1 
2
1 2 
1
1 
1
1 
3
1 3 6 
1
1 
2
1 4 
3
1 3 10 
1
1 
4
1 5 10 15 
2
1 3 
2
1 2 
2
1 5 
1
1 
4
1 3 7 11 
1
1 
1
1 
2
1 2 
1
1 
2
1 2 
2
1 3 
2
1 4 
2
1 5 
1
1 
4
1 3 7 11 
1
1 
2
1 5 
1
1 
1
1 
4
1 6 11 16 
2
1 2 
2
1 4 
3
1 4 7 
1
1 
2
1 3 
2
1 2 
2
1 3 
3
1 6 7 
1
1 
3
1 4 8 
1
1 
...

result:

ok correct (10000 test cases)

Test #3:

score: 0
Accepted
time: 27ms
memory: 9396kb

input:

100
2000 1999
529 528
885 884
1221 1222
375 374
245 244
758 757
711 710
1521 1522
1875 1874
749 750
823 822
1959 1958
1767 1766
155 154
631 632
825 824
1330 1331
457 456
1344 1343
1817 1818
413 414
582 583
1828 1827
1335 1336
654 655
162 161
1668 1667
1966 1967
1472 1471
1185 1184
518 517
1509 1510
...

output:

45
1 20 65 110 155 200 245 290 335 380 425 470 515 560 605 650 695 740 785 830 875 920 965 1010 1055 1100 1145 1190 1235 1280 1325 1370 1415 1460 1505 1550 1595 1640 1685 1730 1775 1820 1865 1910 1955 
1
1 
23
1 11 56 101 146 191 236 281 326 371 416 461 506 551 596 641 686 731 776 821 866 911 956 
6...

result:

ok correct (100 test cases)

Test #4:

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

input:

10
14914 14913
13959 13958
3643 3642
4582 4581
13378 13379
981 980
12901 12902
12355 12356
14692 14691
9670 9669
14632 14631
1441 1440
1367 1368
6237 6238
8297 8298
1021 1020
5096 5097
4773 4774
7778 7779
3013 3014
5536 5535
11621 11620
13904 13903
3050 3049
14179 14178
7471 7472
13380 13381
7403 74...

output:

122
1 31 154 277 400 523 646 769 892 1015 1138 1261 1384 1507 1630 1753 1876 1999 2122 2245 2368 2491 2614 2737 2860 2983 3106 3229 3352 3475 3598 3721 3844 3967 4090 4213 4336 4459 4582 4705 4828 4951 5074 5197 5320 5443 5566 5689 5812 5935 6058 6181 6304 6427 6550 6673 6796 6919 7042 7165 7288 741...

result:

ok correct (10 test cases)

Test #5:

score: 0
Accepted
time: 36ms
memory: 9852kb

input:

10
20000 19999
6831 6760
15763 15900
10362 10184
5821 5880
17555 17389
16708 16574
11592 11436
186 209
19380 19313
8867 8718
12100 12237
16245 16110
18464 18568
4713 4665
17412 17578
18666 18750
4360 4322
12350 12502
4054 4103
2874 2849
8097 8202
14489 14639
1056 1016
13500 13581
2435 2391
199 173
8...

output:

4
1 1143 3269 6126 
5
1 176 178 8283 10809 
5
1 471 3276 8040 11257 
5
1 3415 3873 9863 10584 
6
1 1487 8665 8672 14623 15169 
6
1 207 2577 4075 6159 15046 
7
1 256 972 1059 6581 8593 15985 
6
1 266 2418 5415 6192 7843 
6
1 27 1025 1088 1588 14312 
5
1 2515 7775 8799 8901 

result:

ok correct (10 test cases)

Test #6:

score: 0
Accepted
time: 66ms
memory: 31504kb

input:

1
200000 199999
136649 136648
44943 44944
7148 7149
50332 50333
149967 149966
28976 28975
78549 78550
178698 178697
96434 96433
7859 7858
88976 88977
23348 23347
161682 161681
125393 125392
67892 67893
73592 73593
179054 179055
110841 110842
163714 163715
7982 7981
56309 56310
196486 196485
19176 19...

output:

447
1 192 640 1088 1536 1984 2432 2880 3328 3776 4224 4672 5120 5568 6016 6464 6912 7360 7808 8256 8704 9152 9600 10048 10496 10944 11392 11840 12288 12736 13184 13632 14080 14528 14976 15424 15872 16320 16768 17216 17664 18112 18560 19008 19456 19904 20352 20800 21248 21696 22144 22592 23040 23488 ...

result:

ok correct (1 test case)

Test #7:

score: 0
Accepted
time: 59ms
memory: 23912kb

input:

1
200000 199999
58280 58281
132016 32016
45157 45158
35446 35445
158979 58979
185831 85831
74289 174289
195645 95645
31857 131857
168766 68766
95607 95606
39817 39818
58215 158215
74893 74894
18897 118897
63013 163013
58501 58502
94475 194475
77574 77573
152977 52977
3731 103731
20407 20408
186570 8...

output:

224
1 97 545 993 1441 1889 2337 2785 3233 3681 4129 4577 5025 5473 5921 6369 6817 7265 7713 8161 8609 9057 9505 9953 10401 10849 11297 11745 12193 12641 13089 13537 13985 14433 14881 15329 15777 16225 16673 17121 17569 18017 18465 18913 19361 19809 20257 20705 21153 21601 22049 22497 22945 23393 238...

result:

ok correct (1 test case)

Test #8:

score: 0
Accepted
time: 56ms
memory: 16136kb

input:

1
200000 199999
84088 84001
74829 74679
40726 41179
113019 113238
112813 113025
77336 77177
60908 61208
4521 4639
144249 144094
102763 102692
112856 113070
2428 2356
114005 113754
168454 168270
114538 114311
36802 36341
170182 170306
31641 32012
92503 92395
143570 143702
6871 6715
51503 51997
140883...

output:

6
1 4044 81894 107919 118805 149019 

result:

ok correct (1 test case)

Test #9:

score: -100
Memory Limit Exceeded

input:

1000
11 19
8 11
4 11
2 11
2 3
8 3
6 1
6 4
11 5
5 3
10 8
7 10
4 7
3 9
5 1
5 7
3 6
10 1
11 7
2 9
70 109
32 69
26 15
65 46
70 62
50 23
17 16
15 31
2 23
18 11
48 57
19 29
52 42
26 31
7 1
53 66
5 69
58 20
59 38
3 4
9 53
7 56
52 66
66 28
22 51
2 6
22 35
5 28
25 51
27 13
26 56
10 50
53 56
60 48
67 33
61 23...

output:


result: