QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#224783#2012. 树的重心jrjyy100 ✓792ms61796kbC++202.2kb2023-10-23 14:25:332023-10-23 14:25:33

Judging History

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

  • [2023-10-23 14:25:33]
  • 评测
  • 测评结果:100
  • 用时:792ms
  • 内存:61796kb
  • [2023-10-23 14:25:33]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

template <typename T> struct Fenwick {
    int n; std::vector<T> a;
    Fenwick(int n = 0) { init(n); }
    void init(int m) { n = m, a.assign(n, T()); }
    void add(int p, const T &x) {
        for (++p; p <= n; p += p & -p) a[p - 1] += x;
    }
    T sum(int p) const {
        auto x = T(); for (; p; p -= p & -p) x += a[p - 1];
        return x;
    }
    T rangeSum(int l, int r) const { return sum(r) - sum(l); }
};

void solve() {
    int n; std::cin >> n;
    std::vector<std::vector<int>> adj(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v; std::cin >> u >> v, --u, --v;
        adj[u].push_back(v), adj[v].push_back(u);
    }

    std::vector<int> sz(n, 1), fa(n, -1);
    auto dfs1 = [&](auto self, int u) -> void {
        for (auto v : adj[u]) if (v != fa[u])
            fa[v] = u, self(self, v), sz[u] += sz[v];
    };
    dfs1(dfs1, 0);

    Fenwick<int> fen0(n), fen1(n);
    for (int i = 0; i < n; ++i) fen1.add(sz[i] - 1, 1);

    i64 ans = 0;
    auto dfs2 = [&](auto self, int u) -> void {
        std::multiset<int> s{0};
        for (auto v : adj[u]) s.insert(v == fa[u] ? n - sz[u] : sz[v]);

        auto get = [&](int x) {
            auto it = std::prev(s.end()); if (*it == x) --it;
            return std::pair{std::max(2 * x - n, 1) - 1, std::max(n - 2 * *it, 0)};
        };

        int sum = 0;
        auto [L, R] = get(n - sz[u]); int X = fen0.rangeSum(L, R);
        for (auto v : adj[u]) if (v != fa[u]) {
            auto [l, r] = get(sz[v]); int x = fen0.rangeSum(l, r);

            fen1.add(sz[u] - 1, -1), fen1.add(n - sz[v] - 1, 1);
            self(self, v);
            fen1.add(n - sz[v] - 1, -1), fen1.add(sz[u] - 1, 1);

            if (l < r) sum += fen0.rangeSum(l, r) - x;
        }
        fen0.add(sz[u] - 1, 1);
        if (L < R) sum += fen1.rangeSum(L, R) - (fen0.rangeSum(L, R) - X);

        ans += 1ll * sum * (u + 1);
    };
    dfs2(dfs2, 0);

    std::cout << ans << '\n';
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int t; std::cin >> t;
    while (t--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 0ms
memory: 3648kb

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:

73
78
71
78
61

result:

ok 5 number(s): "73 78 71 78 61"

Test #2:

score: 5
Accepted
time: 0ms
memory: 3688kb

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:

64
64
61
69
60

result:

ok 5 number(s): "64 64 61 69 60"

Test #3:

score: 5
Accepted
time: 1ms
memory: 3692kb

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:

54835
58386
59276
37467
56268

result:

ok 5 number(s): "54835 58386 59276 37467 56268"

Test #4:

score: 5
Accepted
time: 1ms
memory: 3924kb

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:

49937
58078
35079
56997
50641

result:

ok 5 number(s): "49937 58078 35079 56997 50641"

Test #5:

score: 5
Accepted
time: 1ms
memory: 3856kb

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:

60385
55734
50093
50291
50868

result:

ok 5 number(s): "60385 55734 50093 50291 50868"

Test #6:

score: 5
Accepted
time: 4ms
memory: 3808kb

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:

4415301
5174782
4892782
3652307
5974061

result:

ok 5 number(s): "4415301 5174782 4892782 3652307 5974061"

Test #7:

score: 5
Accepted
time: 4ms
memory: 3868kb

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:

5138639
3952166
3122285
3472479
5741008

result:

ok 5 number(s): "5138639 3952166 3122285 3472479 5741008"

Test #8:

score: 5
Accepted
time: 4ms
memory: 3812kb

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:

4417145
3733704
3195185
5409231
5428398

result:

ok 5 number(s): "4417145 3733704 3195185 5409231 5428398"

Test #9:

score: 5
Accepted
time: 117ms
memory: 24636kb

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:

3748673669
3748672930
3748662702
3748645661
3748626824

result:

ok 5 number(s): "3748673669 3748672930 3748662702 3748645661 3748626824"

Test #10:

score: 5
Accepted
time: 120ms
memory: 25712kb

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:

3748681091
3748673027
3748632619
3748681188
3748655226

result:

ok 5 number(s): "3748681091 3748673027 3748632619 3748681188 3748655226"

Test #11:

score: 5
Accepted
time: 122ms
memory: 25752kb

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:

3748681091
3748673027
3748632619
3748681188
3748655226

result:

ok 5 number(s): "3748681091 3748673027 3748632619 3748681188 3748655226"

Test #12:

score: 5
Accepted
time: 609ms
memory: 22332kb

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:

84574742390
109565525676
65160768012
75913832852
75507381681

result:

ok 5 number(s): "84574742390 109565525676 65160768012 75913832852 75507381681"

Test #13:

score: 5
Accepted
time: 604ms
memory: 22068kb

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:

69299203766
69180977724
60241673382
85059049735
74362083283

result:

ok 5 number(s): "69299203766 69180977724 60241673382 85059049735 74362083283"

Test #14:

score: 5
Accepted
time: 613ms
memory: 23112kb

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:

66071449320
112893418366
81195207726
81558798680
81341220820

result:

ok 5 number(s): "66071449320 112893418366 81195207726 81558798680 81341220820"

Test #15:

score: 5
Accepted
time: 603ms
memory: 21984kb

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:

89386489871
104106025313
77493369473
75581305725
100017003326

result:

ok 5 number(s): "89386489871 104106025313 77493369473 75581305725 100017003326"

Test #16:

score: 5
Accepted
time: 220ms
memory: 21440kb

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:

12305515311
11199441088
13766309144
9561148642
15774038413

result:

ok 5 number(s): "12305515311 11199441088 13766309144 9561148642 15774038413"

Test #17:

score: 5
Accepted
time: 495ms
memory: 38392kb

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:

47819479252
58197550266
30771078369
33526588657
35360501058

result:

ok 5 number(s): "47819479252 58197550266 30771078369 33526588657 35360501058"

Test #18:

score: 5
Accepted
time: 502ms
memory: 42800kb

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:

26717669079
31115091879
49229636814
56437623753
46904588024

result:

ok 5 number(s): "26717669079 31115091879 49229636814 56437623753 46904588024"

Test #19:

score: 5
Accepted
time: 791ms
memory: 50688kb

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:

74615066593
101428557767
123625710605
120096837084
93875221231

result:

ok 5 number(s): "74615066593 101428557767 123625710605 120096837084 93875221231"

Test #20:

score: 5
Accepted
time: 792ms
memory: 61796kb

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:

67086038686
84880228165
108267767309
114819760710
134167493019

result:

ok 5 number(s): "67086038686 84880228165 108267767309 114819760710 134167493019"