QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#68266#2012. 树的重心Lenstar0 0ms0kbC++143.5kb2022-12-15 15:40:252022-12-15 15:40:29

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-15 15:40:29]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2022-12-15 15:40:25]
  • 提交

answer

#include <bits/stdc++.h>

const int bufsize = (1 << 20) + 10, N = 3e5 + 10;

inline char Getchar() {
    static char buf[bufsize], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufsize, stdin), p1 == p2) ? EOF : *p1++;
}

inline int read() {
    int x = 0;
    char ch = Getchar();

    while (!isdigit(ch))
        ch = Getchar();

    while (isdigit(ch))
        x = x * 10 + ch - '0', ch = Getchar();

    return x;
}

int tot = 1, fir[N], nex[N << 1], got[N << 1];

inline void AddEdge(int u, int v) {
    nex[++tot] = fir[u], fir[u] = tot, got[tot] = v;
    nex[++tot] = fir[v], fir[v] = tot, got[tot] = u;
}

int rot = 0, siz[N], Max[N], son[N], par[N];
int idx = 0, dfn[N], b[N], w[N], p[N], a[N];

inline void dfs(int u, int fa, int n) {
    siz[u] = 1, Max[u] = 0;

    for (int i = fir[u]; i; i = nex[i])
        if (got[i] != fa)
            dfs(got[i], u, n), siz[u] += siz[got[i]], Max[u] = std::max(Max[u], siz[got[i]]);

    Max[u] = std::max(Max[u], n - siz[u]);

    if (!rot || Max[u] < Max[rot])
        rot = u;
}

inline void dfs(int u, int fa) {
    siz[u] = 1, Max[u] = son[u] = 0, par[u] = fa, dfn[u] = ++idx;

    for (int i = fir[u]; i; i = nex[i])
        if (got[i] != fa) {
            dfs(got[i], u), siz[u] += siz[got[i]];
            Max[u] = std::max(Max[u], siz[got[i]]);

            if (siz[got[i]] > siz[son[u]])
                son[u] = got[i];
        }

    if (siz[u] == 1)
        w[u] = u;
    else
        w[u] = w[son[u]];

    while (siz[u] - siz[w[u]] > siz[u] / 2)
        w[u] = par[w[u]];
}

int main() {
    freopen("centroid.in", "r", stdin);
    freopen("centroid.out", "w", stdout);
    int T = read();

    while (T--) {
        memset(fir, 0, sizeof(fir)), tot = 1;
        int n = read(), now = 0, cnt = 0, s = 0, tmp = 0;
        long long ans = 0;

        for (int i = 1; i <= n - 1; ++i)
            AddEdge(read(), read());

        idx = rot = 0, dfs(1, 0, n), dfs(rot, 0);
        auto check = [&](int u, int v) {
            return Max[v] <= siz[u] / 2;
        };
        auto Check = [&](int u, int v) {
            return Max[v] <= (n - siz[u]) / 2;
        };

        for (int i = 1; i <= n; ++i)
            if (i != rot)
                ans += w[i] + check(i, par[w[i]]) * par[w[i]];

        for (int now = rot; now; now = son[now])
            p[++cnt] = now;

        p[cnt + 1] = 0;

        for (int i = 1; i <= cnt; ++i)
            for (int j = siz[p[i + 1]] + 1; j <= siz[p[i]]; ++j)
                a[j] = p[i];

        for (int i = fir[rot]; i; i = nex[i])
            if (got[i] != son[rot] && siz[got[i]] > siz[s])
                s = got[i];

        cnt = 0, tmp = son[rot], son[rot] = s;

        for (int now = rot; now; now = son[now])
            p[++cnt] = now;

        p[cnt + 1] = 0;

        for (int i = 1; i <= cnt; ++i)
            for (int j = siz[p[i + 1]] + 1; j <= siz[p[i]]; ++j)
                b[j] = p[i];

        for (int i = 1; i <= n; ++i) {
            if (i == rot)
                continue;

            int w = (n - siz[i]) / 2 + ((n - siz[i]) & 1);

            if (dfn[tmp] <= dfn[i] && dfn[i] < dfn[tmp] + siz[tmp]) {
                ans += b[w] + (Check(i, par[b[w]]) || (par[b[w]] == rot && siz[s] <= (n - siz[i]) / 2)) * par[b[w]];
            } else
                ans += a[w] + Check(i, par[a[w]]) * par[a[w]];
        }

        printf("%lld\n", ans);
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Dangerous Syscalls

input:

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

output:


result:


Test #2:

score: 0
Dangerous Syscalls

input:

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

output:


result:


Test #3:

score: 0
Dangerous Syscalls

input:

5
199
7 46
10 170
17 190
23 22
22 67
31 13
13 116
34 100
36 91
37 195
39 88
40 139
43 187
47 158
48 83
49 128
50 30
54 26
26 169
59 85
60 178
65 191
73 28
28 41
75 56
76 82
77 133
80 55
55 44
81 150
87 92
89 74
90 110
91 123
92 163
93 42
42 44
44 180
95 35
35 66
66 161
96 176
97 3
98 140
101 38
103 ...

output:


result:


Test #4:

score: 0
Dangerous Syscalls

input:

5
199
1 169
4 29
5 117
8 30
14 17
15 71
16 180
18 103
20 193
24 38
29 100
32 12
12 39
33 23
23 108
35 40
36 139
37 40
46 95
49 112
50 106
51 108
52 58
53 151
56 176
57 22
60 184
68 39
70 58
58 67
67 25
71 47
74 170
75 114
76 31
31 34
80 90
81 62
82 83
83 100
85 77
92 146
94 146
98 96
96 156
99 195
1...

output:


result:


Test #5:

score: 0
Dangerous Syscalls

input:

5
199
6 193
7 82
9 39
12 29
13 180
14 101
17 172
19 108
35 66
36 156
42 129
49 55
52 40
54 28
28 112
58 136
61 53
53 74
63 179
65 33
33 110
68 4
71 127
75 4
4 167
76 197
77 150
79 31
31 197
82 18
18 165
86 187
87 178
88 143
90 196
92 66
66 166
94 5
5 78
78 140
95 130
97 154
99 169
101 185
103 74
74 ...

output:


result:


Test #6:

score: 0
Dangerous Syscalls

input:

5
1999
3 1248
6 1223
9 1663
12 360
14 1607
15 530
16 847
21 1010
25 733
28 947
32 1077
34 821
35 674
39 1017
40 967
45 430
50 529
51 1012
52 148
53 719
54 293
55 609
57 1047
58 582
60 142
66 1280
67 1530
68 717
69 70
70 746
71 1874
75 218
76 205
77 1498
80 1185
90 1026
94 711
95 385
98 1165
99 277
1...

output:


result:


Test #7:

score: 0
Dangerous Syscalls

input:

5
1999
1 243
2 1365
7 1088
11 1076
12 1102
15 9
18 516
20 1854
22 82
23 1532
25 809
28 973
31 1873
32 113
39 280
43 1138
44 1225
46 1289
47 948
48 814
52 863
54 1950
55 175
61 178
63 1081
66 282
68 1618
70 1306
71 314
74 771
80 406
81 565
82 777
83 1701
86 1247
87 1505
88 1816
90 77
94 308
96 1269
9...

output:


result:


Test #8:

score: 0
Dangerous Syscalls

input:

5
1999
4 1262
6 1067
10 316
13 1983
14 610
15 945
18 192
22 532
24 688
29 385
30 1545
31 1061
37 1970
38 335
39 487
40 475
44 72
45 1468
47 470
49 873
50 1173
51 88
55 1366
56 1107
57 751
59 1340
60 636
63 1172
65 387
66 1286
69 681
70 489
75 1354
76 1958
77 910
78 1423
80 1696
81 328
83 1001
84 147...

output:


result:


Test #9:

score: 0
Dangerous Syscalls

input:

5
49991
19763 10669
10669 20671
20671 35073
35073 22011
22011 39334
39334 16893
16893 36734
36734 35671
35671 24827
24827 5929
5929 22587
22587 16048
16048 24772
24772 40459
40459 37541
37541 33520
33520 47457
47457 23144
23144 40864
40864 13214
13214 23056
23056 19449
19449 41897
41897 23570
23570 ...

output:


result:


Test #10:

score: 0
Dangerous Syscalls

input:

5
49991
17601 40966
40966 35434
35434 29765
29765 29569
29569 11520
11520 8262
8262 21778
21778 21137
21137 21481
21481 5034
5034 29974
29974 12934
12934 47299
47299 14541
14541 5632
5632 8467
8467 39604
39604 38825
38825 24334
24334 5304
5304 47890
47890 17264
17264 819
819 31896
31896 49966
49966 ...

output:


result:


Test #11:

score: 0
Dangerous Syscalls

input:

5
49991
17601 40966
40966 35434
35434 29765
29765 29569
29569 11520
11520 8262
8262 21778
21778 21137
21137 21481
21481 5034
5034 29974
29974 12934
12934 47299
47299 14541
14541 5632
5632 8467
8467 39604
39604 38825
38825 24334
24334 5304
5304 47890
47890 17264
17264 819
819 31896
31896 49966
49966 ...

output:


result:


Test #12:

score: 0
Dangerous Syscalls

input:

5
262143
146770 175612
146770 60732
175612 53530
175612 157976
60732 129972
60732 8262
53530 208343
53530 241810
157976 54045
157976 125169
129972 77022
129972 12934
8262 139684
8262 132308
208343 202200
208343 113975
241810 96796
241810 231714
54045 214868
54045 206276
125169 254469
125169 177432
7...

output:


result:


Test #13:

score: 0
Dangerous Syscalls

input:

5
262143
79000 96360
79000 91210
96360 171486
96360 192733
91210 113737
91210 175804
171486 165391
171486 148872
192733 50082
192733 84694
113737 124373
113737 151080
175804 28512
175804 195077
165391 185052
165391 170088
148872 3444
148872 159445
50082 261317
50082 44496
84694 224968
84694 158634
1...

output:


result:


Test #14:

score: 0
Dangerous Syscalls

input:

5
262143
80668 139094
80668 22182
139094 143143
139094 134409
22182 60477
22182 162265
143143 227503
143143 1560
134409 85115
134409 185254
60477 128516
60477 35671
162265 138714
162265 205852
227503 211287
227503 118458
1560 122509
1560 173622
85115 161651
85115 200495
185254 33882
185254 63113
128...

output:


result:


Test #15:

score: 0
Dangerous Syscalls

input:

5
262143
134055 112409
134055 173361
112409 142281
112409 158535
173361 29319
173361 78805
142281 38595
142281 58788
158535 138588
158535 255716
29319 35295
29319 233057
78805 136424
78805 137397
38595 21026
38595 233348
58788 150537
58788 159016
138588 250059
138588 154684
255716 102400
255716 1721...

output:


result:


Test #16:

score: 0
Dangerous Syscalls

input:

5
99995
259 33949
24431 46214
70531 47343
30948 7638
78110 39021
66676 61827
68226 23740
447 9624
73693 93832
51936 60316
70665 71118
34707 39970
79419 24476
46318 73713
13318 81365
203 61537
15901 1647
15871 94799
2179 98495
44986 18479
742 59873
21425 22057
53003 83148
1938 71642
96209 12362
85249...

output:


result:


Test #17:

score: 0
Dangerous Syscalls

input:

5
199995
138306 178143
126692 90580
195249 41160
183019 129512
30948 65522
174231 160304
62697 53062
34707 136319
119438 13105
86673 47289
21484 63852
135033 177492
88820 41556
127597 11667
38660 9688
20532 47907
199457 156229
19863 119502
124210 148366
53003 31552
130020 22624
138539 78394
125197 1...

output:


result:


Test #18:

score: 0
Dangerous Syscalls

input:

5
199995
175791 121334
164012 66489
62892 65680
158225 14074
191070 165961
53626 106523
110768 25732
168560 2805
33591 198570
25518 182266
193051 47804
171184 195743
175898 49560
105280 165577
137932 33634
75375 129044
109365 164280
125769 147064
170883 197513
162323 100475
47461 181353
80970 151789...

output:


result:


Test #19:

score: 0
Dangerous Syscalls

input:

5
299995
150147 169846
139337 63773
185064 212052
77320 23419
156180 162102
63142 20120
225131 146038
212549 20016
46406 42169
286305 140266
13104 5614
270163 227814
148262 9967
27434 252557
217908 105623
138578 103358
36469 211475
18717 236263
13867 41398
91671 91198
263412 104644
167079 204277
187...

output:


result:


Test #20:

score: 0
Dangerous Syscalls

input:

5
299995
284734 56982
179603 60848
62377 129299
263286 294600
275034 103994
101598 17127
4082 162607
101427 121742
51111 114853
74024 22822
107874 97512
145157 37145
76679 153213
103753 6327
1108 29718
51455 213273
40667 290500
12308 112958
39082 104741
25356 54011
282325 107031
79497 19021
194864 2...

output:


result: