#include <cstdio>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <fstream>
#include <cmath>
#include <queue>
#include <stack>
#include <cassert>
#include <cstring>
#include <climits>
#include <functional>
#include<cstdlib>
//#include "arc.h"
//#include "dreaming.h"
using namespace std;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long LL;
const int MAXN = 2e5 + 11;
using ll = long long;
const ll mod1 = 1e9 + 7;
const ll mod2 = 1000000021;
const ll P = 31;
/*
void setIO(string name = "") {
cin.tie(0)->sync_with_stdio(0); // see /general/fast-io
if ((name.size())) {
freopen((name + ".in").c_str(), "r", stdin); // see /general/input-output
freopen((name + ".out").c_str(), "w", stdout);
}
}
*/
vector<pair<int,ll>> adj[MAXN];
ll n, k;
int sz[MAXN];
bool vis[MAXN];
int mp[1000100];
vector<ll> to_del;
map<ll, bool> used;
int ans = 1e9;
void dfs(int v, int p,bool fill,ll sum,int len)
{
if (sum > k)
return;
if (fill)
{
to_del.push_back(sum);
if (used[sum] == 0)
mp[sum] = len, used[sum] = true;
else
mp[sum] = min(mp[sum], len);
}
else
{
ll need = k - sum;
//cout << v << " " << sum << " " << len <<
// " and " << need << " " << mp[need] << '\n';
if (need == 0 || mp[need] != 0)
ans = min(ans, mp[need] + len);
}
for (auto u : adj[v])
{
if (u.first == p || vis[u.first])
continue;
dfs(u.first, v, fill, sum + u.second, len + 1);
}
}
int find_size(int v, int p)
{
if (vis[v])
return 0;
sz[v] = 1;
for (auto u : adj[v])
{
if (u.first == p)
continue;
sz[v] += find_size(u.first, v);
}
return sz[v];
}
int find_centroid(int v, int p,int n)
{
for (auto u : adj[v])
{
if (u.first == p || vis[u.first])
continue;
if (sz[u.first] > n / 2)
return find_centroid(u.first, v, n);
}
return v;
}
void decomp(int v, int p)
{
find_size(v, -1);
int c = find_centroid(v, -1, sz[v]);
vis[c] = true;
to_del.clear();
for (auto u : adj[c])
{
if (vis[u.first])
continue;
dfs(u.first, -1, 0, u.second, 1);
dfs(u.first, -1, 1, u.second, 1);
}
for (auto vals : to_del)
{
mp[vals] = 0;
used[vals] = false;
}
for (auto u : adj[c])
{
if (vis[u.first])
continue;
decomp(u.first, c);
}
}
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 v = h[i][0]+1;
int u = h[i][1]+1;
int cost = l[i];
adj[u].push_back({ v,cost });
adj[v].push_back({ u,cost });
}
decomp(1, -1);
if (ans == 1e9)
ans = -1;
return ans;
}
/*
int main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
//setIO("movie");
cin >> n >> k;
for(int i =1;i<n;i++)
{
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back({ b,c });
adj[b].push_back({ a,c });
}
decomp(1, -1);
if (ans == 1e9)
cout << -1 << '\n';
else
cout << ans << '\n';
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9560 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9560 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9596 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9816 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9560 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9560 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9596 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9816 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
3 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
4 ms |
12124 KB |
Output is correct |
23 |
Correct |
3 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
25 |
Correct |
6 ms |
11220 KB |
Output is correct |
26 |
Correct |
3 ms |
11612 KB |
Output is correct |
27 |
Correct |
2 ms |
9560 KB |
Output is correct |
28 |
Correct |
4 ms |
10076 KB |
Output is correct |
29 |
Correct |
4 ms |
10588 KB |
Output is correct |
30 |
Correct |
4 ms |
10840 KB |
Output is correct |
31 |
Correct |
4 ms |
11100 KB |
Output is correct |
32 |
Correct |
5 ms |
11352 KB |
Output is correct |
33 |
Correct |
5 ms |
12124 KB |
Output is correct |
34 |
Correct |
4 ms |
11612 KB |
Output is correct |
35 |
Correct |
5 ms |
12380 KB |
Output is correct |
36 |
Correct |
5 ms |
12544 KB |
Output is correct |
37 |
Correct |
5 ms |
11356 KB |
Output is correct |
38 |
Correct |
5 ms |
10536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9560 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9560 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9596 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9816 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
127 ms |
17848 KB |
Output is correct |
20 |
Correct |
123 ms |
17488 KB |
Output is correct |
21 |
Correct |
137 ms |
17796 KB |
Output is correct |
22 |
Correct |
127 ms |
18308 KB |
Output is correct |
23 |
Correct |
70 ms |
17916 KB |
Output is correct |
24 |
Correct |
53 ms |
16784 KB |
Output is correct |
25 |
Correct |
94 ms |
20308 KB |
Output is correct |
26 |
Correct |
89 ms |
24276 KB |
Output is correct |
27 |
Correct |
136 ms |
23284 KB |
Output is correct |
28 |
Correct |
189 ms |
34280 KB |
Output is correct |
29 |
Correct |
169 ms |
33276 KB |
Output is correct |
30 |
Correct |
140 ms |
23288 KB |
Output is correct |
31 |
Correct |
148 ms |
23384 KB |
Output is correct |
32 |
Correct |
157 ms |
23300 KB |
Output is correct |
33 |
Correct |
155 ms |
21840 KB |
Output is correct |
34 |
Correct |
107 ms |
21932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9560 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9560 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9596 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9816 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
3 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
3 ms |
9564 KB |
Output is correct |
22 |
Correct |
4 ms |
12124 KB |
Output is correct |
23 |
Correct |
3 ms |
11356 KB |
Output is correct |
24 |
Correct |
4 ms |
12124 KB |
Output is correct |
25 |
Correct |
6 ms |
11220 KB |
Output is correct |
26 |
Correct |
3 ms |
11612 KB |
Output is correct |
27 |
Correct |
2 ms |
9560 KB |
Output is correct |
28 |
Correct |
4 ms |
10076 KB |
Output is correct |
29 |
Correct |
4 ms |
10588 KB |
Output is correct |
30 |
Correct |
4 ms |
10840 KB |
Output is correct |
31 |
Correct |
4 ms |
11100 KB |
Output is correct |
32 |
Correct |
5 ms |
11352 KB |
Output is correct |
33 |
Correct |
5 ms |
12124 KB |
Output is correct |
34 |
Correct |
4 ms |
11612 KB |
Output is correct |
35 |
Correct |
5 ms |
12380 KB |
Output is correct |
36 |
Correct |
5 ms |
12544 KB |
Output is correct |
37 |
Correct |
5 ms |
11356 KB |
Output is correct |
38 |
Correct |
5 ms |
10536 KB |
Output is correct |
39 |
Correct |
127 ms |
17848 KB |
Output is correct |
40 |
Correct |
123 ms |
17488 KB |
Output is correct |
41 |
Correct |
137 ms |
17796 KB |
Output is correct |
42 |
Correct |
127 ms |
18308 KB |
Output is correct |
43 |
Correct |
70 ms |
17916 KB |
Output is correct |
44 |
Correct |
53 ms |
16784 KB |
Output is correct |
45 |
Correct |
94 ms |
20308 KB |
Output is correct |
46 |
Correct |
89 ms |
24276 KB |
Output is correct |
47 |
Correct |
136 ms |
23284 KB |
Output is correct |
48 |
Correct |
189 ms |
34280 KB |
Output is correct |
49 |
Correct |
169 ms |
33276 KB |
Output is correct |
50 |
Correct |
140 ms |
23288 KB |
Output is correct |
51 |
Correct |
148 ms |
23384 KB |
Output is correct |
52 |
Correct |
157 ms |
23300 KB |
Output is correct |
53 |
Correct |
155 ms |
21840 KB |
Output is correct |
54 |
Correct |
107 ms |
21932 KB |
Output is correct |
55 |
Correct |
19 ms |
10328 KB |
Output is correct |
56 |
Correct |
12 ms |
10332 KB |
Output is correct |
57 |
Correct |
107 ms |
18824 KB |
Output is correct |
58 |
Correct |
35 ms |
18636 KB |
Output is correct |
59 |
Correct |
304 ms |
29296 KB |
Output is correct |
60 |
Correct |
949 ms |
58084 KB |
Output is correct |
61 |
Correct |
253 ms |
23760 KB |
Output is correct |
62 |
Correct |
249 ms |
25212 KB |
Output is correct |
63 |
Correct |
298 ms |
25136 KB |
Output is correct |
64 |
Correct |
1373 ms |
38000 KB |
Output is correct |
65 |
Correct |
124 ms |
26452 KB |
Output is correct |
66 |
Correct |
842 ms |
46492 KB |
Output is correct |
67 |
Correct |
97 ms |
28704 KB |
Output is correct |
68 |
Correct |
501 ms |
42436 KB |
Output is correct |
69 |
Correct |
529 ms |
42312 KB |
Output is correct |
70 |
Correct |
490 ms |
42468 KB |
Output is correct |