QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#181447#2169. 'S No ProblemAlleks_BWA 424ms24412kbC++142.6kb2023-09-16 19:13:102023-09-16 19:13:10

Judging History

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

  • [2023-09-16 19:13:10]
  • 评测
  • 测评结果:WA
  • 用时:424ms
  • 内存:24412kb
  • [2023-09-16 19:13:10]
  • 提交

answer

#include <bits/stdc++.h>
#define L 100005
#define LIM 10
#define PRIME 1000000123
using namespace std;

int n;
vector <pair <int, int>> G[L];
int root = 1, pathLength, maxPath, node1, node2, node3, diam1, diam2, sum;
int lev[L], t[L];
bool vis[L];
map <pair <int, int>, bool> mp;

void init() {
  for (int i = 1; i <= n; i++)
    vis[i] = false;
  pathLength = 0;
  maxPath = -L;
}

void switchEdge(int x, int y) {
  mp[{x, y}] = mp[{y, x}] = true;
}

void switchChain(int x, int y) {
  if (lev[x] < lev[y])
    swap(x, y);
  while (lev[x] > lev[y]) {
    switchEdge(x, t[x]);
    x = t[x];
  }
  while (x != y) {
    switchEdge(x, t[x]);
    switchEdge(y, t[y]);
    x = t[x];
    y = t[y];
  }
}

void DFS_1(int node) {
  vis[node] = true;
  if (maxPath < pathLength) {
    maxPath = pathLength;
    node1 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      lev[it.first] = lev[node] + 1;
      t[it.first] = node;
      pathLength += it.second;
      DFS_1(it.first);
      pathLength -= it.second;
    }
  }
}

void DFS_2(int node) {
  vis[node] = true;
  if (maxPath < pathLength) {
    maxPath = pathLength;
    node2 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      pathLength += it.second;
      DFS_2(it.first);
      pathLength -= it.second;
    }
  }
}

void DFS_3(int node) {
  vis[node] = true;
  if (node != root && maxPath < pathLength) {
    maxPath = pathLength;
    node3 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      int cost = it.second;
      if (mp[{node, it.first}])
        cost = -cost;
      pathLength += cost;
      DFS_3(it.first);
      pathLength -= cost;
    }
  }
}

void DFS_4(int node) {
  vis[node] = true;
  maxPath = max(maxPath, pathLength);
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      int cost = it.second;
      if (mp[{node, it.first}])
        cost = -cost;
      pathLength += cost;
      DFS_4(it.first);
      pathLength -= cost;
    }
  }
}

int main(){
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
    sum += c;
  }

  init();
  DFS_1(root);

  init();
  DFS_2(node1);
  diam1 = maxPath;

  switchChain(node1, node2);

  int randNode = root;
  for (int i = 0; i < LIM; i++) {
    init();
    DFS_3(randNode);

    init();
    DFS_4(node3);
    diam2 = max(diam2, maxPath);

    randNode = (randNode - 1 + PRIME) % n + 1;
  }

  cout << sum * 2 - diam1 - diam2 << "\n";
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 6596kb

input:

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

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 0ms
memory: 6392kb

input:

6
6 2 1
4 6 1
3 1 1
1 5 1
1 6 10

output:

15

result:

ok single line: '15'

Test #3:

score: 0
Accepted
time: 0ms
memory: 5896kb

input:

6
1 3 1
3 2 1
3 4 10
5 4 1
4 6 1

output:

15

result:

ok single line: '15'

Test #4:

score: 0
Accepted
time: 1ms
memory: 6004kb

input:

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

output:

6

result:

ok single line: '6'

Test #5:

score: 0
Accepted
time: 1ms
memory: 6492kb

input:

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

output:

16

result:

ok single line: '16'

Test #6:

score: 0
Accepted
time: 0ms
memory: 5932kb

input:

6
1 6 8
2 6 2
3 6 3
4 6 5
5 6 1

output:

20

result:

ok single line: '20'

Test #7:

score: 0
Accepted
time: 1ms
memory: 5980kb

input:

7
6 4 60
4 2 463
6 7 165
6 3 81
6 1 30
4 5 214

output:

1103

result:

ok single line: '1103'

Test #8:

score: 0
Accepted
time: 1ms
memory: 6164kb

input:

7
1 3 463
1 5 214
7 2 165
7 4 81
7 1 60
7 6 30

output:

1103

result:

ok single line: '1103'

Test #9:

score: 0
Accepted
time: 0ms
memory: 5940kb

input:

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

output:

24

result:

ok single line: '24'

Test #10:

score: 0
Accepted
time: 1ms
memory: 6568kb

input:

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

output:

46

result:

ok single line: '46'

Test #11:

score: 0
Accepted
time: 1ms
memory: 5916kb

input:

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

output:

46

result:

ok single line: '46'

Test #12:

score: 0
Accepted
time: 1ms
memory: 5944kb

input:

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

output:

49

result:

ok single line: '49'

Test #13:

score: 0
Accepted
time: 0ms
memory: 5944kb

input:

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

output:

12

result:

ok single line: '12'

Test #14:

score: 0
Accepted
time: 0ms
memory: 6408kb

input:

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

output:

10

result:

ok single line: '10'

Test #15:

score: 0
Accepted
time: 1ms
memory: 5996kb

input:

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

output:

10

result:

ok single line: '10'

Test #16:

score: 0
Accepted
time: 2ms
memory: 5992kb

input:

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

output:

17

result:

ok single line: '17'

Test #17:

score: 0
Accepted
time: 280ms
memory: 24412kb

input:

100000
1 2 1
1 20001 1
1 40001 1
1 60001 1
1 80001 1
2 3 1
20001 20002 1
40001 40002 1
60001 60002 1
80001 80002 1
3 4 1
20002 20003 1
40002 40003 1
60002 60003 1
80002 80003 1
4 5 1
20003 20004 1
40003 40004 1
60003 60004 1
80003 80004 1
5 6 1
20004 20005 1
40004 40005 1
60004 60005 1
80004 80005 1...

output:

119998

result:

ok single line: '119998'

Test #18:

score: 0
Accepted
time: 1ms
memory: 5980kb

input:

11
11 4 1
10 4 10
9 3 1
8 2 6
7 4 20
6 3 5
5 2 7
4 3 2
3 2 3
2 1 10

output:

82

result:

ok single line: '82'

Test #19:

score: 0
Accepted
time: 1ms
memory: 6004kb

input:

11
10 4 7
7 11 20
7 8 2
6 8 1
8 1 5
3 7 1
8 10 3
2 7 10
5 10 6
10 9 10

output:

82

result:

ok single line: '82'

Test #20:

score: 0
Accepted
time: 1ms
memory: 6104kb

input:

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

output:

58

result:

ok single line: '58'

Test #21:

score: 0
Accepted
time: 1ms
memory: 6056kb

input:

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

output:

58

result:

ok single line: '58'

Test #22:

score: 0
Accepted
time: 0ms
memory: 6112kb

input:

12
9 2 2
2 10 10
2 12 11
1 12 3
12 8 5
12 11 7
12 7 1
4 7 8
3 7 4
7 6 9
7 5 6

output:

87

result:

ok single line: '87'

Test #23:

score: 0
Accepted
time: 0ms
memory: 5996kb

input:

12
8 2 10
7 12 10
1 7 10
4 2 10
2 1 1
9 6 3
5 9 3
1 9 1
10 9 3
9 11 3
3 9 3

output:

70

result:

ok single line: '70'

Test #24:

score: 0
Accepted
time: 0ms
memory: 6056kb

input:

12
7 3 3
10 3 8
3 2 4
11 2 4
2 12 6
2 1 3
3 4 6
3 6 2
4 8 5
4 5 1
4 9 7

output:

64

result:

ok single line: '64'

Test #25:

score: 0
Accepted
time: 1ms
memory: 6736kb

input:

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

output:

22

result:

ok single line: '22'

Test #26:

score: 0
Accepted
time: 1ms
memory: 5920kb

input:

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

output:

21

result:

ok single line: '21'

Test #27:

score: 0
Accepted
time: 1ms
memory: 6060kb

input:

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

output:

28

result:

ok single line: '28'

Test #28:

score: 0
Accepted
time: 1ms
memory: 5980kb

input:

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

output:

22

result:

ok single line: '22'

Test #29:

score: 0
Accepted
time: 1ms
memory: 6076kb

input:

20
1 2 154
1 3 125
3 4 242
1 5 415
5 6 250
2 7 245
1 8 477
1 9 284
2 10 220
8 11 437
4 12 186
7 13 73
3 14 22
2 15 365
1 16 138
15 17 132
11 18 79
3 19 285
4 20 61

output:

5518

result:

ok single line: '5518'

Test #30:

score: 0
Accepted
time: 1ms
memory: 6004kb

input:

25
1 2 147
2 3 259
2 4 270
1 5 435
4 6 421
4 7 236
5 8 169
7 9 448
2 10 472
10 11 496
10 12 131
1 13 148
1 14 187
3 15 214
11 16 128
10 17 378
5 18 156
5 19 62
8 20 480
14 21 176
6 22 412
19 23 389
3 24 244
21 25 318

output:

9588

result:

ok single line: '9588'

Test #31:

score: 0
Accepted
time: 1ms
memory: 6344kb

input:

30
1 2 428
2 3 240
3 4 229
3 5 372
3 6 49
4 7 116
1 8 271
7 9 311
1 10 47
6 11 59
6 12 405
1 13 161
3 14 414
8 15 467
1 16 480
1 17 274
12 18 251
6 19 169
19 20 480
15 21 378
5 22 484
12 23 486
20 24 224
20 25 186
7 26 401
9 27 327
24 28 401
16 29 79
16 30 429

output:

12290

result:

ok single line: '12290'

Test #32:

score: 0
Accepted
time: 1ms
memory: 6428kb

input:

100
1 2 1
1 3 1
2 4 1
2 5 1
2 6 1
6 7 1
7 8 1
4 9 1
9 10 1
9 11 1
11 12 1
5 13 1
2 14 1
14 15 1
1 16 1
3 17 1
4 18 1
13 19 1
4 20 1
14 21 1
19 22 1
5 23 1
14 24 1
17 25 1
4 26 1
12 27 1
16 28 1
9 29 1
6 30 1
22 31 1
19 32 1
24 33 1
16 34 1
15 35 1
2 36 1
23 37 1
25 38 1
3 39 1
37 40 1
32 41 1
28 42 ...

output:

173

result:

ok single line: '173'

Test #33:

score: 0
Accepted
time: 2ms
memory: 6036kb

input:

500
1 2 1
2 3 1
3 4 1
1 5 1
3 6 1
3 7 1
5 8 1
8 9 1
4 10 1
3 11 1
5 12 1
1 13 1
2 14 1
10 15 1
14 16 1
7 17 1
5 18 1
4 19 1
19 20 1
18 21 1
19 22 1
7 23 1
18 24 1
19 25 1
24 26 1
10 27 1
18 28 1
8 29 1
22 30 1
5 31 1
2 32 1
7 33 1
19 34 1
33 35 1
9 36 1
5 37 1
17 38 1
35 39 1
24 40 1
27 41 1
27 42 1...

output:

960

result:

ok single line: '960'

Test #34:

score: 0
Accepted
time: 0ms
memory: 6896kb

input:

1000
1 2 1
1 3 1
3 4 1
4 5 1
2 6 1
6 7 1
5 8 1
4 9 1
2 10 1
7 11 1
10 12 1
10 13 1
11 14 1
11 15 1
9 16 1
16 17 1
3 18 1
1 19 1
12 20 1
6 21 1
8 22 1
20 23 1
18 24 1
9 25 1
14 26 1
24 27 1
1 28 1
17 29 1
6 30 1
5 31 1
9 32 1
17 33 1
10 34 1
23 35 1
35 36 1
1 37 1
24 38 1
38 39 1
9 40 1
25 41 1
18 42...

output:

1951

result:

ok single line: '1951'

Test #35:

score: 0
Accepted
time: 3ms
memory: 6172kb

input:

1000
1 2 9
2 3 9
2 4 4
2 5 7
2 6 3
2 7 10
7 8 3
2 9 3
2 10 4
8 11 9
10 12 2
6 13 1
5 14 3
11 15 4
15 16 1
5 17 9
6 18 9
18 19 8
13 20 5
9 21 7
12 22 3
12 23 5
23 24 10
16 25 4
24 26 9
15 27 6
6 28 4
24 29 6
28 30 3
28 31 8
9 32 2
24 33 5
22 34 5
19 35 1
29 36 5
31 37 1
31 38 10
25 39 3
33 40 2
22 41...

output:

10691

result:

ok single line: '10691'

Test #36:

score: 0
Accepted
time: 0ms
memory: 6108kb

input:

1000
1 2 9
2 3 6
1 4 3
1 5 1
5 6 1
1 7 3
2 8 5
6 9 5
5 10 8
8 11 9
5 12 9
3 13 2
6 14 5
8 15 3
13 16 6
1 17 1
10 18 8
5 19 8
15 20 5
9 21 9
2 22 2
5 23 7
7 24 4
9 25 3
6 26 7
11 27 5
18 28 1
18 29 6
14 30 5
5 31 5
18 32 9
10 33 1
32 34 4
15 35 10
6 36 3
29 37 5
5 38 9
25 39 4
37 40 7
9 41 6
31 42 10...

output:

10698

result:

ok single line: '10698'

Test #37:

score: 0
Accepted
time: 415ms
memory: 16772kb

input:

100000
1 2 6
1 3 3
1 4 3
3 5 1
3 6 6
5 7 4
4 8 2
4 9 1
7 10 8
6 11 7
4 12 3
12 13 10
7 14 4
8 15 8
10 16 1
3 17 8
5 18 1
16 19 4
13 20 1
4 21 10
20 22 7
12 23 10
6 24 3
22 25 5
21 26 2
15 27 7
10 28 4
8 29 6
26 30 9
14 31 10
15 32 5
19 33 6
6 34 2
18 35 2
14 36 5
29 37 7
10 38 2
37 39 6
32 40 3
17 4...

output:

1096451

result:

ok single line: '1096451'

Test #38:

score: 0
Accepted
time: 411ms
memory: 16864kb

input:

100000
1 2 322
1 3 195
2 4 186
1 5 249
4 6 431
1 7 445
7 8 73
7 9 71
1 10 319
9 11 183
7 12 434
7 13 186
12 14 341
2 15 472
14 16 2
14 17 71
15 18 117
12 19 69
12 20 29
10 21 138
19 22 100
22 23 30
16 24 224
14 25 283
22 26 310
13 27 215
10 28 278
2 29 201
21 30 234
9 31 34
23 32 349
26 33 194
2 34 ...

output:

50027510

result:

ok single line: '50027510'

Test #39:

score: 0
Accepted
time: 419ms
memory: 16844kb

input:

100000
1 2 146
2 3 135
2 4 303
3 5 300
1 6 227
5 7 279
7 8 173
2 9 340
6 10 367
7 11 363
1 12 104
12 13 134
13 14 310
5 15 374
3 16 231
5 17 60
11 18 395
14 19 350
11 20 448
6 21 287
21 22 111
9 23 40
15 24 223
2 25 158
24 26 272
15 27 470
2 28 397
11 29 357
11 30 296
19 31 267
4 32 211
19 33 272
17...

output:

50072806

result:

ok single line: '50072806'

Test #40:

score: 0
Accepted
time: 423ms
memory: 16760kb

input:

100000
1 2 378
1 3 164
2 4 255
1 5 142
1 6 152
4 7 276
4 8 157
7 9 209
1 10 407
10 11 481
8 12 150
7 13 67
8 14 316
2 15 297
12 16 137
8 17 436
7 18 151
5 19 215
13 20 256
20 21 392
12 22 266
11 23 480
8 24 491
2 25 490
25 26 190
8 27 210
21 28 400
14 29 77
25 30 12
22 31 129
26 32 332
26 33 2
18 34...

output:

50132921

result:

ok single line: '50132921'

Test #41:

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

input:

100000
1 2 280
1 3 358
2 4 95
4 5 225
2 6 329
6 7 338
6 8 431
5 9 480
3 10 418
3 11 357
1 12 433
1 13 248
13 14 107
13 15 370
11 16 94
4 17 119
16 18 265
7 19 62
8 20 261
18 21 225
5 22 311
1 23 353
1 24 254
14 25 379
11 26 152
9 27 490
15 28 131
1 29 126
4 30 298
20 31 432
15 32 397
10 33 173
4 34 ...

output:

50101696

result:

ok single line: '50101696'

Test #42:

score: 0
Accepted
time: 3ms
memory: 6408kb

input:

1000
970 113 1
569 319 1
715 682 279
804 299 210
615 353 1
733 464 1
306 245 1
180 779 281
841 284 1
1 402 1
21 229 1
128 323 1
76 55 1
57 467 1
943 967 1
572 158 1
690 927 1
15 655 1
430 146 1
374 31 1
825 399 1
368 669 1
524 375 1
616 91 1
121 20 1
604 759 1
186 730 1
697 816 1
837 84 1
73 461 1
5...

output:

23594

result:

ok single line: '23594'

Test #43:

score: 0
Accepted
time: 3ms
memory: 6960kb

input:

1000
343 861 1
967 872 1
364 889 1
290 807 1
390 832 1
302 173 1
590 252 1
238 232 1
742 432 1
180 291 1
358 997 1
73 728 1
971 526 1
61 6 1
433 486 1
96 325 1
124 790 1
605 360 1
923 41 1
229 618 1
263 721 1
669 784 1
774 393 1
659 481 1
514 132 259
376 909 1
841 537 1
538 133 1
385 453 1
651 437 1...

output:

20965

result:

ok single line: '20965'

Test #44:

score: -100
Wrong Answer
time: 0ms
memory: 6260kb

input:

1000
330 988 1
586 881 1
820 772 1
540 624 1
592 367 1
467 888 1
502 518 1
481 676 1
778 549 1
582 590 1
313 554 1
846 406 1
844 472 1
831 807 1
321 480 1
902 67 1
957 649 1
588 373 1
591 33 1
947 829 1
866 421 1
628 71 1
84 6 1
780 185 1
301 322 1
283 715 1
151 930 1
28 545 1
107 333 1
744 112 1
36...

output:

21963

result:

wrong answer 1st lines differ - expected: '21791', found: '21963'