//
// Created by adavy on 2/11/2023.
//
#include <bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using ii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vii = vector<ii>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define f first
#define s second
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define len(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
vector<vector<pair<ll,ll>>> adj;
vl sz,dep,dist;
vb vis;
ll D;
ll bestD = INF;
int init_sz(int nd, int par=-1){
if (vis[nd]) return 0;
sz[nd] = 1;
trav(nei, adj[nd]) if (nei.s!=par) sz[nd] += init_sz(nei.s,nd);
return sz[nd];
}
int find_centroid(int nd, int par, ll num){
trav(nei, adj[nd]) if (nei.s != par) if (!vis[nei.s]&&sz[nei.s]>num/2) return find_centroid(nei.s, nd, num);
return nd;
}
void dfs(int nd, int par,map<ll,ll>& mp,vector<pair<ll,ll>>& prl){
if (mp.find(D-dist[nd])==mp.end()) mp[D-dist[nd]] = INF;
bestD = min(bestD,dep[nd]+mp[D-dist[nd]]);
prl.pb({dist[nd],dep[nd]});
trav(nei, adj[nd]) if (nei.s != par) if (!vis[nei.s]) {
dep[nei.s] = dep[nd] + 1;
dist[nei.s] = dist[nd] + nei.f;
dfs(nei.s,nd,mp,prl);
}
}
void init_centroid(int nd, int par=-1){
init_sz(nd);
int c = find_centroid(nd, -1, sz[nd]);
vis[c] = 1;
map<ll,ll> mp; //weighted, unweighted
trav(nei, adj[c]) if (!vis[nei.s]) {
//if (c==0) cout << "nei " << nei.s << endl;
vector<pair<ll,ll>> prl;
dep[nei.s] = 1;
dist[nei.s] = nei.f;
dfs(nei.s, -1,mp,prl);
for(auto&[distL,depL]:prl){
//if (c==0) {
//cout << "dstdp " << distL << " " << depL << endl;
//}
if (mp.find(distL)==mp.end()) mp[distL] = INF;
mp[distL] = min(mp[distL],depL);
}
}
if (mp.find(D)==mp.end()) mp[D] = INF;
//if (c==0) {
// for(auto&[thing1,thing2]:mp) cout << thing1 <<" "<< thing2 << endl;
//}
bestD = min(bestD,mp[D]);
trav(nei, adj[c]) if (!vis[nei.s]) init_centroid(nei.s,c);
}
int best_path(int n, int k, int h[][2], int l[])
{
D = k;
adj.rsz(n);
vis.rsz(n);
sz.rsz(n);
dep.rsz(n);
dist.rsz(n);
fill(all(vis),0);
F0R(i, n-1){
adj[h[i][0]].pb({l[i],h[i][1]});
adj[h[i][1]].pb({l[i],h[i][0]});
}
init_centroid(0);
if (bestD==INF) return -1;
else return bestD;
}
/*
* 11 34
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
7 */
#include "race.h"
/*
#define MAX_N 500000
static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;
inline
void my_assert(int e) {if (!e) abort();}
void read_input()
{
int i;
my_assert(2==scanf("%d %d",&N,&K));
for(i=0; i<N-1; i++)
my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
my_assert(1==scanf("%d",&solution));
}
int main()
{
int ans;
read_input();
ans = best_path(N,K,H,L);
if(ans==solution)
printf("Correct.\n");
else
printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);
return 0;
}
*/
Compilation message
race.cpp: In function 'void init_centroid(int, int)':
race.cpp:129:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
129 | for(auto&[distL,depL]:prl){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
3 ms |
472 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
3 ms |
572 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
4 ms |
664 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
3 ms |
596 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
3 ms |
568 KB |
Output is correct |
38 |
Correct |
3 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
287 ms |
13584 KB |
Output is correct |
20 |
Correct |
299 ms |
13436 KB |
Output is correct |
21 |
Correct |
259 ms |
13724 KB |
Output is correct |
22 |
Correct |
228 ms |
13136 KB |
Output is correct |
23 |
Correct |
471 ms |
13484 KB |
Output is correct |
24 |
Correct |
226 ms |
12372 KB |
Output is correct |
25 |
Correct |
248 ms |
20952 KB |
Output is correct |
26 |
Correct |
106 ms |
20376 KB |
Output is correct |
27 |
Correct |
371 ms |
24024 KB |
Output is correct |
28 |
Correct |
1601 ms |
85832 KB |
Output is correct |
29 |
Correct |
1599 ms |
85864 KB |
Output is correct |
30 |
Correct |
374 ms |
24012 KB |
Output is correct |
31 |
Correct |
375 ms |
24140 KB |
Output is correct |
32 |
Correct |
432 ms |
24020 KB |
Output is correct |
33 |
Correct |
678 ms |
25648 KB |
Output is correct |
34 |
Correct |
1376 ms |
65988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
308 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
308 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
312 KB |
Output is correct |
20 |
Correct |
1 ms |
300 KB |
Output is correct |
21 |
Correct |
3 ms |
472 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
3 ms |
596 KB |
Output is correct |
24 |
Correct |
3 ms |
596 KB |
Output is correct |
25 |
Correct |
3 ms |
596 KB |
Output is correct |
26 |
Correct |
3 ms |
572 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
596 KB |
Output is correct |
29 |
Correct |
3 ms |
596 KB |
Output is correct |
30 |
Correct |
4 ms |
664 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
3 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
596 KB |
Output is correct |
34 |
Correct |
3 ms |
596 KB |
Output is correct |
35 |
Correct |
3 ms |
596 KB |
Output is correct |
36 |
Correct |
3 ms |
596 KB |
Output is correct |
37 |
Correct |
3 ms |
568 KB |
Output is correct |
38 |
Correct |
3 ms |
620 KB |
Output is correct |
39 |
Correct |
287 ms |
13584 KB |
Output is correct |
40 |
Correct |
299 ms |
13436 KB |
Output is correct |
41 |
Correct |
259 ms |
13724 KB |
Output is correct |
42 |
Correct |
228 ms |
13136 KB |
Output is correct |
43 |
Correct |
471 ms |
13484 KB |
Output is correct |
44 |
Correct |
226 ms |
12372 KB |
Output is correct |
45 |
Correct |
248 ms |
20952 KB |
Output is correct |
46 |
Correct |
106 ms |
20376 KB |
Output is correct |
47 |
Correct |
371 ms |
24024 KB |
Output is correct |
48 |
Correct |
1601 ms |
85832 KB |
Output is correct |
49 |
Correct |
1599 ms |
85864 KB |
Output is correct |
50 |
Correct |
374 ms |
24012 KB |
Output is correct |
51 |
Correct |
375 ms |
24140 KB |
Output is correct |
52 |
Correct |
432 ms |
24020 KB |
Output is correct |
53 |
Correct |
678 ms |
25648 KB |
Output is correct |
54 |
Correct |
1376 ms |
65988 KB |
Output is correct |
55 |
Correct |
34 ms |
2824 KB |
Output is correct |
56 |
Correct |
16 ms |
1748 KB |
Output is correct |
57 |
Correct |
171 ms |
13544 KB |
Output is correct |
58 |
Correct |
43 ms |
11584 KB |
Output is correct |
59 |
Correct |
558 ms |
36920 KB |
Output is correct |
60 |
Correct |
1585 ms |
81092 KB |
Output is correct |
61 |
Correct |
468 ms |
27212 KB |
Output is correct |
62 |
Correct |
399 ms |
24224 KB |
Output is correct |
63 |
Correct |
475 ms |
24136 KB |
Output is correct |
64 |
Correct |
1515 ms |
52800 KB |
Output is correct |
65 |
Correct |
1376 ms |
66812 KB |
Output is correct |
66 |
Correct |
1639 ms |
86028 KB |
Output is correct |
67 |
Correct |
117 ms |
22420 KB |
Output is correct |
68 |
Correct |
740 ms |
54452 KB |
Output is correct |
69 |
Correct |
732 ms |
55324 KB |
Output is correct |
70 |
Correct |
675 ms |
52012 KB |
Output is correct |