QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#768595#2766. Unique Cities_8_8_4 144ms36932kbC++203.8kb2024-11-21 12:44:002024-11-21 12:44:02

Judging History

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

  • [2024-11-21 12:44:02]
  • 评测
  • 测评结果:4
  • 用时:144ms
  • 内存:36932kb
  • [2024-11-21 12:44:00]
  • 提交

answer

#include <bits/stdc++.h>
    
using namespace std;
    
typedef long long ll;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

const int  N = 2e5 + 12, MOD = (int)1e9 + 7;

int n, m, a[N], vis[N], timer;
vector<int> g[N], gt[N];
bool is[N];
vector<vector<int>> dist;
int f(int d) {
    vector<int> D(n + 1, 0);
    timer++;
    vis[d] = timer;
    int lst = d;
    queue<int> q;
    q.push(d);
    while(!q.empty()) {
        int v = q.front();
        lst = v;
        q.pop();
        for(int to : g[v]) {
            if(vis[to] != timer) {
                D[to] = D[v] + 1;
                q.push(to);
                vis[to] = timer;
            }
        }
    }
    dist.push_back(D);
    return lst;
}
int dep[N], mxd[N], res[N], d, d1, o, cr;
int zap = 0;
bool ok[N];
void dfs(int v, int pr = -1) {
    mxd[v] = dep[v];
    for(int to:g[v]) if(to != pr) {
        gt[v].push_back(to);
        dep[to] = dep[v] + 1;
        dfs(to, v);
        if(ok[to]) ok[v] = 1;
        mxd[v] = max(mxd[v], mxd[to]);
    }
    for(int i = 0; i < (int)gt[v].size(); i++) {
        if(mxd[gt[v][i]] == mxd[v]) {
            swap(gt[v][0], gt[v][i]);
        }
    }
    if(v == d) ok[v] = 1;
}
int b = 18, it = 0, cur = 0, ver[N * 2];
int up[N * 2][19];
void go(int v) {
    if(dist[cr][v] > dist[o][v] || (dist[cr][v] == dist[o][v] && zap)) {
        int f = cur, val = -mxd[v] + dep[v] * 2;
        set<int> r;
        if(dep[ver[f]] >= val) {
            for(int i = b - 1; i >= 0; i--) {
                int nv = ver[up[f][i]];
                if(dep[nv] >= val) {
                    f = up[f][i];
                }
            }
            f = up[f][0];
        }
        while(f) {
            int x = ver[f];
            r.insert(a[x]);
            f = up[f][0];
        }
        res[v] += (int)r.size();
    }
    if(gt[v].empty()) return;
    int sz = (int)gt[v].size();
    vector<int> p(sz), s(sz);
    p[0] = mxd[gt[v][0]];
    s[sz - 1] = mxd[gt[v][sz - 1]];
    for(int i = 1; i < sz; i++) {
        p[i] = max(p[i - 1], mxd[gt[v][i]]);
    }
    for(int i = sz - 2; i >= 0; i--) {
        s[i] = max(s[i + 1], mxd[gt[v][i]]);
    }
    vector<int> del;
    int bf = cur;
    for(int i = 0; i < sz; i++) {
        int to = gt[v][i];
        int mx = dep[v];
        if(i) mx = max(mx, p[i - 1]);
        if(i < sz - 1) mx = max(mx, s[i + 1]);
        mx -= dep[v];
        if(i == 1) {
            cur = up[cur][0];
        }
        if(i <= 1 && dep[v] - dep[ver[cur]] <= mx) {
            for(int i = b - 1; i >= 0; i--) {
                int nv = up[cur][i];
                if(dep[v] - dep[ver[nv]] <= mx) {
                    cur = nv;
                }
            }
            cur = up[cur][0];
        }
        if(i <= 1) {
            it++;
            ver[it] = v;
            up[it][0] = cur;
            for(int i = 1; i < b; i++) {
                up[it][i] = up[up[it][i - 1]][i - 1];
            }
            cur = it;
        }
        go(to);
    }
    cur = bf;
}
void solve(int root) {
    it = cur = 0;
    for(int i = 1; i <= n; i++) {
        gt[i].clear();
    }
    ver[0] = 0;
    dep[0] = -(int)1e9;
    dep[root] = 1;
    dfs(root);

    go(root);

}
void test() {
    cin >> n >> m;
    for(int i = 1; i <= n - 1; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    d = f(1), d1 = f(d);
    f(d1);
    o = 2;cr = 1;
    solve(d);
    zap = 1;
    o = 1;cr = 2;
    solve(d1);
    for(int i = 1; i <= n; i++) {
        cout << res[i] << '\n';
    }

}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t = 1; 
    // cin >> t;

    while(t--) 
        test();
}

详细

Subtask #1:

score: 4
Accepted

Test #1:

score: 4
Accepted
time: 0ms
memory: 9744kb

input:

2 1
1 2
1 1

output:

1
1

result:

ok 2 lines

Test #2:

score: 4
Accepted
time: 3ms
memory: 8156kb

input:

1842 848
740 1093
1299 922
801 1560
265 1664
1043 65
1430 427
80 233
4 1238
1623 1473
1569 274
953 1485
1259 649
1671 1409
246 542
742 1517
720 1120
1527 1328
1167 1531
1056 1130
673 1222
192 980
1393 913
446 688
135 23
1065 1787
978 1481
1765 1720
310 202
1406 1451
475 523
104 774
1531 829
169 396
...

output:

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

result:

ok 1842 lines

Test #3:

score: 4
Accepted
time: 28ms
memory: 8324kb

input:

951 908
777 679
676 510
34 425
424 889
78 4
924 408
151 905
399 942
606 776
275 332
616 900
513 372
911 130
465 430
126 303
380 603
506 505
592 38
917 270
435 815
591 773
63 66
417 642
301 693
814 454
735 671
899 674
925 846
254 898
845 569
703 659
707 241
482 715
646 282
274 925
861 374
699 488
903...

output:

302
286
491
486
478
563
380
32
497
563
115
30
581
428
510
491
374
565
242
418
469
550
410
229
499
386
274
588
487
276
279
536
445
203
483
56
565
361
129
140
533
437
229
68
442
559
570
502
493
212
588
96
330
405
262
526
464
516
258
541
554
264
475
441
544
476
406
222
468
397
531
591
406
39
320
198
60...

result:

ok 951 lines

Test #4:

score: 4
Accepted
time: 8ms
memory: 8356kb

input:

1817 365
1516 74
1811 1805
1036 311
1769 1663
1029 473
1596 310
337 888
324 426
385 315
1780 1684
1750 794
895 1029
1108 1194
566 613
489 1230
1727 1520
170 302
731 1171
591 295
1165 1233
304 835
1607 404
1035 1675
158 735
1553 1764
1390 1078
197 980
1176 1626
16 1218
1530 1146
1692 1477
1513 574
32...

output:

0
129
0
0
0
224
1
203
0
0
0
0
229
0
239
0
1
0
0
0
162
0
0
0
0
113
1
0
0
0
0
200
208
0
218
0
2
0
72
0
161
0
0
195
0
0
0
33
0
132
0
0
1
0
146
118
147
0
66
0
0
70
240
177
0
165
0
4
0
1
3
68
243
2
1
2
0
1
0
0
230
208
0
250
12
3
195
0
69
0
0
6
1
114
218
0
0
199
0
0
0
0
1
27
157
1
0
0
0
1
0
163
1
0
1
168
...

result:

ok 1817 lines

Test #5:

score: 4
Accepted
time: 3ms
memory: 10184kb

input:

2000 5
1695 1417
623 8
1319 1473
329 1662
879 1654
108 555
1765 1258
1808 962
893 532
779 664
1507 1612
435 250
1645 349
666 992
921 24
1343 1974
50 1258
1497 2000
1156 1076
227 1921
413 543
737 294
218 554
1735 32
234 1718
1615 1126
1620 1915
1722 10
1031 871
871 674
626 1527
1792 148
101 1837
893 ...

output:

0
1
1
0
2
2
1
0
0
0
0
0
0
3
1
1
2
1
0
3
1
0
0
0
0
0
0
1
1
1
2
1
1
0
0
3
2
4
0
1
1
0
1
0
1
3
0
2
1
0
0
1
1
0
0
0
2
1
1
0
0
2
0
1
1
1
1
0
1
3
0
1
0
1
2
1
3
1
0
1
1
2
1
0
0
1
1
0
1
1
0
0
2
0
2
1
0
2
0
2
0
0
0
1
0
2
0
0
3
1
3
2
1
0
1
1
0
0
1
1
2
0
0
0
0
1
1
0
3
2
0
1
0
0
2
0
2
1
1
0
3
0
0
1
2
0
2
0
0
0
...

result:

ok 2000 lines

Test #6:

score: 4
Accepted
time: 27ms
memory: 8736kb

input:

2000 6
462 1865
693 166
1818 5
444 1736
1133 1670
1356 760
1732 919
1485 126
1655 1476
957 1185
343 1431
968 52
1595 764
1541 1498
1729 1457
558 387
1458 480
1199 1759
979 1230
512 700
278 1058
1894 496
191 1524
411 103
415 1628
1524 715
41 1128
291 1115
1599 1613
114 1990
564 1439
805 1273
504 1051...

output:

6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
6
...

result:

ok 2000 lines

Test #7:

score: 4
Accepted
time: 4ms
memory: 10428kb

input:

2000 3
246 351
568 1925
1167 1934
847 1223
1203 894
1076 1886
1394 1834
704 1777
1483 1744
40 963
619 509
1738 212
1498 77
286 144
1946 456
626 647
1653 658
1216 1418
29 1320
754 1874
667 1585
457 589
1784 1793
1788 1104
488 818
1452 224
1259 1855
653 1931
1253 1413
1352 227
1919 122
1675 563
767 19...

output:

0
3
3
0
0
0
1
3
0
0
3
3
3
1
2
3
3
0
3
0
0
3
0
3
0
0
1
3
1
3
0
0
0
3
0
3
3
3
3
0
3
0
3
1
3
0
0
3
3
0
0
0
1
0
0
3
3
3
3
2
0
3
0
3
0
3
3
0
3
0
0
2
3
3
3
0
3
0
0
3
0
3
0
1
3
3
3
3
0
3
3
2
0
3
0
3
3
0
0
0
3
0
3
0
3
0
3
0
1
0
0
0
0
1
1
3
3
0
0
3
3
0
0
0
0
3
3
0
0
0
0
1
0
3
2
3
3
0
3
3
3
3
3
3
3
3
3
3
3
0
...

result:

ok 2000 lines

Test #8:

score: 4
Accepted
time: 3ms
memory: 8368kb

input:

2000 8
1053 321
1821 1638
946 445
1686 570
532 192
1306 987
70 1362
1564 1337
737 1540
1686 1533
1776 1864
1212 1891
476 1618
1031 815
883 939
1487 177
1659 1116
794 434
473 1040
1867 1424
108 1095
1788 1612
791 937
958 1545
161 1053
1594 1053
668 1900
918 1270
889 1119
635 1747
1126 271
420 551
572...

output:

1
1
2
2
2
1
4
1
3
2
2
3
1
1
1
1
1
3
2
3
1
2
1
1
3
2
1
1
1
2
2
3
1
1
2
2
2
3
3
2
2
3
3
1
1
1
1
2
1
3
1
4
1
1
1
2
2
4
2
2
3
4
3
1
1
2
3
1
2
1
1
2
1
2
1
3
2
3
3
2
1
4
1
2
2
1
3
1
1
2
1
3
2
3
2
2
2
1
3
2
1
2
2
3
4
1
4
2
2
2
1
2
2
3
1
3
1
2
1
3
1
2
2
2
3
1
1
1
1
2
3
1
2
1
3
2
1
2
1
1
2
1
1
1
2
3
2
3
2
1
...

result:

ok 2000 lines

Test #9:

score: 4
Accepted
time: 3ms
memory: 8080kb

input:

2000 10
1446 902
516 588
381 360
1588 766
1162 1333
951 1610
567 1665
329 998
527 1126
1770 1847
156 925
305 384
704 831
171 888
387 1139
450 904
341 1897
999 617
1643 1611
894 1636
57 1221
993 459
660 826
1336 1639
37 652
734 871
950 1234
254 1561
151 675
1585 230
1255 595
92 321
1711 913
1986 231
...

output:

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

result:

ok 2000 lines

Test #10:

score: 4
Accepted
time: 0ms
memory: 8304kb

input:

2000 4
1037 1229
171 1052
87 746
329 1413
1527 1925
1120 349
208 1815
535 1430
432 102
634 707
490 334
1139 933
1492 1110
907 1412
1480 197
324 926
1324 565
1638 937
1122 1238
823 87
387 1159
1261 1829
715 295
1822 1058
1595 1596
1525 1416
288 364
110 1536
137 68
807 85
376 165
33 76
1495 1172
780 6...

output:

0
3
3
0
3
1
4
4
4
4
4
4
3
0
4
0
4
4
1
3
4
3
3
1
1
3
4
3
3
3
0
0
0
4
4
3
2
2
3
0
0
4
4
4
3
2
0
4
2
1
4
3
3
3
1
4
4
0
0
4
3
0
1
4
3
4
4
4
0
3
1
4
3
1
0
0
0
1
0
4
0
3
4
4
2
3
2
3
3
3
4
3
3
0
0
0
1
4
4
2
3
2
4
4
0
3
4
3
3
0
2
3
0
1
0
4
4
1
4
4
1
3
0
3
4
0
1
1
0
3
2
1
3
3
3
4
4
4
3
1
0
2
4
3
1
2
2
3
0
1
...

result:

ok 2000 lines

Test #11:

score: 4
Accepted
time: 3ms
memory: 8132kb

input:

2000 9
203 1192
632 234
1825 844
1559 1036
596 742
33 1345
1254 1183
1592 1737
886 760
836 50
793 1955
115 1040
1067 1943
153 456
1819 1456
1563 1366
664 209
583 1574
1120 1737
1385 1354
852 284
945 1668
225 1963
341 928
1351 979
479 1652
1353 1333
1907 1933
1487 21
1737 600
1913 395
1336 888
338 32...

output:

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

result:

ok 2000 lines

Test #12:

score: 4
Accepted
time: 0ms
memory: 7924kb

input:

2000 6
1425 1140
781 2
1556 1112
1953 1556
475 1556
1019 251
1556 543
1556 1186
1556 983
273 1556
1556 1783
1963 1340
1645 1556
1556 300
1556 1551
1556 346
525 1556
1556 1289
1556 1257
1556 1729
1556 974
1556 888
1556 1738
1556 716
315 1556
1220 1556
1556 353
1001 1469
1556 1335
1556 374
1556 1263
1...

output:

1
2
1
1
1
0
1
1
1
1
1
1
1
1
1
1
0
1
1
1
0
1
1
1
2
0
1
1
1
1
2
2
1
0
1
3
1
0
0
1
1
1
1
1
1
1
2
1
1
0
1
1
1
1
1
1
1
1
1
3
1
1
1
1
1
2
2
1
1
1
1
0
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
2
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
0
1
2
0
...

result:

ok 2000 lines

Test #13:

score: 4
Accepted
time: 19ms
memory: 10728kb

input:

2000 5
1443 1122
821 1362
1348 1904
268 600
502 241
1837 820
622 355
413 684
527 314
1648 822
1702 1812
299 194
1553 1040
265 863
892 1789
1979 329
341 11
169 207
845 960
1592 323
1408 350
982 1569
1704 1561
1091 1759
471 1354
1782 1730
1992 779
1171 782
752 955
80 1938
770 304
333 1094
1329 1655
98...

output:

5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
1
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
4
5
5
5
4
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
5
...

result:

ok 2000 lines

Test #14:

score: 4
Accepted
time: 4ms
memory: 10556kb

input:

2000 1
1230 354
1787 355
1656 683
905 469
1820 1452
101 369
1502 355
754 356
659 355
511 1462
705 471
251 442
1711 355
1973 925
650 822
1934 1160
1358 995
1571 818
355 115
1076 1336
487 355
108 570
774 731
355 1849
1349 1398
490 1168
31 1670
1874 29
699 347
355 1684
272 355
22 1960
643 874
355 1722
...

output:

1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
0
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
0
1
1
...

result:

ok 2000 lines

Test #15:

score: 4
Accepted
time: 4ms
memory: 8524kb

input:

2000 10
1292 134
1644 1008
144 364
765 709
1671 1188
1624 1836
390 900
913 1540
719 1691
295 259
534 1316
1360 296
1228 387
386 1355
1546 1095
1412 1102
141 1266
1267 1198
1292 1300
1030 424
1146 1848
596 886
542 1522
951 1952
671 628
763 1220
1222 231
1944 963
1431 1769
792 959
1046 1292
1292 383
1...

output:

1
10
0
1
10
10
1
10
1
0
0
10
10
0
0
10
2
10
0
6
1
1
1
10
10
0
0
1
10
0
0
0
10
10
0
8
1
1
0
10
10
1
10
10
1
0
1
10
10
1
0
0
0
1
0
10
10
10
1
0
9
0
10
9
10
0
0
0
10
10
1
10
10
1
0
1
10
1
0
10
1
10
0
10
10
0
2
2
0
1
1
10
9
10
10
10
10
10
1
10
10
10
0
10
10
10
1
1
0
1
0
0
10
0
1
10
0
10
1
10
10
1
10
0
0...

result:

ok 2000 lines

Test #16:

score: 4
Accepted
time: 2ms
memory: 7912kb

input:

2000 5
305 900
106 900
577 900
900 1017
900 631
134 900
900 1402
900 1652
1324 900
164 900
559 900
900 1342
900 1734
1125 900
824 900
1246 900
291 900
1190 900
900 1782
900 1195
900 1043
584 900
900 1741
900 1965
1188 900
900 1844
900 1264
900 581
900 845
900 1931
900 1326
1877 900
900 1255
900 894
...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 2000 lines

Test #17:

score: 4
Accepted
time: 11ms
memory: 10460kb

input:

2000 4
1890 1838
1103 1956
1614 260
1512 571
85 1838
1838 148
233 169
1838 58
1838 1946
1838 445
1494 1263
1407 301
100 1912
1418 1975
1627 270
1128 1838
674 1867
1752 1668
1268 1651
82 215
999 146
1838 548
250 1543
1274 1838
305 1838
1838 844
1751 1498
1906 10
543 163
122 1838
660 1655
1838 1576
85...

output:

4
4
1
1
1
4
1
4
4
4
4
4
4
4
1
4
1
1
4
4
4
1
4
4
1
4
4
4
4
4
1
4
1
4
4
4
4
4
4
4
1
4
4
4
4
1
4
4
4
4
1
1
4
1
1
1
4
1
4
4
4
4
4
1
4
1
4
4
1
1
4
4
4
4
4
4
1
4
4
1
4
4
4
1
1
1
4
4
1
4
4
4
4
4
4
4
1
1
4
4
4
1
1
4
4
1
4
1
1
4
4
1
4
4
1
1
1
1
4
4
4
1
4
1
4
4
1
4
1
4
4
1
4
4
1
4
4
4
1
1
4
4
4
4
4
4
4
1
4
4
...

result:

ok 2000 lines

Test #18:

score: 4
Accepted
time: 0ms
memory: 10424kb

input:

2000 2
1032 289
236 1488
1762 128
1926 580
349 922
835 1988
835 1377
1112 1586
341 998
635 387
1835 1723
1029 835
1303 1914
314 1481
1662 430
1314 1152
1212 926
554 523
361 949
1924 1477
154 1799
835 1283
324 23
835 1482
321 540
1320 1820
835 1485
835 200
1927 504
1725 1006
220 656
835 1007
364 1717...

output:

1
1
1
0
1
1
2
0
1
2
1
2
0
2
2
1
1
2
2
0
2
2
0
2
1
1
0
0
1
1
0
0
0
1
2
1
2
2
2
2
1
2
2
1
1
2
2
0
0
2
2
2
0
0
0
0
0
0
0
0
1
1
0
0
0
2
2
0
0
0
0
0
2
0
1
0
0
0
0
1
1
0
2
0
1
2
1
2
0
0
1
2
0
0
2
2
0
0
0
1
0
0
0
0
1
1
1
0
0
0
2
2
1
0
2
1
1
2
0
0
0
2
0
0
1
2
2
0
2
2
1
0
0
0
2
0
2
0
0
1
1
0
2
2
0
1
1
1
1
0
...

result:

ok 2000 lines

Test #19:

score: 4
Accepted
time: 0ms
memory: 8088kb

input:

2000 619
1215 1416
174 1576
1559 1503
514 651
1484 1539
1217 1121
752 485
512 719
1010 1903
926 1242
882 309
844 485
1295 460
115 1504
38 1130
1000 1990
1389 57
219 1493
579 1797
281 1347
224 782
1295 1793
224 278
221 1227
300 1686
990 1266
1643 181
1028 1861
803 1762
304 1976
1554 1042
420 1742
180...

output:

1
3
3
0
0
0
1
3
1
3
0
1
2
3
1
1
0
2
0
1
0
1
2
0
1
1
1
1
1
0
0
1
0
1
0
1
0
0
0
1
1
1
1
2
1
1
0
0
1
0
2
1
0
0
3
3
0
0
0
2
1
0
7
1
0
0
2
3
3
2
0
0
0
0
1
2
0
0
0
0
1
3
0
0
1
0
0
1
1
3
0
2
0
1
0
2
0
0
2
1
0
2
0
0
5
1
2
4
2
0
0
0
1
1
0
1
1
2
1
1
1
0
1
1
4
0
0
2
0
2
0
0
2
2
0
0
1
4
1
4
0
0
0
0
2
0
0
0
0
1
...

result:

ok 2000 lines

Test #20:

score: 4
Accepted
time: 144ms
memory: 10888kb

input:

2000 1589
398 1883
874 1438
1025 159
759 1551
206 670
1074 1873
1807 1108
242 1785
719 1380
15 1405
24 804
668 1287
362 181
1471 1396
1813 101
987 1259
1499 471
1283 221
1641 416
1128 1595
497 1235
170 1034
1579 1430
1522 1085
58 419
1517 1503
638 808
760 946
1773 1014
1441 305
1390 1925
1077 37
142...

output:

719
690
800
868
923
972
712
690
587
433
236
105
331
467
293
866
845
284
908
842
437
668
555
38
416
937
554
93
1002
432
935
859
298
650
998
812
871
562
990
895
1029
598
758
117
1075
781
895
962
1124
731
334
709
106
1081
242
634
1032
936
574
1018
708
163
1092
508
913
1076
1065
797
1005
1048
1040
995
4...

result:

ok 2000 lines

Test #21:

score: 4
Accepted
time: 16ms
memory: 10432kb

input:

2000 1174
1718 1701
1663 1817
990 1332
1638 1283
870 674
249 719
430 853
490 287
621 378
501 1386
66 1713
1794 1811
1455 873
998 910
1998 1968
1103 619
726 42
1184 577
1348 204
1567 1700
632 990
1661 1845
1967 1125
728 1178
1361 560
681 1867
130 139
1983 1380
1484 620
1159 1412
989 1065
109 1354
101...

output:

1
0
0
6
139
0
1
252
0
321
8
144
0
0
0
1
1
2
0
0
67
368
374
274
3
115
0
343
1
0
0
0
30
0
315
0
134
205
213
170
0
250
168
0
398
0
327
0
0
0
0
0
0
1
263
0
378
247
1
0
1
0
3
0
288
236
0
0
0
0
310
0
328
79
0
0
0
391
0
0
0
301
228
0
269
0
391
0
0
0
332
4
0
206
0
317
111
313
71
236
0
0
281
0
193
1
1
0
0
19...

result:

ok 2000 lines

Test #22:

score: 4
Accepted
time: 3ms
memory: 10172kb

input:

2000 144
1589 315
1662 685
726 522
959 721
1420 1935
1591 1075
687 1198
538 3
1410 428
363 1984
621 928
1494 773
325 1051
500 538
669 853
1960 839
318 160
1358 62
637 1323
88 624
1850 538
538 1616
1719 1701
700 1350
117 538
2000 1821
314 1828
1832 1889
1120 1243
1631 1849
939 1758
531 95
390 538
825...

output:

1
1
1
2
1
1
2
3
1
1
2
2
2
2
2
1
2
3
4
1
1
3
2
2
1
3
1
2
2
1
2
4
1
1
5
6
4
2
3
4
2
1
3
1
1
2
4
2
2
2
1
2
1
4
1
1
2
1
3
2
1
1
1
2
1
1
2
1
3
1
2
3
1
1
2
4
1
3
5
1
1
2
1
2
1
1
1
1
2
3
2
1
1
1
1
4
3
1
3
2
1
3
2
2
2
2
5
2
3
1
2
1
5
2
1
1
1
1
1
1
2
6
4
1
2
3
1
2
1
1
2
2
1
3
1
1
1
1
2
4
2
1
1
2
3
1
4
2
1
1
...

result:

ok 2000 lines

Test #23:

score: 4
Accepted
time: 3ms
memory: 8112kb

input:

2000 1539
610 682
1567 1833
455 673
1603 1799
211 138
1579 1009
1441 335
1472 116
1802 1558
256 1034
1176 930
1607 1868
937 1762
1866 1088
54 352
201 411
244 1374
253 324
1048 1554
1197 1284
935 1300
374 1562
1609 1792
1576 1240
805 1997
925 1563
1951 1035
1431 401
1846 1342
809 1304
335 1113
1229 1...

output:

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

result:

ok 2000 lines

Test #24:

score: 4
Accepted
time: 3ms
memory: 10164kb

input:

2000 519
1773 1636
553 814
1028 682
317 1210
1168 13
206 173
1498 1425
1663 1246
1165 1262
1618 134
375 9
1998 546
1415 670
432 1363
1435 1017
400 1395
323 1614
169 906
284 146
776 549
24 58
1001 1552
563 1898
1638 1180
1135 1449
15 1074
1649 1557
1094 1643
527 241
24 1545
1274 1200
1968 1187
1004 9...

output:

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

result:

ok 2000 lines

Test #25:

score: 4
Accepted
time: 3ms
memory: 10212kb

input:

2000 568
1955 1424
1169 799
1607 1094
1731 805
1542 349
38 1167
1030 463
1127 222
1326 328
1183 1140
1914 1363
1945 347
1333 1127
1007 625
1086 869
62 153
140 541
1533 693
1442 996
984 1362
1245 785
1771 140
749 940
1535 1351
1279 1372
1148 1570
162 1579
386 416
1324 1641
252 93
45 1823
1467 1190
17...

output:

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

result:

ok 2000 lines

Test #26:

score: 4
Accepted
time: 0ms
memory: 8256kb

input:

2000 460
1762 905
1762 593
903 1762
1038 1762
1026 1762
1762 1974
16 851
1762 214
751 1271
1762 1868
1762 1011
1762 815
1762 1983
1345 1762
1762 1955
1134 625
1762 1723
1762 740
419 1956
1762 434
1148 1762
1762 233
39 1762
1189 1762
444 1762
541 1762
1762 1918
1762 1010
1762 601
1591 1762
1913 1762
...

output:

1
1
1
1
1
2
1
1
0
1
1
1
1
1
1
2
1
2
0
1
1
1
1
1
2
1
1
2
1
1
1
1
1
1
0
1
1
1
0
0
2
1
1
1
0
1
1
1
0
1
1
0
2
1
1
1
1
1
1
1
1
1
1
1
2
1
2
1
1
2
1
2
1
1
1
2
1
0
1
1
1
0
1
0
2
1
1
1
1
2
1
1
1
1
1
1
0
1
1
0
1
1
1
1
1
1
1
0
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
2
1
0
1
1
1
1
1
1
1
1
2
1
1
1
2
1
1
1
1
1
2
1
2
1
2
0
...

result:

ok 2000 lines

Test #27:

score: 4
Accepted
time: 79ms
memory: 8564kb

input:

2000 1870
195 1081
1072 11
706 155
1893 180
236 761
1895 1174
1239 1526
547 1716
1944 304
153 719
620 1064
739 1506
491 1514
679 1415
384 1562
1826 36
550 446
560 505
1993 359
984 1761
327 4
96 461
513 809
732 991
240 53
7 1707
54 330
1768 602
1493 134
496 1577
610 187
1545 1976
1223 221
1447 1916
5...

output:

535
753
351
85
815
175
371
516
101
464
154
13
528
277
244
321
283
768
449
313
219
666
797
388
1
93
776
155
302
771
272
683
634
93
645
484
365
728
368
779
759
634
86
599
582
713
700
47
534
296
9
25
96
10
368
345
133
315
372
661
397
783
636
696
736
660
231
62
462
162
174
102
356
797
302
771
728
760
14...

result:

ok 2000 lines

Test #28:

score: 4
Accepted
time: 52ms
memory: 10560kb

input:

2000 1091
1068 386
1827 1070
1001 1488
808 125
397 1056
1838 1342
1163 1626
839 567
672 1780
740 1626
1718 897
288 101
1086 1049
1626 1839
1946 372
1530 227
1030 1592
1626 1780
1828 1709
1626 55
1351 390
260 666
1626 1046
621 680
1047 1626
176 1626
1538 1287
1004 272
1187 292
273 1440
1857 310
1973 ...

output:

428
194
2
449
574
314
187
451
441
374
703
2
379
75
400
89
179
3
601
1
2
383
141
288
289
192
2
455
252
162
59
507
402
2
696
2
81
97
2
153
95
2
2
93
277
2
2
673
427
115
700
84
517
2
1
2
589
153
2
583
2
373
19
2
187
505
629
262
384
15
561
125
351
659
2
579
2
459
694
378
673
29
2
2
577
28
2
334
403
241
...

result:

ok 2000 lines

Test #29:

score: 4
Accepted
time: 11ms
memory: 8624kb

input:

2000 1959
377 463
1434 1542
343 638
567 1086
1626 1251
342 28
841 785
835 557
638 1562
541 1291
1224 638
1973 422
1602 341
1643 1875
638 1034
15 1128
1729 638
654 1491
384 1876
370 638
70 1986
1315 1467
675 1894
1980 1549
1822 1451
1230 638
1138 496
900 217
1947 470
1548 727
211 1141
136 1193
1322 1...

output:

2
45
0
1
278
0
0
222
0
78
2
0
0
0
1
0
0
235
0
0
0
281
1
116
0
0
2
234
1
228
3
0
0
0
0
0
1
0
232
225
194
0
263
1
1
0
0
68
209
0
1
0
1
0
206
1
98
112
236
57
0
44
0
28
2
62
0
123
0
0
202
0
91
0
43
1
0
211
165
0
195
0
4
1
0
0
1
0
11
0
1
163
1
148
2
136
1
250
302
126
1
2
168
98
1
143
200
65
45
1
0
0
1
1
...

result:

ok 2000 lines

Test #30:

score: 4
Accepted
time: 2ms
memory: 10228kb

input:

2000 492
235 1628
1289 1628
468 1628
1628 667
1628 220
1628 973
1628 92
172 1628
1628 882
1628 1260
1628 316
1628 1055
1628 1213
1987 1628
1628 136
1156 1628
1628 282
1276 1628
974 1628
1628 1772
1628 1214
229 1628
419 1628
1628 1031
873 1628
636 1628
165 1628
182 1628
567 1628
1403 1628
779 1628
75...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 2000 lines

Test #31:

score: 4
Accepted
time: 44ms
memory: 8484kb

input:

2000 266
115 1606
1219 795
108 49
1606 965
1114 166
1005 1645
744 1671
538 1354
1606 267
230 1465
1872 599
1784 172
431 1601
1737 474
1606 334
821 1172
86 520
1606 1235
1709 1044
72 838
1015 914
649 1606
627 1030
1051 1606
1528 1340
1606 1185
1695 1774
1187 1606
13 691
1329 1366
1606 500
1606 567
16...

output:

1
263
258
1
259
120
158
1
1
1
1
245
79
264
263
1
1
26
1
244
162
239
1
231
202
1
133
1
1
254
1
1
1
212
218
1
1
245
231
257
81
245
1
261
1
254
4
258
248
1
1
165
1
249
135
185
262
258
188
231
202
255
1
1
1
1
263
10
1
188
130
264
1
123
1
1
1
240
1
1
195
261
256
1
1
123
254
1
1
1
182
1
1
258
1
249
1
1
1
...

result:

ok 2000 lines

Test #32:

score: 4
Accepted
time: 16ms
memory: 10684kb

input:

2000 1192
34 1120
699 1974
440 726
1440 1321
1709 1305
946 1419
1927 915
1023 1686
226 1037
1607 646
448 613
189 1233
448 496
657 1092
1911 777
1977 1012
714 1637
1604 306
820 1992
1047 1803
1805 448
344 1164
448 535
448 1558
629 185
7 448
52 859
1161 1575
1556 1787
902 524
643 1673
94 438
1236 459
...

output:

1
142
0
397
0
224
1
0
250
0
264
0
0
0
0
1
301
0
94
3
0
181
307
0
1
1
40
1
0
0
120
1
248
26
66
0
98
116
1
0
1
212
1
0
0
0
1
0
381
1
284
134
1
372
0
0
1
251
0
276
157
1
349
118
1
1
0
1
0
319
1
0
0
172
1
0
0
383
1
1
0
358
126
305
1
105
0
1
0
0
0
0
9
188
282
292
125
48
1
0
324
1
0
1
0
362
1
314
0
370
33...

result:

ok 2000 lines

Subtask #2:

score: 0
Time Limit Exceeded

Test #33:

score: 32
Accepted
time: 86ms
memory: 29796kb

input:

115391 1
32067 50006
1710 5850
21016 66364
72998 34367
24783 10670
49950 93666
81835 81234
53480 68963
87357 43320
93905 30509
72528 92224
520 100511
54804 2917
58490 23858
93643 87530
90737 65205
60740 110812
9553 90266
70028 67222
108045 88982
35584 110286
53518 21733
108735 26404
108228 109796
92...

output:

1
1
0
1
0
1
1
0
1
1
1
1
1
1
1
1
1
0
1
0
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
0
0
1
1
0
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
0
1
1
0
1
1
0
1
1
1
1
1
1
1
1
0
1
0
1
1
0
1
1
1
1
1
0
1
1
1
1
0
1
1
0
1
0
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 115391 lines

Test #34:

score: 0
Time Limit Exceeded

input:

114976 1
74053 36053
62978 89255
32367 21913
113882 44280
60815 35782
107811 95272
109039 78845
22484 41688
1781 111596
111506 59375
19869 45586
84990 81214
38638 90205
14928 14370
10758 5465
87745 5949
66720 6357
76134 26466
91805 105936
31792 68220
74216 108462
60158 67410
69489 50297
74956 63776
...

output:


result:


Subtask #3:

score: 0
Time Limit Exceeded

Test #50:

score: 32
Accepted
time: 140ms
memory: 36932kb

input:

157976 157976
20171 157173
44732 54119
107845 121149
109200 110309
82678 108206
89140 64200
36916 128759
3966 123760
92978 105716
43700 146126
14924 3429
107721 36095
94906 78173
97162 29552
119574 39605
25145 138492
16622 99431
60281 7949
76176 136644
75716 91518
127987 110605
77999 110960
101187 5...

output:

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

result:

ok 157976 lines

Test #51:

score: 0
Time Limit Exceeded

input:

191205 191205
92326 99995
188142 63029
71973 50899
68069 72878
159479 103397
143191 93854
27574 146981
63427 186167
104978 177629
22355 188155
184293 15184
101908 143833
111535 144687
112209 61685
59401 128258
97717 87496
168079 130237
49319 190230
119632 98368
76193 184597
22790 136093
1080 80811
1...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%