#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
typedef long long ll;
typedef long double ld;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define Confundo ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define minheap priority_queue<int,vector<int>,greater<int>>
#define print2d(dp,n,m) for(int i=0;i<n;i++){for(int j=0;j<m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
#define ftt int t;cin>>t;for(int tt=1;tt<=t;++tt)
#define Sum(v) accumulate(v.begin(),v.end(),(ll)0)
#define Rev(v) reverse(v.begin(),v.end());
#define Sort(v) sort(v.begin(),v.end());
#define Input(v) for(auto &x:v) cin>>x;
#define Output(v) for(auto x:v) cout<<x<<" ";
#define mem(a, b) memset(a, b, sizeof(a))
#define dbgx(x) cout<<"\nhi "<<x<<"\n"
#define double long double
#define int long long
#define maxheap priority_queue<int>
#define dbg cout<<"\nhi\n"
#define sayy cout<<"YES\n"
#define sayn cout<<"NO\n"
#define pii pair<int,int>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define vii vector<pair<int,int>>
#define vvi vector<vector<int>>
#define vb vector<bool>
#define vi vector<int>
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define snd second
#define fst first
#define endl "\n"
const int INF = numeric_limits<int>::max() / 2;
const double PI = 3.1415926535898;
const int MOD = 1e9 + 7;
const int LIM = 2e5 + 5;
// O(log y)
int fpow(int x, int y) {
int temp;
if (y == 0)
return 1;
temp = fpow(x, y / 2);
if (y % 2 == 0)
return (temp * temp) % MOD;
else
return (x * ((temp * temp) % MOD)) % MOD;
}
// O(log max(a, b))
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
set<pii> adj[LIM];
vector<int> visited;
int k, ans, sz[LIM], arr[1000000 + 5];
int dfsForSize(int u, int p)
{
sz[u] = 1;
for (auto x : adj[u])
{
if (x.fst != p)
sz[u] += dfsForSize(x.fst, u);
}
return sz[u];
}
int findCentroid(int u, int p, int n)
{
for (auto x : adj[u])
{
if (x.fst != p)
{
if (sz[x.fst] > n / 2)
return findCentroid(x.fst, u, n);
}
}
return u;
}
void dfs1(int u, int p, int curLevel, int curLength)
{
if (curLength <= k && arr[k - curLength] < INF)
ans = min(ans, arr[k - curLength] + curLevel);
for (auto edge : adj[u])
{
if (edge.fst != p)
dfs1(edge.fst, u, curLevel + 1, curLength + edge.snd);
}
}
void dfs2(int u, int p, int curLevel, int curLength)
{
if (curLength <= k)
{
arr[curLength] = min(arr[curLength], curLevel);
visited.pb(curLength);
}
for (auto edge : adj[u])
{
if (edge.fst != p)
dfs2(edge.fst, u, curLevel + 1, curLength + edge.snd);
}
}
void centroidDecomposition(int u, int p)
{
int n = dfsForSize(u, p);
int centroid = findCentroid(u, p, n);
visited.clear();
for (auto edge : adj[centroid])
{
if (edge.fst != p)
{
dfs1(edge.fst, centroid, 1, edge.snd);
dfs2(edge.fst, centroid, 1, edge.snd);
}
}
for (int x : visited)
arr[x] = INF;
arr[0] = 0;
vector<pii> v;
for (auto x : adj[centroid])
v.pb(x);
for (auto x : v)
{
pii p = {centroid, x.snd};
adj[x.fst].erase(p);
adj[centroid].erase(x);
centroidDecomposition(x.fst, centroid);
}
}
int32_t best_path(int32_t n, int32_t K, int32_t h[][2], int32_t l[])
{
k = (int)K;
for (int i = 0; i < LIM; ++i)
{
adj[i].clear();
sz[i] = 0;
}
for (int i = 0; i < 1000000 + 5; ++i)
arr[i] = INF;
arr[0] = 0;
for (int i = 0; i < n - 1; ++i)
{
int u = h[i][0], v = h[i][1], len = l[i];
adj[u].insert({v, len});
adj[v].insert({u, len});
}
ans = INF;
centroidDecomposition(0, -1);
if (ans >= INF)
ans = -1;
int32_t finans = (int32_t)ans;
return finans;
}
// int32_t main()
// {
// int32_t n, k;
// cin >> n >> k;
// int32_t h[n - 1][2], l[n - 1];
// for (int i = 0; i < n - 1; ++i)
// {
// int u, v, len;
// cin >> u >> v >> len;
// h[i][0] = u;
// h[i][1] = v;
// l[i] = len;
// }
// cout << best_path(n, k, h, l);
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19020 KB |
Output is correct |
2 |
Correct |
11 ms |
19020 KB |
Output is correct |
3 |
Correct |
12 ms |
19024 KB |
Output is correct |
4 |
Correct |
11 ms |
19024 KB |
Output is correct |
5 |
Correct |
11 ms |
19032 KB |
Output is correct |
6 |
Correct |
13 ms |
19020 KB |
Output is correct |
7 |
Correct |
12 ms |
19036 KB |
Output is correct |
8 |
Correct |
11 ms |
18988 KB |
Output is correct |
9 |
Correct |
11 ms |
19020 KB |
Output is correct |
10 |
Correct |
10 ms |
19104 KB |
Output is correct |
11 |
Correct |
10 ms |
19020 KB |
Output is correct |
12 |
Correct |
12 ms |
19056 KB |
Output is correct |
13 |
Correct |
11 ms |
19044 KB |
Output is correct |
14 |
Correct |
10 ms |
19020 KB |
Output is correct |
15 |
Correct |
11 ms |
19020 KB |
Output is correct |
16 |
Correct |
11 ms |
19052 KB |
Output is correct |
17 |
Correct |
11 ms |
19032 KB |
Output is correct |
18 |
Correct |
10 ms |
19020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19020 KB |
Output is correct |
2 |
Correct |
11 ms |
19020 KB |
Output is correct |
3 |
Correct |
12 ms |
19024 KB |
Output is correct |
4 |
Correct |
11 ms |
19024 KB |
Output is correct |
5 |
Correct |
11 ms |
19032 KB |
Output is correct |
6 |
Correct |
13 ms |
19020 KB |
Output is correct |
7 |
Correct |
12 ms |
19036 KB |
Output is correct |
8 |
Correct |
11 ms |
18988 KB |
Output is correct |
9 |
Correct |
11 ms |
19020 KB |
Output is correct |
10 |
Correct |
10 ms |
19104 KB |
Output is correct |
11 |
Correct |
10 ms |
19020 KB |
Output is correct |
12 |
Correct |
12 ms |
19056 KB |
Output is correct |
13 |
Correct |
11 ms |
19044 KB |
Output is correct |
14 |
Correct |
10 ms |
19020 KB |
Output is correct |
15 |
Correct |
11 ms |
19020 KB |
Output is correct |
16 |
Correct |
11 ms |
19052 KB |
Output is correct |
17 |
Correct |
11 ms |
19032 KB |
Output is correct |
18 |
Correct |
10 ms |
19020 KB |
Output is correct |
19 |
Correct |
10 ms |
19020 KB |
Output is correct |
20 |
Correct |
12 ms |
19148 KB |
Output is correct |
21 |
Correct |
12 ms |
19276 KB |
Output is correct |
22 |
Correct |
12 ms |
19148 KB |
Output is correct |
23 |
Correct |
15 ms |
19188 KB |
Output is correct |
24 |
Correct |
13 ms |
19168 KB |
Output is correct |
25 |
Correct |
12 ms |
19160 KB |
Output is correct |
26 |
Correct |
12 ms |
19192 KB |
Output is correct |
27 |
Correct |
11 ms |
19148 KB |
Output is correct |
28 |
Correct |
11 ms |
19184 KB |
Output is correct |
29 |
Correct |
12 ms |
19148 KB |
Output is correct |
30 |
Correct |
12 ms |
19148 KB |
Output is correct |
31 |
Correct |
11 ms |
19148 KB |
Output is correct |
32 |
Correct |
12 ms |
19192 KB |
Output is correct |
33 |
Correct |
12 ms |
19148 KB |
Output is correct |
34 |
Correct |
13 ms |
19148 KB |
Output is correct |
35 |
Correct |
15 ms |
19180 KB |
Output is correct |
36 |
Correct |
14 ms |
19148 KB |
Output is correct |
37 |
Correct |
12 ms |
19264 KB |
Output is correct |
38 |
Correct |
12 ms |
19148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19020 KB |
Output is correct |
2 |
Correct |
11 ms |
19020 KB |
Output is correct |
3 |
Correct |
12 ms |
19024 KB |
Output is correct |
4 |
Correct |
11 ms |
19024 KB |
Output is correct |
5 |
Correct |
11 ms |
19032 KB |
Output is correct |
6 |
Correct |
13 ms |
19020 KB |
Output is correct |
7 |
Correct |
12 ms |
19036 KB |
Output is correct |
8 |
Correct |
11 ms |
18988 KB |
Output is correct |
9 |
Correct |
11 ms |
19020 KB |
Output is correct |
10 |
Correct |
10 ms |
19104 KB |
Output is correct |
11 |
Correct |
10 ms |
19020 KB |
Output is correct |
12 |
Correct |
12 ms |
19056 KB |
Output is correct |
13 |
Correct |
11 ms |
19044 KB |
Output is correct |
14 |
Correct |
10 ms |
19020 KB |
Output is correct |
15 |
Correct |
11 ms |
19020 KB |
Output is correct |
16 |
Correct |
11 ms |
19052 KB |
Output is correct |
17 |
Correct |
11 ms |
19032 KB |
Output is correct |
18 |
Correct |
10 ms |
19020 KB |
Output is correct |
19 |
Correct |
276 ms |
33028 KB |
Output is correct |
20 |
Correct |
286 ms |
32904 KB |
Output is correct |
21 |
Correct |
277 ms |
33408 KB |
Output is correct |
22 |
Correct |
255 ms |
33856 KB |
Output is correct |
23 |
Correct |
250 ms |
32836 KB |
Output is correct |
24 |
Correct |
138 ms |
32704 KB |
Output is correct |
25 |
Correct |
295 ms |
35928 KB |
Output is correct |
26 |
Correct |
152 ms |
37052 KB |
Output is correct |
27 |
Correct |
365 ms |
46404 KB |
Output is correct |
28 |
Correct |
761 ms |
52824 KB |
Output is correct |
29 |
Correct |
750 ms |
52776 KB |
Output is correct |
30 |
Correct |
372 ms |
46452 KB |
Output is correct |
31 |
Correct |
355 ms |
46516 KB |
Output is correct |
32 |
Correct |
473 ms |
46360 KB |
Output is correct |
33 |
Correct |
564 ms |
46404 KB |
Output is correct |
34 |
Correct |
580 ms |
46404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19020 KB |
Output is correct |
2 |
Correct |
11 ms |
19020 KB |
Output is correct |
3 |
Correct |
12 ms |
19024 KB |
Output is correct |
4 |
Correct |
11 ms |
19024 KB |
Output is correct |
5 |
Correct |
11 ms |
19032 KB |
Output is correct |
6 |
Correct |
13 ms |
19020 KB |
Output is correct |
7 |
Correct |
12 ms |
19036 KB |
Output is correct |
8 |
Correct |
11 ms |
18988 KB |
Output is correct |
9 |
Correct |
11 ms |
19020 KB |
Output is correct |
10 |
Correct |
10 ms |
19104 KB |
Output is correct |
11 |
Correct |
10 ms |
19020 KB |
Output is correct |
12 |
Correct |
12 ms |
19056 KB |
Output is correct |
13 |
Correct |
11 ms |
19044 KB |
Output is correct |
14 |
Correct |
10 ms |
19020 KB |
Output is correct |
15 |
Correct |
11 ms |
19020 KB |
Output is correct |
16 |
Correct |
11 ms |
19052 KB |
Output is correct |
17 |
Correct |
11 ms |
19032 KB |
Output is correct |
18 |
Correct |
10 ms |
19020 KB |
Output is correct |
19 |
Correct |
10 ms |
19020 KB |
Output is correct |
20 |
Correct |
12 ms |
19148 KB |
Output is correct |
21 |
Correct |
12 ms |
19276 KB |
Output is correct |
22 |
Correct |
12 ms |
19148 KB |
Output is correct |
23 |
Correct |
15 ms |
19188 KB |
Output is correct |
24 |
Correct |
13 ms |
19168 KB |
Output is correct |
25 |
Correct |
12 ms |
19160 KB |
Output is correct |
26 |
Correct |
12 ms |
19192 KB |
Output is correct |
27 |
Correct |
11 ms |
19148 KB |
Output is correct |
28 |
Correct |
11 ms |
19184 KB |
Output is correct |
29 |
Correct |
12 ms |
19148 KB |
Output is correct |
30 |
Correct |
12 ms |
19148 KB |
Output is correct |
31 |
Correct |
11 ms |
19148 KB |
Output is correct |
32 |
Correct |
12 ms |
19192 KB |
Output is correct |
33 |
Correct |
12 ms |
19148 KB |
Output is correct |
34 |
Correct |
13 ms |
19148 KB |
Output is correct |
35 |
Correct |
15 ms |
19180 KB |
Output is correct |
36 |
Correct |
14 ms |
19148 KB |
Output is correct |
37 |
Correct |
12 ms |
19264 KB |
Output is correct |
38 |
Correct |
12 ms |
19148 KB |
Output is correct |
39 |
Correct |
276 ms |
33028 KB |
Output is correct |
40 |
Correct |
286 ms |
32904 KB |
Output is correct |
41 |
Correct |
277 ms |
33408 KB |
Output is correct |
42 |
Correct |
255 ms |
33856 KB |
Output is correct |
43 |
Correct |
250 ms |
32836 KB |
Output is correct |
44 |
Correct |
138 ms |
32704 KB |
Output is correct |
45 |
Correct |
295 ms |
35928 KB |
Output is correct |
46 |
Correct |
152 ms |
37052 KB |
Output is correct |
47 |
Correct |
365 ms |
46404 KB |
Output is correct |
48 |
Correct |
761 ms |
52824 KB |
Output is correct |
49 |
Correct |
750 ms |
52776 KB |
Output is correct |
50 |
Correct |
372 ms |
46452 KB |
Output is correct |
51 |
Correct |
355 ms |
46516 KB |
Output is correct |
52 |
Correct |
473 ms |
46360 KB |
Output is correct |
53 |
Correct |
564 ms |
46404 KB |
Output is correct |
54 |
Correct |
580 ms |
46404 KB |
Output is correct |
55 |
Correct |
25 ms |
20516 KB |
Output is correct |
56 |
Correct |
24 ms |
20600 KB |
Output is correct |
57 |
Correct |
138 ms |
33872 KB |
Output is correct |
58 |
Correct |
104 ms |
35632 KB |
Output is correct |
59 |
Correct |
145 ms |
37068 KB |
Output is correct |
60 |
Correct |
804 ms |
54864 KB |
Output is correct |
61 |
Correct |
381 ms |
46904 KB |
Output is correct |
62 |
Correct |
379 ms |
48404 KB |
Output is correct |
63 |
Correct |
494 ms |
48488 KB |
Output is correct |
64 |
Correct |
786 ms |
48568 KB |
Output is correct |
65 |
Correct |
574 ms |
46404 KB |
Output is correct |
66 |
Correct |
843 ms |
52788 KB |
Output is correct |
67 |
Correct |
401 ms |
52184 KB |
Output is correct |
68 |
Correct |
582 ms |
46996 KB |
Output is correct |
69 |
Correct |
569 ms |
47300 KB |
Output is correct |
70 |
Correct |
496 ms |
45904 KB |
Output is correct |