QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71445#3845. Colorful TreeIsrothyAC ✓1447ms233316kbC++235.7kb2023-01-10 01:41:252023-01-10 01:41:27

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-10 01:41:27]
  • Judged
  • Verdict: AC
  • Time: 1447ms
  • Memory: 233316kb
  • [2023-01-10 01:41:25]
  • Submitted

answer

#include <algorithm>
#include <iostream>
#include <vector>

const int M = 5e5 + 10;
const int K = 20;
std::vector<std::pair<int, int>> adj[M];
long long ST[K][2 * M], dis[M];
int op[M], x[M], y[M], c[M], d[M], p[M], cnt[M], color[M], pos[M];
int depth[M], eulern[M], Log2[2 * M];
int dfs_clock;

template<class T, class C> struct ZKW_Tree {
    size_t n{};
    std::vector<T> tree;
    ZKW_Tree() = default;
    void init(size_t n, T t) {
        this->n = 1;
        while (this->n < n + 2) {
            this->n <<= 1;
        }
        tree = std::vector<T>(this->n << 1, t);
    }
    void update(int i, T x) {
        for (tree[i += n] = x; i > 1; i >>= 1) {
            auto original = tree[i >> 1];
            tree[i >> 1] = C()(tree[i], tree[i ^ 1]);
            if (tree[i >> 1] == original) {
                break;
            }
        }
    }
    T top() {
        return tree[1];
    }
};

void dfs(int u, int fa) {
    ++dfs_clock;
    ST[0][dfs_clock] = dis[u];
    eulern[u] = dfs_clock;
    for (auto [v, w]: adj[u]) {
        dis[v] = dis[u] + w;
        depth[v] = depth[u] + 1;
        dfs(v, u);
        ++dfs_clock;
        ST[0][dfs_clock] = dis[u];
    }
}

void ST_init() {
    for (int k = 1; k < K; ++k) {
        for (int i = 1; i <= dfs_clock - (1 << k) + 1; ++i) {
            ST[k][i] = std::min(ST[k - 1][i], ST[k - 1][i + (1 << (k - 1))]);
        }
    }
}

long long distance(int u, int v) {
    int x = eulern[u];
    int y = eulern[v];
    if (x > y) {
        std::swap(x, y);
    }
    int k = Log2[y - x + 1];
    return dis[u] + dis[v] - 2 * std::min(ST[k][x], ST[k][y - (1 << k) + 1]);
}
struct merge {
    std::pair<int, int> operator()(std::pair<int, int> p1, std::pair<int, int> p2) {
        if (p1.first == -1) {
            return p2;
        }
        if (p2.first == -1) {
            return p1;
        }
        int nodes[4] = {p1.first, p1.second, p2.first, p2.second};
        std::tuple<long long, int, int> path(-1, -1, -1);
        for (int i = 0; i < 4; ++i) {
            for (int j = i + 1; j < 4; ++j) {
                path = std::max(path, std::make_tuple(distance(nodes[i], nodes[j]), nodes[i], nodes[j]));
            }
        }
        return {std::get<1>(path), std::get<2>(path)};
    }
};
struct merge2 {
    std::pair<std::pair<int, int>, int>
    operator()(std::pair<std::pair<int, int>, int> p1, std::pair<std::pair<int, int>, int> p2) {
        if (p1.first.first == -1) {
            return p2;
        }
        if (p2.first.first == -1) {
            return p1;
        }
        std::pair<std::pair<int, int>, int> ret;
        ret.first = merge()(p1.first, p2.first);
        if (color[ret.first.first] != color[ret.first.second]) {
            ret.second = ret.first.first;
            return ret;
        }
        int nodes[6] = {p1.first.first, p1.first.second, p2.first.first, p2.first.second};
        int m = 4;
        if (p1.second != -1) {
            nodes[m++] = p1.second;
        }
        if (p2.second != -1) {
            nodes[m++] = p2.second;
        }
        std::tuple<long long, int, int> path(-1, -1, -1);
        for (int i = 0; i < m; ++i) {
            for (int j = i + 1; j < m; ++j) {
                if (color[nodes[i]] != color[nodes[j]]) {
                    path = std::max(path, std::make_tuple(distance(nodes[i], nodes[j]), nodes[i], nodes[j]));
                }
            }
        }
        if (std::get<0>(path) == -1) {
            ret.second = -1;
        } else {
            int u = std::get<1>(path), v = std::get<2>(path);
            ret.second = color[u] == color[ret.first.first] ? v : u;
        }
        return ret;
    }
};


ZKW_Tree<std::pair<int, int>, merge> F[M];
ZKW_Tree<std::pair<std::pair<int, int>, int>, merge2> G;

void updateU(int u, std::pair<int, int> p) {
    F[color[u]].update(pos[u], p);
    G.update(color[u], {F[color[u]].top(), -1});
}

int main() {
    for (int i = 2; i < M * 2; ++i) {
        Log2[i] = Log2[i >> 1] + 1;
    }
    int _;
    for (scanf("%d", &_); _ != 0; --_) {
        int q, n = 1;
        scanf("%d%d", &q, &color[1]);
        for (int i = 1; i <= q; ++i) {
            cnt[i] = 0;
            adj[i].clear();
        }
        ++cnt[color[1]];
        p[0] = cnt[color[1]];
        for (int i = 1; i <= q; ++i) {
            scanf("%d", &op[i]);
            if (op[i] == 0) {
                scanf("%d%d%d", &x[i], &c[i], &d[i]);
                ++n;
                adj[x[i]].emplace_back(n, d[i]);
                ++cnt[c[i]];
                p[i] = cnt[c[i]];
                y[i] = n;
            } else {
                scanf("%d%d", &x[i], &c[i]);
                ++cnt[c[i]];
                p[i] = cnt[c[i]];
            }
        }
        dfs_clock = 0;
        dfs(1, -1);
        ST_init();
        G.init(q, {{-1, -1}, -1});
        for (int i = 1; i <= q; ++i) {
            F[i].init(cnt[i], {-1, -1});
        }
        pos[1] = p[0];
        updateU(1, {1, 1});
        for (int i = 1; i <= q; ++i) {
            if (op[i] == 0) {
                int u = y[i];
                color[u] = c[i];
                pos[u] = p[i];
                updateU(u, {u, u});
            } else {
                int u = x[i];
                updateU(u, {-1, -1});
                color[u] = c[i];
                pos[u] = p[i];
                updateU(u, {u, u});
            }
            auto top = G.top();
            if (top.second == -1) {
                puts("0");
            } else {
                long long res = std::max(distance(top.first.first, top.second), distance(top.first.second, top.second));
                printf("%lld\n", res);
            }
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 60800kb

input:

2
1 1
0 1 1 1
5 1
0 1 1 1
0 1 2 1
0 3 3 1
1 4 1
1 3 1

output:

0
0
2
3
2
0

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 263ms
memory: 64964kb

input:

50000
10 1
0 1 8 46
0 1 4 61
0 3 4 54
0 4 7 33
0 3 4 94
1 3 1
0 4 1 30
0 6 1 96
1 7 7
0 2 2 39
10 3
1 1 5
0 1 8 91
1 1 1
1 2 5
1 1 3
0 2 7 50
0 1 9 90
1 2 2
0 4 9 5
0 3 10 88
10 8
0 1 2 6
0 2 1 59
0 1 7 69
1 2 2
1 1 10
1 4 9
0 4 6 35
1 3 1
1 1 9
0 4 4 30
10 5
0 1 10 28
0 1 7 26
1 3 6
1 1 7
0 3 10 43...

output:

46
107
161
194
201
201
201
297
297
336
0
91
91
91
91
141
231
231
236
324
6
65
134
134
134
134
169
169
169
169
28
54
54
54
69
97
103
97
122
122
0
100
100
116
157
157
157
157
157
157
20
119
119
130
130
130
170
217
217
217
0
71
113
160
254
271
325
325
325
344
0
85
0
85
102
161
161
161
161
229
52
129
22...

result:

ok 500000 lines

Test #3:

score: 0
Accepted
time: 352ms
memory: 64976kb

input:

25000
20 13
0 1 6 681707255
0 1 16 568801871
1 2 6
0 1 10 105982369
1 3 19
0 1 19 652217942
1 2 14
0 5 2 647572648
1 5 15
1 1 10
0 4 11 217814971
1 5 19
0 5 1 401386214
1 7 13
0 1 20 57222063
0 4 14 497981650
0 5 6 365165172
0 9 17 146559515
0 4 12 573686770
1 11 17
20 9
0 1 16 963644680
0 2 11 7621...

output:

681707255
1250509126
1250509126
1250509126
1250509126
1333925197
1333925197
1981497845
1981497845
1981497845
1981497845
1981497845
1981497845
1981497845
1981497845
1981497845
1981497845
1981497845
1981497845
1981497845
963644680
1725749081
2592136755
3057373619
3057373619
3057373619
3057373619
30573...

result:

ok 500000 lines

Test #4:

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

input:

5000
100 2
0 1 2 916
1 1 1
1 1 2
0 2 1 192
0 2 2 746
0 1 2 942
0 3 2 467
0 1 1 4
0 3 2 690
1 5 2
0 5 1 554
0 2 2 442
1 1 1
0 5 1 984
1 8 1
1 7 2
0 4 1 871
0 2 1 143
1 7 2
1 8 1
0 11 2 3
0 1 2 990
0 6 1 84
1 9 1
0 14 1 822
0 4 1 146
1 14 2
1 17 1
1 12 2
1 13 2
0 2 2 536
1 15 1
0 13 2 1000
1 6 1
1 18 ...

output:

0
916
0
1108
1108
2050
2050
2050
2050
2050
3294
3294
3294
3724
3588
3588
3588
3588
3588
3588
4462
4462
4462
4462
4462
4462
4462
4462
5284
5284
5284
5284
5284
5284
5284
4549
5338
5338
5338
5338
5338
5338
5338
5338
5338
5338
5338
5338
7679
7679
7679
7679
7679
7679
7679
7679
7679
7679
7679
7679
7679
76...

result:

ok 500000 lines

Test #5:

score: 0
Accepted
time: 469ms
memory: 71264kb

input:

5000
100 1
1 1 8
1 1 5
0 1 3 1
1 2 5
0 1 10 1
1 1 6
1 2 7
1 1 2
1 3 8
0 2 7 1
0 4 1 1
0 2 9 1
1 1 1
1 3 3
1 1 1
0 4 9 1
1 3 4
1 1 10
0 3 9 1
1 3 5
1 2 2
1 1 6
0 7 3 1
1 4 3
0 2 8 1
1 3 4
0 6 10 1
1 5 6
1 10 9
1 1 6
0 2 4 1
0 10 7 1
1 2 5
0 1 4 1
0 7 6 1
0 1 7 1
1 6 8
0 14 2 1
0 12 3 1
1 2 2
1 11 2
0...

output:

0
0
1
0
2
2
2
2
2
3
4
4
4
4
4
4
4
4
5
5
5
5
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
7
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
8
0
2
2
3
3
4
4
4
4
4
4
4
4
5
5
5
5
5
5
5
5
5
6
6
6
6
6
6
6
6
6
6
6
6
6
6
7
7
7
7
7
7
7
7
8
8
8
8
9
9
...

result:

ok 500000 lines

Test #6:

score: 0
Accepted
time: 424ms
memory: 77596kb

input:

500
1000 495
1 1 313
0 1 335 430486180
1 2 386
0 2 26 363858266
0 2 719 736642454
0 1 299 241725027
1 4 945
0 2 568 17405209
1 1 127
1 2 25
0 5 309 852015021
1 5 80
1 5 212
0 6 593 139668155
0 4 663 424776866
1 8 650
0 4 340 549468221
0 6 836 906695670
1 4 734
1 5 654
1 3 198
1 10 834
1 8 391
0 8 27...

output:

0
430486180
430486180
794344446
1167128634
1408853661
1408853661
1408853661
1408853661
1408853661
2260868682
2260868682
2260868682
2260868682
2685645548
2685645548
2810336903
2810336903
2810336903
2810336903
2810336903
2810336903
2810336903
2810336903
3781366757
3781366757
3781366757
3781366757
3781...

result:

ok 500000 lines

Test #7:

score: 0
Accepted
time: 502ms
memory: 86972kb

input:

50
10000 8
1 1 48
0 1 16 436673414
1 1 41
1 1 63
0 2 81 990987688
0 1 82 927130364
1 3 34
0 1 95 838088791
1 2 55
0 1 69 257942068
0 1 10 316402396
1 4 59
1 1 7
0 5 18 958655995
1 4 47
1 7 39
1 7 89
0 2 4 577324056
1 6 33
1 3 39
0 2 85 103826155
1 6 78
1 3 38
0 2 39 741296286
0 6 20 210624285
0 11 4...

output:

0
436673414
436673414
436673414
1427661102
2354791466
2354791466
2354791466
2354791466
2354791466
2354791466
2354791466
2354791466
3224405888
3224405888
3224405888
3224405888
3224405888
3224405888
3224405888
3224405888
3224405888
3224405888
3224405888
3224405888
3410936697
3410936697
3410936697
3410...

result:

ok 500000 lines

Test #8:

score: 0
Accepted
time: 559ms
memory: 118008kb

input:

5
100000 4
0 1 10 663863160
1 1 6
0 2 6 318884946
1 1 3
0 3 8 714200366
1 4 3
0 1 10 41030383
0 3 10 293156472
1 3 1
0 3 6 280189449
1 7 6
0 1 8 807153083
0 2 6 408368392
0 9 8 516932628
1 7 10
0 10 10 643338925
1 4 2
0 6 4 128185005
1 10 9
1 6 2
1 5 10
1 4 8
1 9 1
1 8 2
0 3 1 652261161
1 11 6
0 7 6...

output:

663863160
663863160
663863160
982748106
1696948472
1033085312
1737978855
1737978855
1737978855
1737978855
1737978855
2504101555
2504101555
2504101555
2504101555
3039656188
3039656188
3039656188
3039656188
3039656188
3039656188
3039656188
3039656188
3039656188
3039656188
3039656188
3039656188
3039656...

result:

ok 500000 lines

Test #9:

score: 0
Accepted
time: 783ms
memory: 191736kb

input:

1
500000 1
1 1 2
1 1 1
1 1 2
1 1 1
0 1 2 943448284
1 1 1
1 2 1
1 1 1
0 2 2 616891491
1 2 2
0 3 1 503252491
1 2 1
1 1 1
1 3 1
0 1 1 218781184
0 3 2 761656816
1 6 2
0 2 2 783749599
1 4 1
0 3 1 908021038
0 7 1 504064622
0 9 1 617004735
1 2 2
1 3 1
1 1 1
0 9 2 415701223
1 8 1
1 7 2
1 3 1
1 10 1
1 8 1
1 ...

output:

0
0
0
0
943448284
943448284
0
0
1560339775
1560339775
1560339775
1560339775
1560339775
0
0
2540777775
2540777775
2540777775
2540777775
2540777775
2666362528
3283367263
3283367263
3283367263
3283367263
3283367263
3283367263
3283367263
3283367263
3283367263
3283367263
3283367263
3283367263
3283367263
...

result:

ok 500000 lines

Test #10:

score: 0
Accepted
time: 1021ms
memory: 197656kb

input:

1
500000 274
1 1 535
0 1 845 1274
1 2 394
0 2 386 6183
0 2 525 3927
0 1 586 3682
1 3 852
0 5 251 2802
1 1 505
0 2 56 1950
1 6 333
1 5 795
0 4 327 5974
1 4 980
1 8 941
0 5 880 1005
0 1 503 6850
1 2 252
0 7 918 3445
0 2 205 6315
1 3 157
0 2 764 7535
0 1 282 8046
0 6 276 6690
1 7 796
1 13 662
1 14 448
...

output:

0
1274
1274
7457
10110
11139
11139
13941
13941
13941
13941
13941
17659
17659
17659
17659
18025
18025
18025
18025
18025
18025
19221
24349
24349
24349
24349
24482
24482
26515
26515
28094
28094
28208
28208
28208
28208
28208
28208
28208
28208
28208
28208
28208
33306
33306
33306
37567
37567
37567
37567
3...

result:

ok 500000 lines

Test #11:

score: 0
Accepted
time: 1360ms
memory: 195604kb

input:

1
500000 239339
1 1 146317
0 1 129548 523936872
0 1 4183 185688231
0 2 342282 902627418
0 4 139772 67526644
0 1 467303 553060652
1 2 115944
1 2 478660
1 5 54038
0 6 451081 121928887
1 4 281520
0 5 384089 131672868
1 5 347420
1 7 230578
0 5 235065 54719994
1 1 353014
0 4 290757 777778193
1 1 3248
0 9...

output:

0
523936872
709625103
1612252521
1679779165
2047151586
2047151586
2047151586
2047151586
2169080473
2169080473
2300753341
2300753341
2300753341
2300753341
2300753341
2879332022
2879332022
2879332022
3026309525
3026309525
3026309525
3396007558
3396007558
3396007558
3396007558
3396007558
3396007558
339...

result:

ok 500000 lines

Test #12:

score: 0
Accepted
time: 318ms
memory: 64928kb

input:

50000
10 2
0 1 4 83
0 2 6 8
1 1 6
0 3 3 7
1 1 3
0 4 4 79
0 5 5 33
0 6 3 48
0 7 4 70
0 8 6 92
10 4
0 1 4 55
0 1 1 43
0 1 10 37
0 3 10 49
1 2 10
0 4 5 29
0 4 8 40
1 7 10
1 7 2
1 7 10
10 1
1 1 2
1 1 1
1 1 2
0 1 2 64
0 2 2 51
1 3 1
0 2 2 98
1 4 1
0 4 2 45
0 4 1 18
10 4
0 1 3 26
0 2 4 85
0 2 2 5
0 3 4 57...

output:

83
91
83
98
91
177
210
210
328
420
0
98
98
147
98
158
169
158
169
158
0
0
0
0
0
115
149
162
194
194
26
85
90
147
264
264
243
264
298
277
58
127
178
217
225
217
286
347
364
392
79
79
154
170
170
214
340
261
335
340
22
91
91
91
91
91
111
180
251
251
23
72
124
124
168
168
168
168
148
148
0
102
102
102
...

result:

ok 500000 lines

Test #13:

score: 0
Accepted
time: 429ms
memory: 66936kb

input:

25000
20 7
0 1 8 559701009
0 1 5 676124630
0 2 8 10828465
0 2 4 105982369
0 3 7 193254939
0 3 6 685990139
1 5 6
0 1 8 533389662
1 7 8
1 7 6
0 2 3 685213771
1 9 6
0 9 6 970279681
1 10 7
0 7 7 20675720
0 2 2 780258754
0 5 4 990226586
0 11 3 538406497
0 10 2 803458412
1 15 3
20 15
0 1 5 578750662
0 2 3...

output:

559701009
1235825639
1246654104
1341808008
1535062947
2027798147
1932644243
1932644243
2027798147
1932644243
2607029549
2114294349
3084574030
3577309230
3577309230
3577309230
3577309230
4136391447
4939849859
4401443362
578750662
1525909296
1907478681
1907478681
2198891299
2624384257
2717666707
33755...

result:

ok 500000 lines

Test #14:

score: 0
Accepted
time: 511ms
memory: 71320kb

input:

5000
100 8
0 1 8 800310387
0 1 7 993516606
0 2 5 922636287
0 2 7 402792931
0 3 4 576900512
0 3 8 400125621
0 4 5 859546870
0 4 5 797041479
0 5 3 652871847
0 5 10 134889328
0 6 1 659851484
0 6 10 755953355
0 7 9 265736130
0 7 10 695033142
0 8 5 522558475
0 8 9 509373783
0 9 6 792747882
0 9 6 89209618...

output:

0
1793826993
2716463280
2716463280
3293363792
3293363792
4152910662
4152910662
4152910662
4152910662
4812762146
4908864017
4908864017
4908864017
5431422492
5431422492
5639106508
5639106508
5639106508
5639106508
5639106508
5639106508
5543004637
5543004637
6058807391
5851123375
5851123375
6198798471
6...

result:

ok 500000 lines

Test #15:

score: 0
Accepted
time: 499ms
memory: 77660kb

input:

500
1000 21
0 1 4 38470
0 1 22 6021
0 1 12 71417
0 4 1 32046
0 1 11 94671
0 6 12 13357
0 7 17 70512
0 4 15 90516
0 8 7 62298
0 7 9 95283
0 11 12 79580
0 9 1 1428
0 8 13 50232
0 14 17 62228
0 10 15 57113
0 11 15 17539
0 12 5 72946
0 13 7 44542
0 16 19 96056
0 15 10 3788
0 18 17 75234
0 18 23 54455
0 ...

output:

38470
44491
109887
141933
198134
211491
282003
340473
402771
402771
444824
446252
446252
454361
461312
461312
519198
563740
601910
601910
638974
638974
677149
718826
769112
769112
840028
840028
853365
907553
907553
907553
926863
974293
1012287
1051803
1051803
1126247
1126247
1140269
1154573
1175629
...

result:

ok 500000 lines

Test #16:

score: 0
Accepted
time: 535ms
memory: 85044kb

input:

100
5000 4
0 1 2 550393785
0 2 2 951830211
0 2 3 4252693
0 3 7 926622278
0 5 2 749217359
0 6 3 208978435
0 7 2 378345963
0 8 8 376967997
0 8 7 85826811
0 9 6 918480721
0 11 8 211271884
0 12 7 750914822
0 12 6 247849170
0 13 3 493177878
0 14 4 852096465
0 15 6 810191101
0 16 7 854274382
0 18 8 250897...

output:

550393785
1502223996
1502223996
2428846274
3178063633
3387042068
3765388031
4142356028
4142356028
5060836749
5272108633
6023023455
6023023455
6516201333
6516201333
7326392434
7326392434
7477225803
7793058688
8052920323
8675963212
8675963212
8709305124
9552802307
9552802307
10003488182
10815282199
10...

result:

ok 500000 lines

Test #17:

score: 0
Accepted
time: 672ms
memory: 108904kb

input:

10
50000 16
0 1 19 610998119
0 2 10 968784283
0 2 8 728478746
0 3 11 8123553
0 5 9 905467015
0 5 16 808139157
0 6 5 239568162
0 7 10 729045302
0 9 3 111298980
0 9 16 937084244
0 10 1 31606327
0 11 15 947495583
0 13 12 222831846
0 13 6 464440501
0 15 6 306675524
0 15 10 208389226
0 17 8 381723227
0 1...

output:

610998119
1579782402
1697263029
1705386582
2610853597
2610853597
2850421759
3242571041
3353870021
4179655285
4179655285
5127150868
5349982714
5591591369
5898266893
5898266893
6064223195
6707438312
6885224384
6971817109
7725764105
7725764105
8600320942
9092560942
9120634882
9509893119
9981661191
1009...

result:

ok 500000 lines

Test #18:

score: 0
Accepted
time: 988ms
memory: 134120kb

input:

5
100000 73905
0 1 54264 565631382
0 1 74169 626367322
0 1 1876 207833748
0 3 32340 425912405
0 3 86765 580893492
0 3 78509 620008111
0 1 78047 297088682
0 8 9352 657396693
0 2 46656 876561387
0 2 21981 758211326
0 1 66191 497473364
0 7 31373 710550905
0 4 49110 898163352
0 7 7842 195277625
0 15 490...

output:

565631382
1191998704
1191998704
1617911109
1772892196
1812006815
1812006815
2200860808
2688568202
2688568202
2688568202
3399119107
3399119107
3399119107
3611560882
3611560882
3611560882
3611560882
3611560882
3611560882
3611560882
4591901636
5270705092
5270705092
5270705092
5270705092
5270705092
5270...

result:

ok 500000 lines

Test #19:

score: 0
Accepted
time: 635ms
memory: 132268kb

input:

5
100000 4
0 1 3 1
0 1 1 1
0 1 3 1
0 1 3 1
0 1 2 1
0 1 2 1
0 1 4 1
0 1 1 1
0 1 1 1
0 1 2 1
0 1 1 1
0 1 6 1
0 1 5 1
0 1 3 1
0 1 5 1
0 1 6 1
0 1 4 1
0 1 1 1
0 1 5 1
0 1 4 1
0 1 2 1
0 1 1 1
0 1 4 1
0 1 3 1
0 1 2 1
0 1 5 1
0 1 3 1
0 1 2 1
0 1 5 1
0 1 4 1
0 1 5 1
0 1 1 1
0 1 1 1
0 1 6 1
0 1 4 1
0 1 2 1
0...

output:

1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
...

result:

ok 500000 lines

Test #20:

score: 0
Accepted
time: 705ms
memory: 159384kb

input:

2
250000 11
0 1 50 202202842
0 1 50 85307347
0 1 73 246079785
0 1 56 133905466
0 1 18 801455715
0 1 48 753262040
0 1 39 283860920
0 1 28 115702619
0 1 71 325507310
0 1 23 141694786
0 1 21 270230752
0 1 59 735296608
0 1 7 282754042
0 1 43 904468061
0 1 8 944064703
0 1 27 562969141
0 1 22 282348612
0 ...

output:

202202842
202202842
448282627
448282627
1047535500
1554717755
1554717755
1554717755
1554717755
1554717755
1554717755
1554717755
1554717755
1705923776
1848532764
1848532764
1848532764
1848532764
1848532764
1848532764
1848532764
1848532764
1848532764
1848532764
1908115585
1927038812
1927038812
1927038...

result:

ok 500000 lines

Test #21:

score: 0
Accepted
time: 1447ms
memory: 233316kb

input:

1
500000 69011
0 1 236904 20629548
0 2 260855 313010419
0 3 28053 297397611
0 3 19007 902627418
0 4 310068 163639772
0 6 189575 939558501
0 6 149152 267951762
0 7 269740 153256316
0 8 334821 561424258
0 9 254332 720792001
0 11 52044 121928887
0 12 126109 952781520
0 12 151204 105884089
0 13 105212 1...

output:

20629548
333639967
631037578
1236267385
1363664801
2303223302
2303223302
2456479618
2456479618
3177271619
3299200506
4251982026
4251982026
4408130719
4408130719
4408130719
4595171814
4826477300
5604255493
6179758741
6357595075
6705817722
7006106410
7333336195
7635848766
7635848766
8236267327
8493849...

result:

ok 500000 lines

Test #22:

score: 0
Accepted
time: 1078ms
memory: 228952kb

input:

1
500000 235
0 1 102 770825679
0 1 117 727361092
0 1 182 37810510
0 2 66 997641218
0 1 101 923302075
0 2 19 816708498
0 3 174 626718710
0 3 177 76708026
0 4 176 129764471
0 9 243 675986453
0 6 47 953130968
0 7 234 614032707
0 10 27 107421351
0 13 230 275840846
0 11 195 802920811
0 12 168 155478879
0...

output:

770825679
1498186771
1498186771
2495827989
2691768972
2691768972
3122546699
3122546699
3122546699
3248522468
3644899940
4077999927
4077999927
4353840773
4760384112
4760384112
4760384112
5419209746
5419209746
5419209746
6108267795
6108267795
6108267795
6713907751
7583397574
7955717994
7955717994
8866...

result:

ok 500000 lines

Test #23:

score: 0
Accepted
time: 721ms
memory: 224920kb

input:

1
500000 29
0 1 35 218434386
0 1 34 77275555
0 1 22 364690105
0 1 20 820509965
0 1 36 382227729
0 1 19 166859173
0 1 32 301286862
0 1 39 944780201
0 1 13 288477853
0 1 31 934709967
0 1 29 509071322
0 1 34 812840639
0 1 21 134532890
0 1 11 47092631
0 1 11 690264483
0 1 6 79097445
0 1 18 66173679
0 1 ...

output:

218434386
295709941
583124491
1185200070
1202737694
1202737694
1202737694
1765290166
1765290166
1879490168
1879490168
1879490168
1879490168
1879490168
1879490168
1879490168
1879490168
1879490168
1879490168
1879490168
1911979881
1911979881
1911979881
1911979881
1911979881
1911979881
1911979881
191197...

result:

ok 500000 lines

Test #24:

score: 0
Accepted
time: 893ms
memory: 208604kb

input:

1
500000 2
0 1 3 505547402
0 1 4 843136457
0 3 4 305506050
0 3 5 179838552
0 3 1 990973261
0 1 2 410248177
0 5 4 251725712
0 1 2 683250420
0 6 4 333183379
0 5 3 989392456
0 5 1 199710046
0 5 1 660800795
0 3 4 940016914
0 14 3 839189979
0 4 2 793298954
0 10 3 363885173
0 17 5 255544114
0 15 3 5907987...

output:

505547402
1348683859
1654189909
1654189909
2339657120
2339657120
2339657120
2517360138
2850543517
2850543517
2850543517
2850543517
2850543517
3305593770
3305593770
3305593770
3722792820
4313591617
4557689320
4557689320
4557689320
4557689320
4557689320
4858543126
4858543126
4858543126
5034839910
5034...

result:

ok 500000 lines

Test #25:

score: 0
Accepted
time: 693ms
memory: 218116kb

input:

1
500000 2
0 1 2 829699153
0 1 2 900413744
0 2 1 519631747
0 3 2 392362986
0 4 1 969741295
0 5 1 683698016
0 6 1 211987905
0 8 2 534118212
0 9 1 825828606
0 9 1 648886877
0 11 2 503252491
0 11 2 933186231
0 13 2 651907715
0 14 1 102213641
0 15 2 281991223
0 16 2 606676583
0 16 2 325885062
0 17 2 487...

output:

0
0
2249744644
2642107630
3611848925
3611848925
3823836830
5041653058
5183783648
5183783648
6193792426
6623726166
7275633881
7275633881
7659838745
8266515328
8266515328
8753724073
8769473406
8769473406
9149531948
9149531948
10111442189
10728446924
10728446924
11416764312
11416764312
11467215353
1148...

result:

ok 500000 lines