QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#174301#6634. Central Subsetcada_dia_mas_insanos#TL 1822ms16388kbC++172.3kb2023-09-10 06:15:242023-09-10 06:15:25

Judging History

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

  • [2023-09-10 06:15:25]
  • 评测
  • 测评结果:TL
  • 用时:1822ms
  • 内存:16388kb
  • [2023-09-10 06:15:24]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 10, oo = 1e9;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

int n, m;
int dis[maxn], in[maxn];
vector <int> graph[maxn];

void bfs(int root) {
    for (int i=0; i <n; i++) dis[i]=oo;
    queue <int> q;
    for (int i=0; i < n; i++) if (in[i]) dis[i]=0, q.push(i);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        for(int&v : graph[u]) {
            if (dis[v] > dis[u]+1) {
                dis[v]=dis[u]+1;
                q.push(v);
            }
        }
    }
}
void clean() {
    for (int i=0; i < n; i++) {
        graph[i].clear();
		in[i]=0;
        dis[i]=oo;
    }
}
int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    #ifdef LOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif // LOCAL

    int tt; cin >> tt;
    while (tt--) {
        cin >> n >> m;
        for (int i=0; i<m; i++) {
            int u, v; cin >> u >> v;
            u--, v--;
            graph[u].push_back(v);
            graph[v].push_back(u);
        }

        bfs(rand(0, n-1));
        int limit = ceil(sqrt(n));
        vector <int> taken;
        while (taken.size() < limit) {
            int w=-1;
            for (int i=0; i<n; i++) {
                if (in[i]) continue;
                if (w == -1 || dis[i]>dis[w]) w=i;
            }
			if (w == -1) break;
            taken.push_back(w);
            in[w]=1;
            bfs(w);
        }

        bool ok=1;
        queue <int> q;
        for (int i=0; i <n; i++) dis[i]=oo;
        for (int& u : taken) q.push(u), dis[u]=0;
        while (!q.empty()) {
            int u = q.front(); q.pop();
            for(int& v : graph[u]) {
                if (dis[v] > dis[u]+1) {
                    dis[v] = dis[u]+1;
                    q.push(v);
                }
            }
        }
        for (int i=0; i<n; i++) ok &= dis[i]<=limit;
        if (!ok) cout << -1 << endl;
        else {
            cout << taken.size() << endl;
            for (int& i : taken) cout << i+1 << ' ';
            cout << endl;
        }
        clean();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 8312kb

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

result:

ok correct (2 test cases)

Test #2:

score: 0
Accepted
time: 67ms
memory: 9364kb

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

result:

ok correct (10000 test cases)

Test #3:

score: 0
Accepted
time: 121ms
memory: 8928kb

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 2000 1000 1500 500 750 1250 1750 250 375 625 875 1125 1375 1625 1875 125 63 187 312 437 562 687 812 937 1062 1187 1312 1437 1562 1687 1812 1937 32 94 156 218 281 343 406 468 531 593 656 718 
45
1 1983 1728 1572 1697 1463 1648 1774 1949 1951 1332 1429 1624 1637 1889 822 1227 1607 1673 1938 1263 ...

result:

ok correct (100 test cases)

Test #4:

score: 0
Accepted
time: 308ms
memory: 9060kb

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:

123
1 14914 7457 3729 11185 1865 5593 9321 13049 933 2797 4661 6525 8389 10253 12117 13981 467 1399 2331 3263 4195 5127 6059 6991 7923 8855 9787 10719 11651 12583 13515 14447 234 700 1166 1632 2098 2564 3030 3496 3962 4428 4894 5360 5826 6292 6758 7224 7690 8156 8622 9088 9554 10020 10486 10952 1141...

result:

ok correct (10 test cases)

Test #5:

score: 0
Accepted
time: 616ms
memory: 10384kb

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:

142
1 20000 14594 19977 19093 7289 6991 18322 19941 15162 11213 13851 14898 19811 3654 19019 19008 19834 16293 8991 3321 3711 13573 6716 2557 12588 19606 5668 16353 9579 19591 2738 2306 14658 18039 13271 7126 18950 18442 15467 11591 18731 12326 4186 19229 5340 6276 5271 1466 12284 2851 7189 8319 184...

result:

ok correct (10 test cases)

Test #6:

score: 0
Accepted
time: 1755ms
memory: 16156kb

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:

448
1 200000 100000 150000 50000 75000 125000 175000 25000 37500 62500 87500 112500 137500 162500 187500 12500 18750 31250 43750 56250 68750 81250 93750 106250 118750 131250 143750 156250 168750 181250 193750 6250 9375 15625 21875 28125 34375 40625 46875 53125 59375 65625 71875 78125 84375 90625 968...

result:

ok correct (1 test case)

Test #7:

score: 0
Accepted
time: 1822ms
memory: 16388kb

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:

448
1 100001 150001 125001 175000 137501 187500 112501 162500 118751 131251 143751 168750 181250 193750 106251 156250 109376 115626 121876 128126 134376 140626 146876 159375 165625 171875 178125 184375 190625 196875 103126 153125 101564 104688 107813 110938 114063 117188 120313 123438 126563 129688 ...

result:

ok correct (1 test case)

Test #8:

score: -100
Time Limit Exceeded

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:


result: