#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define type(x) __typeof((x).begin())
#define foreach(it, x) for(type(x) it = (x).begin(); it != (x).end(); it++)
typedef long long ll;
typedef pair < int, int > ii;
const int inf = 1e9 + 333;
const ll linf = 1e18 + inf;
const int N = 1e5 + 5;
int curRoot;
vector < ii > v[N];
bool h[N];
int mx;
int dist[N], root[N];
vector < int > vec;
void dfs(int p, int x, int t = 0) {
root[x] = curRoot;
vec.push_back(x);
h[x] = 1;
dist[x] = t;
if(mx == -1 or t > dist[mx]) {
mx = x;
}
foreach(it, v[x]) {
int u = it -> first;
int e = it -> second;
if(u != p)
dfs(x, u, t + e);
}
}
int distA[N], distB[N];
int calcLp(int x) {
mx = -1;
vec.clear();
dfs(0, x);
//printf("mx = %d\n", mx);
//////////////////////////////
int A = mx;
mx = -1;
vec.clear();
dfs(0, A);
foreach(it, vec) {
int x = *it;
distA[x] = dist[x];
}
/////////////////////////////
int B = mx;
mx = -1;
vec.clear();
dfs(0, B);
foreach(it, vec) {
int x = *it;
distB[x] = dist[x];
}
int mn = 2 * inf, mni = -1;
foreach(it, vec) {
int x = *it;
//printf("x = %d\n", x);
if(abs(distA[x] - distB[x]) < mn) {
mn = abs(distA[x] - distB[x]);
mni = x;
}
}
//puts("");
assert(mni != -1);
return mni;
}
int f(int x) {
if(x != root[x])
return root[x] = f(root[x]);
return x;
}
int travelTime(int n, int m, int l, int A[], int B[], int T[]) {
for(int i = 0; i < m; i++) {
int x = A[i];
int y = B[i];
int c = T[i];
v[x].push_back(ii(y, c));
v[y].push_back(ii(x, c));
}
priority_queue < ii > Q;
for(int i = 0; i < n; i++) {
if(!h[i]) {
//printf("i = %d\n", i);
curRoot = i;
int x = calcLp(i);
int a = distA[x];
int b = distB[x];
if(a < b)
swap(a, b);
//printf("x = %d a = %d b = %d\n", x, a, b);
Q.push(ii(-a, -b));
}
}
while(Q.size() > 1) {
int x1 = -Q.top().first;
int y1 = -Q.top().second;
Q.pop();
int x2 = -Q.top().first;
int y2 = -Q.top().second;
Q.pop();
if(x1 + y1 > x1 + x2 + l) {
Q.push(ii(-(x1), -(y1)));
}
else if(x2 + y2 > x1 + x2 + l) {
Q.push(ii(-(x2), -(y2)));
}
else {
int n1 = max(x1, x2);
int n2 = min(x1, x2) + l;
if(n1 < n2)
swap(n1, n2);
Q.push(ii(-(n1), -(n2)));
}
}
return -(Q.top().first + Q.top().second);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
13044 KB |
Output is correct |
2 |
Correct |
72 ms |
12916 KB |
Output is correct |
3 |
Correct |
43 ms |
9460 KB |
Output is correct |
4 |
Correct |
12 ms |
4224 KB |
Output is correct |
5 |
Correct |
11 ms |
3584 KB |
Output is correct |
6 |
Correct |
18 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
34 ms |
6520 KB |
Output is correct |
9 |
Correct |
39 ms |
7928 KB |
Output is correct |
10 |
Correct |
4 ms |
2816 KB |
Output is correct |
11 |
Correct |
55 ms |
9464 KB |
Output is correct |
12 |
Correct |
78 ms |
11120 KB |
Output is correct |
13 |
Correct |
4 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
13044 KB |
Output is correct |
2 |
Correct |
72 ms |
12916 KB |
Output is correct |
3 |
Correct |
43 ms |
9460 KB |
Output is correct |
4 |
Correct |
12 ms |
4224 KB |
Output is correct |
5 |
Correct |
11 ms |
3584 KB |
Output is correct |
6 |
Correct |
18 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
34 ms |
6520 KB |
Output is correct |
9 |
Correct |
39 ms |
7928 KB |
Output is correct |
10 |
Correct |
4 ms |
2816 KB |
Output is correct |
11 |
Correct |
55 ms |
9464 KB |
Output is correct |
12 |
Correct |
78 ms |
11120 KB |
Output is correct |
13 |
Correct |
4 ms |
2816 KB |
Output is correct |
14 |
Correct |
4 ms |
2688 KB |
Output is correct |
15 |
Correct |
4 ms |
2688 KB |
Output is correct |
16 |
Correct |
4 ms |
2688 KB |
Output is correct |
17 |
Correct |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
4 ms |
2688 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2688 KB |
Output is correct |
22 |
Correct |
4 ms |
2688 KB |
Output is correct |
23 |
Correct |
4 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2688 KB |
Output is correct |
25 |
Correct |
3 ms |
2688 KB |
Output is correct |
26 |
Correct |
4 ms |
2688 KB |
Output is correct |
27 |
Correct |
5 ms |
2688 KB |
Output is correct |
28 |
Correct |
4 ms |
2660 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
4 ms |
2688 KB |
Output is correct |
33 |
Correct |
4 ms |
2688 KB |
Output is correct |
34 |
Correct |
3 ms |
2688 KB |
Output is correct |
35 |
Correct |
4 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
2688 KB |
Output is correct |
37 |
Correct |
4 ms |
2688 KB |
Output is correct |
38 |
Correct |
4 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
13044 KB |
Output is correct |
2 |
Correct |
72 ms |
12916 KB |
Output is correct |
3 |
Correct |
43 ms |
9460 KB |
Output is correct |
4 |
Correct |
12 ms |
4224 KB |
Output is correct |
5 |
Correct |
11 ms |
3584 KB |
Output is correct |
6 |
Correct |
18 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
34 ms |
6520 KB |
Output is correct |
9 |
Correct |
39 ms |
7928 KB |
Output is correct |
10 |
Correct |
4 ms |
2816 KB |
Output is correct |
11 |
Correct |
55 ms |
9464 KB |
Output is correct |
12 |
Correct |
78 ms |
11120 KB |
Output is correct |
13 |
Correct |
4 ms |
2816 KB |
Output is correct |
14 |
Correct |
4 ms |
2688 KB |
Output is correct |
15 |
Correct |
4 ms |
2688 KB |
Output is correct |
16 |
Correct |
4 ms |
2688 KB |
Output is correct |
17 |
Correct |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
4 ms |
2688 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2688 KB |
Output is correct |
22 |
Correct |
4 ms |
2688 KB |
Output is correct |
23 |
Correct |
4 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2688 KB |
Output is correct |
25 |
Correct |
3 ms |
2688 KB |
Output is correct |
26 |
Correct |
4 ms |
2688 KB |
Output is correct |
27 |
Correct |
5 ms |
2688 KB |
Output is correct |
28 |
Correct |
4 ms |
2660 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
4 ms |
2688 KB |
Output is correct |
33 |
Correct |
4 ms |
2688 KB |
Output is correct |
34 |
Correct |
3 ms |
2688 KB |
Output is correct |
35 |
Correct |
4 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
2688 KB |
Output is correct |
37 |
Correct |
4 ms |
2688 KB |
Output is correct |
38 |
Correct |
4 ms |
2688 KB |
Output is correct |
39 |
Correct |
71 ms |
12872 KB |
Output is correct |
40 |
Correct |
69 ms |
12912 KB |
Output is correct |
41 |
Correct |
47 ms |
9464 KB |
Output is correct |
42 |
Correct |
11 ms |
4224 KB |
Output is correct |
43 |
Correct |
3 ms |
2688 KB |
Output is correct |
44 |
Correct |
4 ms |
2688 KB |
Output is correct |
45 |
Correct |
4 ms |
2688 KB |
Output is correct |
46 |
Correct |
4 ms |
2688 KB |
Output is correct |
47 |
Correct |
4 ms |
2688 KB |
Output is correct |
48 |
Correct |
4 ms |
2688 KB |
Output is correct |
49 |
Correct |
4 ms |
2688 KB |
Output is correct |
50 |
Correct |
4 ms |
2688 KB |
Output is correct |
51 |
Correct |
4 ms |
2688 KB |
Output is correct |
52 |
Correct |
4 ms |
2688 KB |
Output is correct |
53 |
Correct |
4 ms |
2688 KB |
Output is correct |
54 |
Correct |
4 ms |
2688 KB |
Output is correct |
55 |
Correct |
4 ms |
2688 KB |
Output is correct |
56 |
Correct |
4 ms |
2688 KB |
Output is correct |
57 |
Correct |
113 ms |
9968 KB |
Output is correct |
58 |
Correct |
92 ms |
9844 KB |
Output is correct |
59 |
Correct |
86 ms |
9840 KB |
Output is correct |
60 |
Correct |
94 ms |
9716 KB |
Output is correct |
61 |
Correct |
91 ms |
9936 KB |
Output is correct |
62 |
Correct |
79 ms |
9664 KB |
Output is correct |
63 |
Correct |
86 ms |
9716 KB |
Output is correct |
64 |
Incorrect |
58 ms |
9080 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
7156 KB |
Output is correct |
2 |
Incorrect |
45 ms |
7296 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
13044 KB |
Output is correct |
2 |
Correct |
72 ms |
12916 KB |
Output is correct |
3 |
Correct |
43 ms |
9460 KB |
Output is correct |
4 |
Correct |
12 ms |
4224 KB |
Output is correct |
5 |
Correct |
11 ms |
3584 KB |
Output is correct |
6 |
Correct |
18 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
34 ms |
6520 KB |
Output is correct |
9 |
Correct |
39 ms |
7928 KB |
Output is correct |
10 |
Correct |
4 ms |
2816 KB |
Output is correct |
11 |
Correct |
55 ms |
9464 KB |
Output is correct |
12 |
Correct |
78 ms |
11120 KB |
Output is correct |
13 |
Correct |
4 ms |
2816 KB |
Output is correct |
14 |
Correct |
4 ms |
2688 KB |
Output is correct |
15 |
Correct |
5 ms |
2816 KB |
Output is correct |
16 |
Correct |
6 ms |
2944 KB |
Output is correct |
17 |
Incorrect |
5 ms |
2816 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
13044 KB |
Output is correct |
2 |
Correct |
72 ms |
12916 KB |
Output is correct |
3 |
Correct |
43 ms |
9460 KB |
Output is correct |
4 |
Correct |
12 ms |
4224 KB |
Output is correct |
5 |
Correct |
11 ms |
3584 KB |
Output is correct |
6 |
Correct |
18 ms |
5120 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
34 ms |
6520 KB |
Output is correct |
9 |
Correct |
39 ms |
7928 KB |
Output is correct |
10 |
Correct |
4 ms |
2816 KB |
Output is correct |
11 |
Correct |
55 ms |
9464 KB |
Output is correct |
12 |
Correct |
78 ms |
11120 KB |
Output is correct |
13 |
Correct |
4 ms |
2816 KB |
Output is correct |
14 |
Correct |
4 ms |
2688 KB |
Output is correct |
15 |
Correct |
4 ms |
2688 KB |
Output is correct |
16 |
Correct |
4 ms |
2688 KB |
Output is correct |
17 |
Correct |
4 ms |
2816 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
4 ms |
2688 KB |
Output is correct |
20 |
Correct |
4 ms |
2688 KB |
Output is correct |
21 |
Correct |
4 ms |
2688 KB |
Output is correct |
22 |
Correct |
4 ms |
2688 KB |
Output is correct |
23 |
Correct |
4 ms |
2688 KB |
Output is correct |
24 |
Correct |
4 ms |
2688 KB |
Output is correct |
25 |
Correct |
3 ms |
2688 KB |
Output is correct |
26 |
Correct |
4 ms |
2688 KB |
Output is correct |
27 |
Correct |
5 ms |
2688 KB |
Output is correct |
28 |
Correct |
4 ms |
2660 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
4 ms |
2688 KB |
Output is correct |
33 |
Correct |
4 ms |
2688 KB |
Output is correct |
34 |
Correct |
3 ms |
2688 KB |
Output is correct |
35 |
Correct |
4 ms |
2688 KB |
Output is correct |
36 |
Correct |
4 ms |
2688 KB |
Output is correct |
37 |
Correct |
4 ms |
2688 KB |
Output is correct |
38 |
Correct |
4 ms |
2688 KB |
Output is correct |
39 |
Correct |
71 ms |
12872 KB |
Output is correct |
40 |
Correct |
69 ms |
12912 KB |
Output is correct |
41 |
Correct |
47 ms |
9464 KB |
Output is correct |
42 |
Correct |
11 ms |
4224 KB |
Output is correct |
43 |
Correct |
3 ms |
2688 KB |
Output is correct |
44 |
Correct |
4 ms |
2688 KB |
Output is correct |
45 |
Correct |
4 ms |
2688 KB |
Output is correct |
46 |
Correct |
4 ms |
2688 KB |
Output is correct |
47 |
Correct |
4 ms |
2688 KB |
Output is correct |
48 |
Correct |
4 ms |
2688 KB |
Output is correct |
49 |
Correct |
4 ms |
2688 KB |
Output is correct |
50 |
Correct |
4 ms |
2688 KB |
Output is correct |
51 |
Correct |
4 ms |
2688 KB |
Output is correct |
52 |
Correct |
4 ms |
2688 KB |
Output is correct |
53 |
Correct |
4 ms |
2688 KB |
Output is correct |
54 |
Correct |
4 ms |
2688 KB |
Output is correct |
55 |
Correct |
4 ms |
2688 KB |
Output is correct |
56 |
Correct |
4 ms |
2688 KB |
Output is correct |
57 |
Correct |
113 ms |
9968 KB |
Output is correct |
58 |
Correct |
92 ms |
9844 KB |
Output is correct |
59 |
Correct |
86 ms |
9840 KB |
Output is correct |
60 |
Correct |
94 ms |
9716 KB |
Output is correct |
61 |
Correct |
91 ms |
9936 KB |
Output is correct |
62 |
Correct |
79 ms |
9664 KB |
Output is correct |
63 |
Correct |
86 ms |
9716 KB |
Output is correct |
64 |
Incorrect |
58 ms |
9080 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |