#include <bits/stdc++.h>
using namespace std;
#define wait() system("pause")
#define clock_starts() clock_t begin = clock()
#define clock_ends() clock_t end = clock()
#define print_running_time() double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC; \
printf("Elapsed Time : %.10f second(s)\n", elapsed_secs)
#define read(s) freopen(s, "r", stdin)
#define write(s) freopen(s, "w", stdout)
#define debug(s) cout<< #s <<" = "<< s <<endl
#define int long long
#define inf 1000000000000000000LL
const int maxn = 200010;
bool onCtree[maxn]; int n, k;
vector<pair<int, int>> v[maxn];
vector<int> ctree[maxn], subtree[maxn];
int level[maxn], T[19][maxn], sub[maxn], D[maxn], parent[maxn];
map<int, int> mp; int ret = inf;
vector<int> eulerTour; int lg[maxn*2], where[maxn], table[19][maxn*2];
void preCalc(int p, int node, int l, int d) {
level[node] = l; eulerTour.push_back(node);
D[node] = d, T[0][node] = parent[node] = p;
for(auto b : v[node]) {
if(b.first != p) {
preCalc(node, b.first, l+1, d+b.second);
eulerTour.push_back(node);
}
}
}
int MIN(int a, int b) { // I can easily believe that where O(1)
// lca needed there is a level[] array :/
return ( level[a] < level[b] ? a : b ) ;
}
void makeTable() {
int N = eulerTour.size()-1;
for(int i=0; i<maxn; i++) where[i] = inf;
for(int i=0; i<=N; i++) where[eulerTour[i]] = min(where[eulerTour[i]], i);
for(int i=0; i<=N; i++) table[0][i] = eulerTour[i];
lg[1] = 0; for(int i=2; i<=N; i++) lg[i] = lg[i >> 1] + 1;
for(int p=1; p<20; p++) {
for(int i=0; i+(1<<p)-1<=N; i++) {
table[p][i] = MIN(table[p-1][i], table[p-1][i+(1<<(p-1))]);
}
}
}
int getMin(int l, int r) {
int k = lg[r-l+1];
return MIN(table[k][l], table[k][r-(1<<k)+1]);
}
int __lca(int a, int b) {
if(where[a] > where[b]) swap(a, b);
return getMin(where[a], where[b]);
}
int cost(int x, int y) {
return D[x] + D[y] - 2 * D[__lca(x, y)];
}
int dist(int x, int y) {
return level[x] + level[y] - 2 * level[__lca(x, y)];
}
void dfs(int p, int node) {
sub[node] = 1;
for(auto b : v[node]) {
if(!onCtree[b.first] and p != b.first) {
dfs(node, b.first);
sub[node] += sub[b.first];
}
}
}
int decompose(int p, int node, int sz) {
for(auto b : v[node]) {
if(!onCtree[b.first] and b.first != p and sub[b.first] > sz/2) {
return decompose(node, b.first, sz);
}
}
onCtree[node] = true;
for(auto b : v[node]) {
if(!onCtree[b.first]) {
dfs(node, b.first);
ctree[node].push_back(decompose(node, b.first, sub[b.first]));
}
} return node;
}
void add(int node) {
subtree[node].push_back(node);
for(auto b : ctree[node]) {
add(b);
for(auto x : subtree[b]) {
subtree[node].push_back(x);
}
}
}
void solve() {
preCalc(-1,1,0,0);
//process();
makeTable();
dfs(-1,1);
int root = decompose(-1,1, sub[1]);
add(root);
for(int i=1; i<=n; i++) ctree[i].reserve(ctree[i].size());
for(int i=1; i<=n; i++) subtree[i].reserve(subtree[i].size());
for(int i=1; i<=n; i++) {
for(auto b : ctree[i]) {
for(auto node : subtree[b]) {
//int d = cost(i, node);
int lca = __lca(i, node);
int d = D[i] + D[node] - 2 * D[lca];
if(mp.find(k-d) != mp.end()) {
//ret = min(ret, dist(i, node)+mp[k-d]);
ret = min(ret, level[i] + level[node] - 2 * level[lca] + mp[k-d]);
}
}
for(auto node : subtree[b]) {
//int d = cost(i, node);
int lca = __lca(i, node);
int d = D[i] + D[node] - 2 * D[lca];
if(mp.find(d) != mp.end()) {
mp[d] = min(mp[d], level[i] + level[node] - 2 * level[lca]);
} else mp[d] = level[i] + level[node] - 2 * level[lca];
}
}
if(mp.find(k) != mp.end()) {
ret = min( ret , mp[k] );
}
mp.clear();
}
}
int32_t best_path(int32_t N, int32_t K, int32_t H[][2], int32_t L[]) {
n = N, k = K;
for(int i=0; i<n-1; i++) {
int x = H[i][0]+1, y = H[i][1]+1, w = L[i];
v[x].push_back({y, w});
v[y].push_back({x, w});
}
if(k==0) return 0;
solve();
if(ret == inf) return -1;
return (int32_t)ret;
}
/*
int32_t main () {
cin>>n>>k;
for(int i=1; i<n; i++) {
int x, y, w; scanf("%lld%lld%lld", &x, &y, &w);
x++, y++;
v[x].push_back({y, w});
v[y].push_back({x, w});
}
if(k==0) {
puts("0");
return 0;
}
solve();
if(ret == inf) {
puts("-1");
} else {
printf("%lld\n", ret);
}
//wait();
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16120 KB |
Output is correct |
2 |
Correct |
17 ms |
16272 KB |
Output is correct |
3 |
Correct |
15 ms |
16360 KB |
Output is correct |
4 |
Correct |
14 ms |
16360 KB |
Output is correct |
5 |
Correct |
14 ms |
16360 KB |
Output is correct |
6 |
Correct |
15 ms |
16416 KB |
Output is correct |
7 |
Correct |
14 ms |
16416 KB |
Output is correct |
8 |
Correct |
14 ms |
16416 KB |
Output is correct |
9 |
Correct |
15 ms |
16420 KB |
Output is correct |
10 |
Correct |
15 ms |
16420 KB |
Output is correct |
11 |
Correct |
14 ms |
16420 KB |
Output is correct |
12 |
Correct |
14 ms |
16420 KB |
Output is correct |
13 |
Correct |
14 ms |
16420 KB |
Output is correct |
14 |
Correct |
14 ms |
16472 KB |
Output is correct |
15 |
Correct |
15 ms |
16472 KB |
Output is correct |
16 |
Correct |
18 ms |
16472 KB |
Output is correct |
17 |
Correct |
14 ms |
16472 KB |
Output is correct |
18 |
Correct |
14 ms |
16472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16120 KB |
Output is correct |
2 |
Correct |
17 ms |
16272 KB |
Output is correct |
3 |
Correct |
15 ms |
16360 KB |
Output is correct |
4 |
Correct |
14 ms |
16360 KB |
Output is correct |
5 |
Correct |
14 ms |
16360 KB |
Output is correct |
6 |
Correct |
15 ms |
16416 KB |
Output is correct |
7 |
Correct |
14 ms |
16416 KB |
Output is correct |
8 |
Correct |
14 ms |
16416 KB |
Output is correct |
9 |
Correct |
15 ms |
16420 KB |
Output is correct |
10 |
Correct |
15 ms |
16420 KB |
Output is correct |
11 |
Correct |
14 ms |
16420 KB |
Output is correct |
12 |
Correct |
14 ms |
16420 KB |
Output is correct |
13 |
Correct |
14 ms |
16420 KB |
Output is correct |
14 |
Correct |
14 ms |
16472 KB |
Output is correct |
15 |
Correct |
15 ms |
16472 KB |
Output is correct |
16 |
Correct |
18 ms |
16472 KB |
Output is correct |
17 |
Correct |
14 ms |
16472 KB |
Output is correct |
18 |
Correct |
14 ms |
16472 KB |
Output is correct |
19 |
Correct |
14 ms |
16472 KB |
Output is correct |
20 |
Correct |
15 ms |
16472 KB |
Output is correct |
21 |
Correct |
17 ms |
16748 KB |
Output is correct |
22 |
Correct |
17 ms |
16768 KB |
Output is correct |
23 |
Correct |
17 ms |
16836 KB |
Output is correct |
24 |
Correct |
17 ms |
16836 KB |
Output is correct |
25 |
Correct |
16 ms |
16836 KB |
Output is correct |
26 |
Correct |
16 ms |
16836 KB |
Output is correct |
27 |
Correct |
16 ms |
16836 KB |
Output is correct |
28 |
Correct |
17 ms |
16856 KB |
Output is correct |
29 |
Correct |
17 ms |
16860 KB |
Output is correct |
30 |
Correct |
17 ms |
16860 KB |
Output is correct |
31 |
Correct |
16 ms |
16860 KB |
Output is correct |
32 |
Correct |
17 ms |
16860 KB |
Output is correct |
33 |
Correct |
16 ms |
16860 KB |
Output is correct |
34 |
Correct |
17 ms |
16860 KB |
Output is correct |
35 |
Correct |
18 ms |
16860 KB |
Output is correct |
36 |
Correct |
18 ms |
16860 KB |
Output is correct |
37 |
Correct |
17 ms |
16860 KB |
Output is correct |
38 |
Correct |
17 ms |
16860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16120 KB |
Output is correct |
2 |
Correct |
17 ms |
16272 KB |
Output is correct |
3 |
Correct |
15 ms |
16360 KB |
Output is correct |
4 |
Correct |
14 ms |
16360 KB |
Output is correct |
5 |
Correct |
14 ms |
16360 KB |
Output is correct |
6 |
Correct |
15 ms |
16416 KB |
Output is correct |
7 |
Correct |
14 ms |
16416 KB |
Output is correct |
8 |
Correct |
14 ms |
16416 KB |
Output is correct |
9 |
Correct |
15 ms |
16420 KB |
Output is correct |
10 |
Correct |
15 ms |
16420 KB |
Output is correct |
11 |
Correct |
14 ms |
16420 KB |
Output is correct |
12 |
Correct |
14 ms |
16420 KB |
Output is correct |
13 |
Correct |
14 ms |
16420 KB |
Output is correct |
14 |
Correct |
14 ms |
16472 KB |
Output is correct |
15 |
Correct |
15 ms |
16472 KB |
Output is correct |
16 |
Correct |
18 ms |
16472 KB |
Output is correct |
17 |
Correct |
14 ms |
16472 KB |
Output is correct |
18 |
Correct |
14 ms |
16472 KB |
Output is correct |
19 |
Correct |
556 ms |
70056 KB |
Output is correct |
20 |
Correct |
558 ms |
70700 KB |
Output is correct |
21 |
Correct |
575 ms |
70700 KB |
Output is correct |
22 |
Correct |
455 ms |
70700 KB |
Output is correct |
23 |
Correct |
621 ms |
72060 KB |
Output is correct |
24 |
Correct |
329 ms |
72060 KB |
Output is correct |
25 |
Correct |
644 ms |
78900 KB |
Output is correct |
26 |
Correct |
219 ms |
81848 KB |
Output is correct |
27 |
Correct |
577 ms |
118456 KB |
Output is correct |
28 |
Correct |
2005 ms |
162272 KB |
Output is correct |
29 |
Correct |
2176 ms |
162272 KB |
Output is correct |
30 |
Correct |
557 ms |
162272 KB |
Output is correct |
31 |
Correct |
594 ms |
162272 KB |
Output is correct |
32 |
Correct |
834 ms |
162272 KB |
Output is correct |
33 |
Correct |
1432 ms |
162272 KB |
Output is correct |
34 |
Correct |
2033 ms |
162272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16120 KB |
Output is correct |
2 |
Correct |
17 ms |
16272 KB |
Output is correct |
3 |
Correct |
15 ms |
16360 KB |
Output is correct |
4 |
Correct |
14 ms |
16360 KB |
Output is correct |
5 |
Correct |
14 ms |
16360 KB |
Output is correct |
6 |
Correct |
15 ms |
16416 KB |
Output is correct |
7 |
Correct |
14 ms |
16416 KB |
Output is correct |
8 |
Correct |
14 ms |
16416 KB |
Output is correct |
9 |
Correct |
15 ms |
16420 KB |
Output is correct |
10 |
Correct |
15 ms |
16420 KB |
Output is correct |
11 |
Correct |
14 ms |
16420 KB |
Output is correct |
12 |
Correct |
14 ms |
16420 KB |
Output is correct |
13 |
Correct |
14 ms |
16420 KB |
Output is correct |
14 |
Correct |
14 ms |
16472 KB |
Output is correct |
15 |
Correct |
15 ms |
16472 KB |
Output is correct |
16 |
Correct |
18 ms |
16472 KB |
Output is correct |
17 |
Correct |
14 ms |
16472 KB |
Output is correct |
18 |
Correct |
14 ms |
16472 KB |
Output is correct |
19 |
Correct |
14 ms |
16472 KB |
Output is correct |
20 |
Correct |
15 ms |
16472 KB |
Output is correct |
21 |
Correct |
17 ms |
16748 KB |
Output is correct |
22 |
Correct |
17 ms |
16768 KB |
Output is correct |
23 |
Correct |
17 ms |
16836 KB |
Output is correct |
24 |
Correct |
17 ms |
16836 KB |
Output is correct |
25 |
Correct |
16 ms |
16836 KB |
Output is correct |
26 |
Correct |
16 ms |
16836 KB |
Output is correct |
27 |
Correct |
16 ms |
16836 KB |
Output is correct |
28 |
Correct |
17 ms |
16856 KB |
Output is correct |
29 |
Correct |
17 ms |
16860 KB |
Output is correct |
30 |
Correct |
17 ms |
16860 KB |
Output is correct |
31 |
Correct |
16 ms |
16860 KB |
Output is correct |
32 |
Correct |
17 ms |
16860 KB |
Output is correct |
33 |
Correct |
16 ms |
16860 KB |
Output is correct |
34 |
Correct |
17 ms |
16860 KB |
Output is correct |
35 |
Correct |
18 ms |
16860 KB |
Output is correct |
36 |
Correct |
18 ms |
16860 KB |
Output is correct |
37 |
Correct |
17 ms |
16860 KB |
Output is correct |
38 |
Correct |
17 ms |
16860 KB |
Output is correct |
39 |
Correct |
556 ms |
70056 KB |
Output is correct |
40 |
Correct |
558 ms |
70700 KB |
Output is correct |
41 |
Correct |
575 ms |
70700 KB |
Output is correct |
42 |
Correct |
455 ms |
70700 KB |
Output is correct |
43 |
Correct |
621 ms |
72060 KB |
Output is correct |
44 |
Correct |
329 ms |
72060 KB |
Output is correct |
45 |
Correct |
644 ms |
78900 KB |
Output is correct |
46 |
Correct |
219 ms |
81848 KB |
Output is correct |
47 |
Correct |
577 ms |
118456 KB |
Output is correct |
48 |
Correct |
2005 ms |
162272 KB |
Output is correct |
49 |
Correct |
2176 ms |
162272 KB |
Output is correct |
50 |
Correct |
557 ms |
162272 KB |
Output is correct |
51 |
Correct |
594 ms |
162272 KB |
Output is correct |
52 |
Correct |
834 ms |
162272 KB |
Output is correct |
53 |
Correct |
1432 ms |
162272 KB |
Output is correct |
54 |
Correct |
2033 ms |
162272 KB |
Output is correct |
55 |
Correct |
52 ms |
162272 KB |
Output is correct |
56 |
Correct |
41 ms |
162272 KB |
Output is correct |
57 |
Correct |
261 ms |
162272 KB |
Output is correct |
58 |
Correct |
114 ms |
162272 KB |
Output is correct |
59 |
Correct |
487 ms |
162272 KB |
Output is correct |
60 |
Correct |
1921 ms |
162272 KB |
Output is correct |
61 |
Correct |
659 ms |
162272 KB |
Output is correct |
62 |
Correct |
573 ms |
162272 KB |
Output is correct |
63 |
Correct |
889 ms |
162272 KB |
Output is correct |
64 |
Correct |
1684 ms |
162272 KB |
Output is correct |
65 |
Correct |
1523 ms |
162272 KB |
Output is correct |
66 |
Correct |
1842 ms |
162272 KB |
Output is correct |
67 |
Correct |
410 ms |
162272 KB |
Output is correct |
68 |
Correct |
953 ms |
162272 KB |
Output is correct |
69 |
Correct |
1030 ms |
162272 KB |
Output is correct |
70 |
Correct |
925 ms |
162272 KB |
Output is correct |