#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <map>
using namespace std;
typedef long long ll;
const int maxn = 200005;
const int INF = 999999999;
int n, k;
int ans = 0;
struct Edge {
int other, weight;
};
vector<Edge> adj[maxn];
struct Node
{
map<int,int>* mp;
int lAux, pathAux;
int curr;
inline int sz() {return mp->size();}
Node() {
mp = new map<int,int>;
pathAux = 0;
lAux = 0;
curr = INF;
}
void addSelf() {
(*mp)[-lAux] = -pathAux;
}
} nodes[maxn];
void merge(Node& a, Node& b, int weight) {
b.lAux += weight;
bool swapped = false;
if (a.sz() < b.sz()) {
swap(a,b);
swapped = true;
}
//check for paths
if (!swapped) {
if (a.mp->count(k-weight-a.lAux)) {
int n2 = (*a.mp)[k-weight-a.lAux] + a.pathAux;
ans = min(ans,1+n2);
}
}
for (auto p: (*b.mp)) {
int len = p.first + b.lAux;
int num = p.second + b.pathAux;
if (a.mp->count(k-len-a.lAux)) {
int n2 = (*a.mp)[k-len-a.lAux] + a.pathAux;
ans = min(ans,num+n2);
}
}
//now add the paths
for (auto p: (*b.mp)) {
int len = p.first + b.lAux;
int num = p.second + b.pathAux;
if (!a.mp->count(len-a.lAux)) {
(*a.mp)[len-a.lAux] = num-a.pathAux;
}
else if ((*a.mp)[len-a.lAux] > num-a.pathAux) {
(*a.mp)[len-a.lAux] = num-a.pathAux;
}
}
//add the path from a to b itself
if (!swapped) {
if (!a.mp->count(weight-a.lAux)) {
(*a.mp)[weight-a.lAux] = 1 - a.pathAux;
}
else if ((*a.mp)[weight-a.lAux] > weight-a.pathAux) {
(*a.mp)[weight-a.lAux] = 1 - a.pathAux;
}
}
delete b.mp;
}
void dfs(int i, int par) {
nodes[i] = Node();
nodes[i].addSelf();
for (Edge e: adj[i]) {
if (e.other == par) continue;
dfs(e.other,i);
merge(nodes[i],nodes[e.other],e.weight);
}
nodes[i].pathAux++;
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
for (int i = 0; i < N - 1; i++) {
int h1 = H[i][0], h2 = H[i][1], li = L[i];
adj[h1].push_back({h2,li});
adj[h2].push_back({h1,li});
}
ans = INF;
dfs(0,-1);
return (ans == INF ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22264 KB |
Output is correct |
2 |
Correct |
25 ms |
22264 KB |
Output is correct |
3 |
Correct |
25 ms |
22264 KB |
Output is correct |
4 |
Correct |
25 ms |
22264 KB |
Output is correct |
5 |
Correct |
25 ms |
22264 KB |
Output is correct |
6 |
Correct |
25 ms |
22264 KB |
Output is correct |
7 |
Correct |
25 ms |
22264 KB |
Output is correct |
8 |
Correct |
24 ms |
22264 KB |
Output is correct |
9 |
Correct |
25 ms |
22268 KB |
Output is correct |
10 |
Correct |
25 ms |
22264 KB |
Output is correct |
11 |
Correct |
25 ms |
22264 KB |
Output is correct |
12 |
Correct |
25 ms |
22264 KB |
Output is correct |
13 |
Correct |
25 ms |
22264 KB |
Output is correct |
14 |
Correct |
25 ms |
22392 KB |
Output is correct |
15 |
Correct |
25 ms |
22264 KB |
Output is correct |
16 |
Correct |
24 ms |
22264 KB |
Output is correct |
17 |
Correct |
27 ms |
22264 KB |
Output is correct |
18 |
Correct |
25 ms |
22264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22264 KB |
Output is correct |
2 |
Correct |
25 ms |
22264 KB |
Output is correct |
3 |
Correct |
25 ms |
22264 KB |
Output is correct |
4 |
Correct |
25 ms |
22264 KB |
Output is correct |
5 |
Correct |
25 ms |
22264 KB |
Output is correct |
6 |
Correct |
25 ms |
22264 KB |
Output is correct |
7 |
Correct |
25 ms |
22264 KB |
Output is correct |
8 |
Correct |
24 ms |
22264 KB |
Output is correct |
9 |
Correct |
25 ms |
22268 KB |
Output is correct |
10 |
Correct |
25 ms |
22264 KB |
Output is correct |
11 |
Correct |
25 ms |
22264 KB |
Output is correct |
12 |
Correct |
25 ms |
22264 KB |
Output is correct |
13 |
Correct |
25 ms |
22264 KB |
Output is correct |
14 |
Correct |
25 ms |
22392 KB |
Output is correct |
15 |
Correct |
25 ms |
22264 KB |
Output is correct |
16 |
Correct |
24 ms |
22264 KB |
Output is correct |
17 |
Correct |
27 ms |
22264 KB |
Output is correct |
18 |
Correct |
25 ms |
22264 KB |
Output is correct |
19 |
Correct |
25 ms |
22264 KB |
Output is correct |
20 |
Correct |
25 ms |
22264 KB |
Output is correct |
21 |
Correct |
26 ms |
22268 KB |
Output is correct |
22 |
Correct |
26 ms |
22392 KB |
Output is correct |
23 |
Correct |
27 ms |
22392 KB |
Output is correct |
24 |
Correct |
27 ms |
22520 KB |
Output is correct |
25 |
Correct |
27 ms |
22392 KB |
Output is correct |
26 |
Correct |
26 ms |
22392 KB |
Output is correct |
27 |
Correct |
25 ms |
22264 KB |
Output is correct |
28 |
Correct |
26 ms |
22392 KB |
Output is correct |
29 |
Correct |
26 ms |
22392 KB |
Output is correct |
30 |
Correct |
26 ms |
22392 KB |
Output is correct |
31 |
Correct |
26 ms |
22392 KB |
Output is correct |
32 |
Correct |
26 ms |
22392 KB |
Output is correct |
33 |
Correct |
27 ms |
22392 KB |
Output is correct |
34 |
Correct |
26 ms |
22392 KB |
Output is correct |
35 |
Correct |
26 ms |
22392 KB |
Output is correct |
36 |
Correct |
26 ms |
22392 KB |
Output is correct |
37 |
Correct |
26 ms |
22392 KB |
Output is correct |
38 |
Correct |
27 ms |
22392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22264 KB |
Output is correct |
2 |
Correct |
25 ms |
22264 KB |
Output is correct |
3 |
Correct |
25 ms |
22264 KB |
Output is correct |
4 |
Correct |
25 ms |
22264 KB |
Output is correct |
5 |
Correct |
25 ms |
22264 KB |
Output is correct |
6 |
Correct |
25 ms |
22264 KB |
Output is correct |
7 |
Correct |
25 ms |
22264 KB |
Output is correct |
8 |
Correct |
24 ms |
22264 KB |
Output is correct |
9 |
Correct |
25 ms |
22268 KB |
Output is correct |
10 |
Correct |
25 ms |
22264 KB |
Output is correct |
11 |
Correct |
25 ms |
22264 KB |
Output is correct |
12 |
Correct |
25 ms |
22264 KB |
Output is correct |
13 |
Correct |
25 ms |
22264 KB |
Output is correct |
14 |
Correct |
25 ms |
22392 KB |
Output is correct |
15 |
Correct |
25 ms |
22264 KB |
Output is correct |
16 |
Correct |
24 ms |
22264 KB |
Output is correct |
17 |
Correct |
27 ms |
22264 KB |
Output is correct |
18 |
Correct |
25 ms |
22264 KB |
Output is correct |
19 |
Correct |
161 ms |
28456 KB |
Output is correct |
20 |
Correct |
159 ms |
28536 KB |
Output is correct |
21 |
Correct |
157 ms |
28764 KB |
Output is correct |
22 |
Correct |
162 ms |
28536 KB |
Output is correct |
23 |
Correct |
198 ms |
28920 KB |
Output is correct |
24 |
Correct |
169 ms |
28936 KB |
Output is correct |
25 |
Correct |
149 ms |
39708 KB |
Output is correct |
26 |
Correct |
104 ms |
49784 KB |
Output is correct |
27 |
Correct |
252 ms |
35448 KB |
Output is correct |
28 |
Correct |
386 ms |
77804 KB |
Output is correct |
29 |
Correct |
386 ms |
75384 KB |
Output is correct |
30 |
Correct |
257 ms |
35448 KB |
Output is correct |
31 |
Correct |
262 ms |
35412 KB |
Output is correct |
32 |
Correct |
282 ms |
35488 KB |
Output is correct |
33 |
Correct |
501 ms |
34708 KB |
Output is correct |
34 |
Correct |
430 ms |
45276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
22264 KB |
Output is correct |
2 |
Correct |
25 ms |
22264 KB |
Output is correct |
3 |
Correct |
25 ms |
22264 KB |
Output is correct |
4 |
Correct |
25 ms |
22264 KB |
Output is correct |
5 |
Correct |
25 ms |
22264 KB |
Output is correct |
6 |
Correct |
25 ms |
22264 KB |
Output is correct |
7 |
Correct |
25 ms |
22264 KB |
Output is correct |
8 |
Correct |
24 ms |
22264 KB |
Output is correct |
9 |
Correct |
25 ms |
22268 KB |
Output is correct |
10 |
Correct |
25 ms |
22264 KB |
Output is correct |
11 |
Correct |
25 ms |
22264 KB |
Output is correct |
12 |
Correct |
25 ms |
22264 KB |
Output is correct |
13 |
Correct |
25 ms |
22264 KB |
Output is correct |
14 |
Correct |
25 ms |
22392 KB |
Output is correct |
15 |
Correct |
25 ms |
22264 KB |
Output is correct |
16 |
Correct |
24 ms |
22264 KB |
Output is correct |
17 |
Correct |
27 ms |
22264 KB |
Output is correct |
18 |
Correct |
25 ms |
22264 KB |
Output is correct |
19 |
Correct |
25 ms |
22264 KB |
Output is correct |
20 |
Correct |
25 ms |
22264 KB |
Output is correct |
21 |
Correct |
26 ms |
22268 KB |
Output is correct |
22 |
Correct |
26 ms |
22392 KB |
Output is correct |
23 |
Correct |
27 ms |
22392 KB |
Output is correct |
24 |
Correct |
27 ms |
22520 KB |
Output is correct |
25 |
Correct |
27 ms |
22392 KB |
Output is correct |
26 |
Correct |
26 ms |
22392 KB |
Output is correct |
27 |
Correct |
25 ms |
22264 KB |
Output is correct |
28 |
Correct |
26 ms |
22392 KB |
Output is correct |
29 |
Correct |
26 ms |
22392 KB |
Output is correct |
30 |
Correct |
26 ms |
22392 KB |
Output is correct |
31 |
Correct |
26 ms |
22392 KB |
Output is correct |
32 |
Correct |
26 ms |
22392 KB |
Output is correct |
33 |
Correct |
27 ms |
22392 KB |
Output is correct |
34 |
Correct |
26 ms |
22392 KB |
Output is correct |
35 |
Correct |
26 ms |
22392 KB |
Output is correct |
36 |
Correct |
26 ms |
22392 KB |
Output is correct |
37 |
Correct |
26 ms |
22392 KB |
Output is correct |
38 |
Correct |
27 ms |
22392 KB |
Output is correct |
39 |
Correct |
161 ms |
28456 KB |
Output is correct |
40 |
Correct |
159 ms |
28536 KB |
Output is correct |
41 |
Correct |
157 ms |
28764 KB |
Output is correct |
42 |
Correct |
162 ms |
28536 KB |
Output is correct |
43 |
Correct |
198 ms |
28920 KB |
Output is correct |
44 |
Correct |
169 ms |
28936 KB |
Output is correct |
45 |
Correct |
149 ms |
39708 KB |
Output is correct |
46 |
Correct |
104 ms |
49784 KB |
Output is correct |
47 |
Correct |
252 ms |
35448 KB |
Output is correct |
48 |
Correct |
386 ms |
77804 KB |
Output is correct |
49 |
Correct |
386 ms |
75384 KB |
Output is correct |
50 |
Correct |
257 ms |
35448 KB |
Output is correct |
51 |
Correct |
262 ms |
35412 KB |
Output is correct |
52 |
Correct |
282 ms |
35488 KB |
Output is correct |
53 |
Correct |
501 ms |
34708 KB |
Output is correct |
54 |
Correct |
430 ms |
45276 KB |
Output is correct |
55 |
Correct |
42 ms |
23288 KB |
Output is correct |
56 |
Correct |
38 ms |
22904 KB |
Output is correct |
57 |
Correct |
97 ms |
28664 KB |
Output is correct |
58 |
Correct |
87 ms |
28392 KB |
Output is correct |
59 |
Correct |
141 ms |
49964 KB |
Output is correct |
60 |
Correct |
368 ms |
76128 KB |
Output is correct |
61 |
Correct |
308 ms |
36848 KB |
Output is correct |
62 |
Correct |
241 ms |
35448 KB |
Output is correct |
63 |
Correct |
286 ms |
35548 KB |
Output is correct |
64 |
Correct |
531 ms |
41976 KB |
Output is correct |
65 |
Correct |
534 ms |
45412 KB |
Output is correct |
66 |
Correct |
450 ms |
70064 KB |
Output is correct |
67 |
Correct |
277 ms |
35812 KB |
Output is correct |
68 |
Correct |
507 ms |
43944 KB |
Output is correct |
69 |
Correct |
482 ms |
47492 KB |
Output is correct |
70 |
Correct |
431 ms |
43152 KB |
Output is correct |