QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#47334 | #2232. Win Diesel | tovarisch | AC ✓ | 243ms | 57308kb | C++ | 3.3kb | 2022-09-08 07:28:39 | 2022-09-08 07:28:39 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <array>
using namespace std;
/* *
*
* Too many mind, no mind.
*
*
* */
using pii = pair <int, int>;
const int maxn = 2e5 + 10, LOG2N= 30;
int vis[maxn], dist[maxn], up[maxn][LOG2N];
vector <int> graph[maxn], g[maxn];
void dfs(int u, int p, int h){
dist[u] = h;
/*
* Binary lifting, we will jump in powers of 2.
* up[u][i] means we jump up from vertex u, 2^i positions.
* here we are calculating jumps for each power of 2.
*/
up[u][0] = p;
//cout << "here: " << u << ' ' << up[u][0] << endl;
//cout << "LCA : " << u << ' ' << dist[u] << endl;
//cout << "Par 0: " << u << ' ' << p << endl;
for(int i = 1; i < LOG2N; i++) {
up[u][i] = up[up[u][i - 1]][i - 1];
//cout << "Par " << i << ": " << u << ' ' << up[u][i] << endl;
}
//cout << endl;
for(int v: g[u])
if(v != p) dfs(v, u, h + 1);
}
int lca(int u, int v){
// First both u, v should be at the same distance from the root (height).
if(dist[u] < dist[v]) swap(u, v);
int offset = dist[u] - dist[v];
for(int i = 0; i < LOG2N; i++)
if(offset & (1 << i)) u = up[u][i];
// At this point u is at the same hight of v, if u == v it means v was the LCA of u.
if(u == v) return u;
// We want to lift up u and v to be childs of the lca, if jumping from
// u and v a distance 2^i ends in the same vertex (up[u][i] == up[v][i]) it
// means that it is the LCA or the vertex is above the LCA, then we do not jump that distance,
// we always want to be under the LCA in order to end at the childs of the LCA.
for(int i = LOG2N - 1; i >= 0; i--)
if(up[u][i] != up[v][i])
u = up[u][i], v = up[v][i];
return up[u][0];
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n, m; cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v;
//cout << u << ' ' << v << endl;
graph[u].push_back(v); graph[v].push_back(u);
}
vector <int> par(n, -1), vis(n, 0), nodes;
vector<int> q = {0};
vis[0] = 1;
while(!q.empty()){
vector <int> aux;
for (int& u : q) {
nodes.push_back(u);
for (int& v : graph[u]) {
if (vis[v]) continue;
par[v] = u;
vis[v] = 1;
aux.push_back(v);
}
}
sort(aux.begin(), aux.end());
q = aux;
}
//cout << "break" << endl;
for (int i = 1; i < n; i++) {
//cout << i << ' ' << par[i] << endl;
g[i].push_back(par[i]);
g[par[i]].push_back(i);
}
dfs(0, -1, 0);
//cout << up[5][0] << endl;
long long ans = 0;
for (int i = 1; i < nodes.size(); i++) {
//cout << nodes[i] << ' ';
int u = nodes[i - 1], v = nodes[i];
int w = lca(u, v);
//cout << u << ' ' << v << ' ' << w << endl;
ans += dist[u] + dist[v] - 2ll * dist[w];
}
//cout << up[5][0] << endl;
//cout << lca(1, 5) << endl;
cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 12848kb
input:
6 9 3 4 3 0 3 5 3 2 4 1 4 2 1 0 0 5 5 2
output:
12
result:
ok single line: '12'
Test #2:
score: 0
Accepted
time: 6ms
memory: 13012kb
input:
7 10 6 1 6 4 6 3 1 2 1 4 1 3 2 4 2 5 4 0 3 0
output:
19
result:
ok single line: '19'
Test #3:
score: 0
Accepted
time: 1ms
memory: 12996kb
input:
6 5 3 0 0 2 2 1 2 4 1 5
output:
11
result:
ok single line: '11'
Test #4:
score: 0
Accepted
time: 2ms
memory: 12900kb
input:
4 5 2 3 2 0 2 1 3 0 0 1
output:
5
result:
ok single line: '5'
Test #5:
score: 0
Accepted
time: 3ms
memory: 13012kb
input:
7 7 0 4 0 3 4 1 4 2 1 6 3 6 6 5
output:
11
result:
ok single line: '11'
Test #6:
score: 0
Accepted
time: 2ms
memory: 13052kb
input:
4 4 2 1 2 3 1 0 0 3
output:
6
result:
ok single line: '6'
Test #7:
score: 0
Accepted
time: 6ms
memory: 13064kb
input:
3 2 2 0 0 1
output:
3
result:
ok single line: '3'
Test #8:
score: 0
Accepted
time: 2ms
memory: 12920kb
input:
4 6 0 3 0 2 0 1 3 2 3 1 2 1
output:
5
result:
ok single line: '5'
Test #9:
score: 0
Accepted
time: 2ms
memory: 13008kb
input:
7 6 4 2 4 6 4 5 2 3 3 0 5 1
output:
9
result:
ok single line: '9'
Test #10:
score: 0
Accepted
time: 6ms
memory: 12912kb
input:
3 3 0 2 0 1 2 1
output:
3
result:
ok single line: '3'
Test #11:
score: 0
Accepted
time: 0ms
memory: 12936kb
input:
100 112 11 59 11 93 11 27 11 17 11 66 11 3 11 80 59 0 59 17 59 89 93 71 93 98 93 22 0 81 27 88 27 82 27 13 27 26 81 97 81 95 81 32 17 33 17 13 17 19 17 70 88 61 88 60 88 16 88 25 89 39 89 9 66 84 66 23 61 57 61 75 61 25 61 55 61 8 61 47 3 50 3 15 3 44 3 46 39 23 39 5 39 78 97 18 97 74 97 67 97 77 33...
output:
716
result:
ok single line: '716'
Test #12:
score: 0
Accepted
time: 2ms
memory: 12948kb
input:
200 296 120 0 120 29 120 68 120 40 120 126 120 178 120 135 0 53 0 174 29 67 29 166 29 93 68 121 68 187 53 85 53 4 53 94 53 13 53 183 53 111 53 10 67 39 67 116 67 26 67 75 85 164 85 195 85 3 85 183 85 105 85 92 40 70 40 123 4 90 4 181 4 72 164 189 164 194 164 76 164 132 164 163 164 11 164 69 164 19 1...
output:
1346
result:
ok single line: '1346'
Test #13:
score: 0
Accepted
time: 2ms
memory: 12980kb
input:
300 589 155 138 155 114 155 236 155 33 155 83 155 93 155 231 138 268 138 27 138 12 138 267 138 221 138 203 138 46 138 283 268 41 268 163 268 149 268 45 268 264 268 38 41 252 41 50 41 34 41 292 27 77 27 213 27 226 27 146 27 64 27 229 27 10 163 183 163 209 163 122 163 255 163 120 163 295 163 217 163 1...
output:
2244
result:
ok single line: '2244'
Test #14:
score: 0
Accepted
time: 1ms
memory: 12932kb
input:
400 400 335 205 335 378 335 187 335 94 205 111 205 267 205 104 205 230 205 249 205 276 205 319 111 262 111 235 111 35 111 138 111 24 262 219 262 302 262 208 262 46 262 228 235 192 235 178 235 181 267 30 267 264 267 76 267 371 267 272 104 96 104 188 104 340 104 33 30 334 30 115 334 279 334 36 334 391...
output:
3384
result:
ok single line: '3384'
Test #15:
score: 0
Accepted
time: 3ms
memory: 12952kb
input:
500 597 134 340 134 99 134 237 134 4 134 56 134 318 134 147 134 1 134 321 134 44 340 0 340 311 340 208 340 445 340 285 340 473 340 450 340 384 99 316 99 136 99 360 99 435 99 412 99 188 99 410 99 11 316 23 316 269 316 405 316 58 316 447 316 194 0 414 0 149 0 6 0 109 136 350 136 23 136 214 136 335 136...
output:
4602
result:
ok single line: '4602'
Test #16:
score: 0
Accepted
time: 3ms
memory: 13184kb
input:
600 859 121 503 121 58 121 15 121 514 121 235 121 436 503 477 503 395 503 362 503 37 58 421 58 142 58 29 58 202 58 183 58 558 58 593 58 21 58 79 477 383 477 6 477 125 477 247 421 530 421 599 421 560 421 62 421 441 421 235 142 150 142 597 142 101 142 236 142 39 142 368 530 71 530 122 530 407 530 157 ...
output:
5930
result:
ok single line: '5930'
Test #17:
score: 0
Accepted
time: 2ms
memory: 13064kb
input:
700 893 104 402 104 3 104 43 104 485 104 521 104 651 402 480 402 275 402 81 402 499 402 228 402 383 402 694 402 222 402 498 402 359 402 120 402 283 402 526 480 409 480 147 480 437 480 510 480 309 480 691 480 41 480 261 275 616 275 92 275 561 275 180 275 479 81 328 81 449 499 337 499 398 499 699 499 ...
output:
7303
result:
ok single line: '7303'
Test #18:
score: 0
Accepted
time: 3ms
memory: 13080kb
input:
800 1373 617 185 617 169 617 301 617 579 617 712 617 508 617 706 185 721 185 674 185 335 185 64 185 91 185 184 185 628 185 192 185 651 185 386 185 748 721 409 721 192 674 779 674 255 674 644 335 667 335 138 335 780 335 60 335 110 335 548 335 648 335 339 335 95 335 151 335 334 335 58 335 36 335 105 3...
output:
7699
result:
ok single line: '7699'
Test #19:
score: 0
Accepted
time: 0ms
memory: 13200kb
input:
900 1567 2 580 2 189 2 361 2 575 2 449 2 175 2 889 580 624 580 750 580 418 580 592 580 869 624 151 624 585 624 767 624 721 151 585 151 173 151 676 151 90 151 595 151 421 151 696 151 800 151 392 585 235 585 534 585 198 585 96 585 389 585 815 585 399 585 118 189 341 189 171 189 700 189 21 189 803 189 ...
output:
8597
result:
ok single line: '8597'
Test #20:
score: 0
Accepted
time: 7ms
memory: 13124kb
input:
1000 1988 683 135 683 540 683 97 683 945 683 265 683 62 683 18 683 322 683 927 683 791 135 853 135 936 135 581 135 530 135 722 540 254 540 826 540 926 540 65 540 696 540 383 540 911 540 636 540 863 254 541 254 164 254 427 254 982 254 56 254 546 254 850 254 569 826 775 826 856 826 19 826 985 826 317 ...
output:
8732
result:
ok single line: '8732'
Test #21:
score: 0
Accepted
time: 79ms
memory: 32724kb
input:
100000 147312 62593 93379 62593 46170 62593 34195 62593 47110 62593 59794 62593 30550 62593 31608 62593 88347 62593 58876 62593 27700 62593 71755 62593 36062 62593 33060 93379 28479 93379 5116 93379 40975 93379 56125 93379 55001 93379 60382 93379 87183 93379 38507 93379 39449 93379 32623 93379 59245...
output:
1962118
result:
ok single line: '1962118'
Test #22:
score: 0
Accepted
time: 86ms
memory: 33020kb
input:
100000 197080 17596 58554 17596 91986 17596 9940 17596 26138 17596 54150 17596 2804 17596 9058 17596 56492 17596 12732 17596 43493 17596 38935 17596 58008 17596 686 17596 36980 58554 28457 58554 34282 58554 18111 58554 62670 58554 61963 58554 3545 58554 2316 58554 20617 58554 6136 58554 2597 58554 3...
output:
1566991
result:
ok single line: '1566991'
Test #23:
score: 0
Accepted
time: 99ms
memory: 32960kb
input:
100000 193771 60299 16334 60299 46076 60299 71103 60299 54030 60299 17900 60299 3203 60299 23608 60299 59476 60299 73219 60299 18833 60299 51356 60299 35 60299 5387 60299 25994 60299 50973 16334 92829 16334 97127 16334 87398 16334 84464 16334 45145 16334 34997 16334 75157 92829 15045 92829 25988 928...
output:
1595267
result:
ok single line: '1595267'
Test #24:
score: 0
Accepted
time: 99ms
memory: 32980kb
input:
100000 185721 86154 60431 86154 33933 86154 25073 86154 65649 86154 34172 86154 77030 86154 56715 86154 2609 86154 73528 86154 92490 86154 77025 86154 77490 86154 46354 86154 74661 86154 91119 86154 14890 86154 18153 86154 35177 86154 8660 86154 44713 86154 7651 86154 49429 86154 54481 60431 48123 6...
output:
1691846
result:
ok single line: '1691846'
Test #25:
score: 0
Accepted
time: 75ms
memory: 32356kb
input:
100000 131482 18119 46839 18119 27991 18119 17383 18119 20046 18119 12235 18119 56659 18119 86476 18119 49506 18119 91940 18119 8409 18119 90849 18119 31537 18119 18641 18119 26041 18119 80525 18119 65747 46839 86651 46839 57918 46839 39039 46839 9399 46839 23395 46839 97663 46839 67674 46839 71288 ...
output:
2212134
result:
ok single line: '2212134'
Test #26:
score: 0
Accepted
time: 97ms
memory: 57308kb
input:
200000 199999 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51...
output:
199999
result:
ok single line: '199999'
Test #27:
score: 0
Accepted
time: 232ms
memory: 54336kb
input:
200000 200000 160315 175190 160315 196064 175190 30728 30728 169883 169883 184253 184253 166019 166019 67937 67937 41491 41491 41684 41684 149325 149325 73374 73374 88370 88370 149206 149206 116875 116875 72976 72976 159596 159596 28335 28335 87686 87686 56156 56156 93213 93213 190125 190125 115678 ...
output:
15009557238
result:
ok single line: '15009557238'
Test #28:
score: 0
Accepted
time: 84ms
memory: 54484kb
input:
200000 199999 0 1 0 100001 1 2 1 100002 2 3 2 100003 3 4 3 100004 4 5 4 100005 5 6 5 100006 6 7 6 100007 7 8 7 100008 8 9 8 100009 9 10 9 100010 10 11 10 100011 11 12 11 100012 12 13 12 100013 13 14 13 100014 14 15 14 100015 15 16 15 100016 16 17 16 100017 17 18 17 100018 18 19 18 100019 19 20 19 10...
output:
499996
result:
ok single line: '499996'
Test #29:
score: 0
Accepted
time: 114ms
memory: 34484kb
input:
100000 200000 48014 90403 48014 13065 48014 53291 48014 65440 48014 38558 48014 45153 48014 17676 48014 90714 48014 40538 48014 13423 48014 65416 48014 19995 48014 85383 48014 39494 48014 88398 48014 33763 48014 63656 48014 17015 48014 62040 48014 30816 48014 76592 48014 23894 48014 92818 48014 7661...
output:
3164059590
result:
ok single line: '3164059590'
Test #30:
score: 0
Accepted
time: 177ms
memory: 51712kb
input:
200000 199999 187154 58121 58121 95209 58121 159095 95209 45094 95209 164461 159095 33139 159095 14102 45094 92254 45094 25050 164461 162982 164461 198880 33139 93303 33139 151572 14102 120255 14102 190599 92254 95265 92254 88631 25050 90582 25050 181873 162982 67414 162982 23995 198880 185327 19888...
output:
5386584
result:
ok single line: '5386584'
Test #31:
score: 0
Accepted
time: 93ms
memory: 40524kb
input:
133334 199999 0 1 0 2 1 3 2 3 2 4 3 5 4 5 4 6 5 7 6 7 6 8 7 9 8 9 8 10 9 11 10 11 10 12 11 13 12 13 12 14 13 15 14 15 14 16 15 17 16 17 16 18 17 19 18 19 18 20 19 21 20 21 20 22 21 23 22 23 22 24 23 25 24 25 24 26 25 27 26 27 26 28 27 29 28 29 28 30 29 31 30 31 30 32 31 33 32 33 32 34 33 35 34 35 34...
output:
8888911111
result:
ok single line: '8888911111'
Test #32:
score: 0
Accepted
time: 12ms
memory: 13612kb
input:
450 101025 403 213 403 211 403 440 403 334 403 322 403 214 403 32 403 190 403 399 403 397 403 65 403 15 403 87 403 105 403 17 403 134 403 302 403 443 403 162 403 404 403 31 403 352 403 280 403 256 403 338 403 192 403 312 403 258 403 95 403 294 403 319 403 23 403 115 403 410 403 449 403 160 403 116 4...
output:
897
result:
ok single line: '897'
Test #33:
score: 0
Accepted
time: 29ms
memory: 14948kb
input:
1000 200000 123 58 123 326 123 363 123 394 123 127 123 328 123 382 123 370 123 658 123 768 123 215 123 421 123 924 123 241 123 84 123 252 123 378 123 530 123 223 123 752 123 810 123 40 123 731 123 302 123 933 123 298 123 266 123 918 123 721 123 831 123 732 123 92 123 9 123 789 123 171 123 33 123 618...
output:
1998
result:
ok single line: '1998'
Test #34:
score: 0
Accepted
time: 28ms
memory: 16968kb
input:
10000 200000 361 3020 361 2289 361 765 361 6616 361 5039 361 3951 361 3056 361 4004 361 5078 361 5662 361 8774 361 8170 361 8593 361 2189 361 8761 361 5777 361 6140 361 516 361 2423 361 1915 361 8176 361 6973 361 2341 361 6115 361 8776 361 5546 361 8937 361 9843 361 9760 361 4374 361 2272 361 1371 3...
output:
19998
result:
ok single line: '19998'
Test #35:
score: 0
Accepted
time: 72ms
memory: 53740kb
input:
200000 199999 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 ...
output:
399997
result:
ok single line: '399997'
Test #36:
score: 0
Accepted
time: 56ms
memory: 33368kb
input:
100000 199997 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 10 0 11 0 12 0 13 0 14 0 15 0 16 0 17 0 18 0 19 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 27 0 28 0 29 0 30 0 31 0 32 0 33 0 34 0 35 0 36 0 37 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 47 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 58 0 59 ...
output:
199997
result:
ok single line: '199997'
Test #37:
score: 0
Accepted
time: 52ms
memory: 23276kb
input:
50000 165901 10470 48545 10470 3142 10470 35516 10470 33681 10470 19438 10470 48838 10470 9040 10470 35883 10470 41154 10470 14793 10470 24125 10470 46508 10470 33398 10470 13588 10470 42591 48545 3142 3142 35516 3142 33681 35516 33681 35516 48838 33681 19438 33681 48838 33681 35883 19438 48838 1943...
output:
40719760
result:
ok single line: '40719760'
Test #38:
score: 0
Accepted
time: 41ms
memory: 24796kb
input:
60001 60000 0 1 0 2 0 3 1 4 2 5 3 6 4 7 5 8 6 9 7 10 8 11 9 12 10 13 11 14 12 15 13 16 14 17 15 18 16 19 17 20 18 21 19 22 20 23 21 24 22 25 23 26 24 27 25 28 26 29 27 30 28 31 29 32 30 33 31 34 32 35 33 36 34 37 35 38 36 39 37 40 38 41 39 42 40 43 41 44 42 45 43 46 44 47 45 48 46 49 47 50 48 51 49 ...
output:
1200040000
result:
ok single line: '1200040000'
Test #39:
score: 0
Accepted
time: 43ms
memory: 25348kb
input:
60001 60000 15830 40917 15830 6959 15830 40438 40917 3623 6959 40214 40438 29929 3623 39458 40214 43634 29929 22937 39458 22356 43634 16591 22937 4101 22356 37084 16591 25558 4101 50945 37084 16662 25558 46830 50945 8514 16662 37435 46830 867 8514 31777 37435 46819 867 36539 31777 10009 46819 55208 ...
output:
634769582
result:
ok single line: '634769582'
Test #40:
score: 0
Accepted
time: 46ms
memory: 33420kb
input:
100000 157093 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 10 0 11 0 12 0 14 0 16 0 17 0 18 0 20 0 21 0 22 0 23 0 24 0 25 0 26 0 30 0 31 0 33 0 34 0 35 0 37 0 40 0 41 0 42 0 43 0 44 0 46 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 56 0 60 0 61 0 62 0 63 0 65 0 66 0 69 0 70 0 71 0 72 0 73 0 75 0 77 0 80 0 81 0 82 0 83 0 8...
output:
245653
result:
ok single line: '245653'
Test #41:
score: 0
Accepted
time: 154ms
memory: 44528kb
input:
150001 200000 31165 133948 31165 48844 133948 43625 48844 43625 43625 45915 43625 132932 45915 109767 132932 109767 109767 12765 109767 33199 12765 17314 33199 17314 17314 57151 17314 102018 57151 125868 102018 125868 125868 111967 125868 58356 111967 97132 58356 97132 97132 107663 97132 1909 107663...
output:
28258817
result:
ok single line: '28258817'
Test #42:
score: 0
Accepted
time: 149ms
memory: 43212kb
input:
149998 199997 71314 107183 71314 24384 71314 149624 107183 18249 24384 18249 18249 70169 18249 42765 70169 30327 42765 30327 30327 103231 30327 109311 103231 88766 109311 88766 88766 126403 88766 133745 126403 51979 133745 51979 51979 78962 51979 135030 78962 72291 135030 72291 72291 61824 72291 382...
output:
4850301383
result:
ok single line: '4850301383'
Test #43:
score: 0
Accepted
time: 108ms
memory: 53116kb
input:
200000 200000 0 1 0 50001 0 100001 0 150001 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 ...
output:
8750100000
result:
ok single line: '8750100000'
Test #44:
score: 0
Accepted
time: 243ms
memory: 54220kb
input:
200000 200000 160933 48296 160933 129987 160933 143931 160933 28925 48296 137291 137291 115897 115897 146844 146844 76449 76449 182958 182958 109438 109438 192579 192579 41005 41005 27025 27025 195460 195460 71977 71977 10947 10947 107731 107731 63881 63881 72994 72994 194566 194566 154939 154939 70...
output:
7037869697
result:
ok single line: '7037869697'