#include<bits/stdc++.h>
using namespace std;
void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if(sizeof...(t)) cerr << ", ";
DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL
#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 each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"
#define fi first
#define se second
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;
void setIO(string NAME = "") {
cin.tie(0)->sync_with_stdio(0);
if(sz(NAME)) {
freopen((NAME + ".inp").c_str(),"r",stdin);
freopen((NAME + ".out").c_str(),"w",stdout);
}
}
tcT> bool ckmin(T&a, const T&b) {
return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
return b > a ? a=b,1 : 0; }
const int MOD = 1e9 + 7;
const int MX = 2e5+5;
const ll INF = 1e18;
int N, dep[MX];
V<pi> adj[MX];
map<ll, int> mn_dep[MX];
ll dist[MX], K;
int ans = MOD;
void dfs(int u, int p) {
mn_dep[u][dist[u]] = dep[u];
each(ed, adj[u]) if(ed.fi != p) {
dep[ed.fi] = dep[u] + 1;
dist[ed.fi] = dist[u] + ed.se;
dfs(ed.fi, u);
}
}
void dfsCalc(int u, int p) {
each(ed, adj[u]) if(ed.fi != p) {
dfsCalc(ed.fi, u);
int v = ed.fi;
if(sz(mn_dep[u]) < sz(mn_dep[v])) swap(mn_dep[u], mn_dep[v]);
each(val, mn_dep[v]) {
ll du = K + dist[u]*2 - val.fi;
if(mn_dep[u].find(du) != mn_dep[u].end()) ckmin(ans, val.se + mn_dep[u][du] - 2*dep[u]);
}
each(val, mn_dep[v]) {
if(mn_dep[u].find(val.fi) != mn_dep[u].end()) ckmin(mn_dep[u][val.fi], val.se);
else mn_dep[u][val.fi] = val.se;
}
mn_dep[v].clear();
}
}
int best_path(int n, int k, int eds[][2], int cst[]) {
N = n;
K = k;
F0R(i, N-1) {
int u = eds[i][0], v = eds[i][1], w = cst[i];
adj[u].pb({v, w});
adj[v].pb({u, w});
}
dfs(0, -1);
dfsCalc(0, -1);
if(ans >= MOD) ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'void setIO(std::string)':
race.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
39 | freopen((NAME + ".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((NAME + ".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14336 KB |
Output is correct |
5 |
Correct |
9 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14428 KB |
Output is correct |
7 |
Correct |
9 ms |
14440 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14408 KB |
Output is correct |
10 |
Correct |
8 ms |
14404 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14444 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
10 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14336 KB |
Output is correct |
5 |
Correct |
9 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14428 KB |
Output is correct |
7 |
Correct |
9 ms |
14440 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14408 KB |
Output is correct |
10 |
Correct |
8 ms |
14404 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14444 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
10 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14408 KB |
Output is correct |
20 |
Correct |
8 ms |
14440 KB |
Output is correct |
21 |
Correct |
8 ms |
14540 KB |
Output is correct |
22 |
Correct |
8 ms |
14548 KB |
Output is correct |
23 |
Correct |
9 ms |
14548 KB |
Output is correct |
24 |
Correct |
9 ms |
14548 KB |
Output is correct |
25 |
Correct |
9 ms |
14524 KB |
Output is correct |
26 |
Correct |
9 ms |
14548 KB |
Output is correct |
27 |
Correct |
8 ms |
14560 KB |
Output is correct |
28 |
Correct |
8 ms |
14500 KB |
Output is correct |
29 |
Correct |
9 ms |
14548 KB |
Output is correct |
30 |
Correct |
9 ms |
14544 KB |
Output is correct |
31 |
Correct |
11 ms |
14532 KB |
Output is correct |
32 |
Correct |
11 ms |
14548 KB |
Output is correct |
33 |
Correct |
12 ms |
14548 KB |
Output is correct |
34 |
Correct |
8 ms |
14540 KB |
Output is correct |
35 |
Correct |
8 ms |
14548 KB |
Output is correct |
36 |
Correct |
8 ms |
14548 KB |
Output is correct |
37 |
Correct |
9 ms |
14548 KB |
Output is correct |
38 |
Correct |
9 ms |
14544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14336 KB |
Output is correct |
5 |
Correct |
9 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14428 KB |
Output is correct |
7 |
Correct |
9 ms |
14440 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14408 KB |
Output is correct |
10 |
Correct |
8 ms |
14404 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14444 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
10 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
121 ms |
27936 KB |
Output is correct |
20 |
Correct |
122 ms |
28000 KB |
Output is correct |
21 |
Correct |
115 ms |
27976 KB |
Output is correct |
22 |
Correct |
154 ms |
28248 KB |
Output is correct |
23 |
Correct |
146 ms |
28360 KB |
Output is correct |
24 |
Correct |
117 ms |
28244 KB |
Output is correct |
25 |
Correct |
105 ms |
35700 KB |
Output is correct |
26 |
Correct |
66 ms |
43116 KB |
Output is correct |
27 |
Correct |
205 ms |
41928 KB |
Output is correct |
28 |
Correct |
270 ms |
72100 KB |
Output is correct |
29 |
Correct |
336 ms |
70772 KB |
Output is correct |
30 |
Correct |
190 ms |
41944 KB |
Output is correct |
31 |
Correct |
195 ms |
41948 KB |
Output is correct |
32 |
Correct |
277 ms |
42120 KB |
Output is correct |
33 |
Correct |
251 ms |
41000 KB |
Output is correct |
34 |
Correct |
337 ms |
45224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14336 KB |
Output is correct |
5 |
Correct |
9 ms |
14420 KB |
Output is correct |
6 |
Correct |
9 ms |
14428 KB |
Output is correct |
7 |
Correct |
9 ms |
14440 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14408 KB |
Output is correct |
10 |
Correct |
8 ms |
14404 KB |
Output is correct |
11 |
Correct |
8 ms |
14420 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
7 ms |
14420 KB |
Output is correct |
15 |
Correct |
9 ms |
14444 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
10 ms |
14420 KB |
Output is correct |
18 |
Correct |
8 ms |
14420 KB |
Output is correct |
19 |
Correct |
9 ms |
14408 KB |
Output is correct |
20 |
Correct |
8 ms |
14440 KB |
Output is correct |
21 |
Correct |
8 ms |
14540 KB |
Output is correct |
22 |
Correct |
8 ms |
14548 KB |
Output is correct |
23 |
Correct |
9 ms |
14548 KB |
Output is correct |
24 |
Correct |
9 ms |
14548 KB |
Output is correct |
25 |
Correct |
9 ms |
14524 KB |
Output is correct |
26 |
Correct |
9 ms |
14548 KB |
Output is correct |
27 |
Correct |
8 ms |
14560 KB |
Output is correct |
28 |
Correct |
8 ms |
14500 KB |
Output is correct |
29 |
Correct |
9 ms |
14548 KB |
Output is correct |
30 |
Correct |
9 ms |
14544 KB |
Output is correct |
31 |
Correct |
11 ms |
14532 KB |
Output is correct |
32 |
Correct |
11 ms |
14548 KB |
Output is correct |
33 |
Correct |
12 ms |
14548 KB |
Output is correct |
34 |
Correct |
8 ms |
14540 KB |
Output is correct |
35 |
Correct |
8 ms |
14548 KB |
Output is correct |
36 |
Correct |
8 ms |
14548 KB |
Output is correct |
37 |
Correct |
9 ms |
14548 KB |
Output is correct |
38 |
Correct |
9 ms |
14544 KB |
Output is correct |
39 |
Correct |
121 ms |
27936 KB |
Output is correct |
40 |
Correct |
122 ms |
28000 KB |
Output is correct |
41 |
Correct |
115 ms |
27976 KB |
Output is correct |
42 |
Correct |
154 ms |
28248 KB |
Output is correct |
43 |
Correct |
146 ms |
28360 KB |
Output is correct |
44 |
Correct |
117 ms |
28244 KB |
Output is correct |
45 |
Correct |
105 ms |
35700 KB |
Output is correct |
46 |
Correct |
66 ms |
43116 KB |
Output is correct |
47 |
Correct |
205 ms |
41928 KB |
Output is correct |
48 |
Correct |
270 ms |
72100 KB |
Output is correct |
49 |
Correct |
336 ms |
70772 KB |
Output is correct |
50 |
Correct |
190 ms |
41944 KB |
Output is correct |
51 |
Correct |
195 ms |
41948 KB |
Output is correct |
52 |
Correct |
277 ms |
42120 KB |
Output is correct |
53 |
Correct |
251 ms |
41000 KB |
Output is correct |
54 |
Correct |
337 ms |
45224 KB |
Output is correct |
55 |
Correct |
21 ms |
15700 KB |
Output is correct |
56 |
Correct |
16 ms |
15700 KB |
Output is correct |
57 |
Correct |
87 ms |
28228 KB |
Output is correct |
58 |
Correct |
54 ms |
27976 KB |
Output is correct |
59 |
Correct |
88 ms |
43088 KB |
Output is correct |
60 |
Correct |
304 ms |
71068 KB |
Output is correct |
61 |
Correct |
232 ms |
42120 KB |
Output is correct |
62 |
Correct |
186 ms |
42092 KB |
Output is correct |
63 |
Correct |
297 ms |
42280 KB |
Output is correct |
64 |
Correct |
447 ms |
42640 KB |
Output is correct |
65 |
Correct |
495 ms |
44976 KB |
Output is correct |
66 |
Correct |
298 ms |
67744 KB |
Output is correct |
67 |
Correct |
190 ms |
42756 KB |
Output is correct |
68 |
Correct |
376 ms |
42756 KB |
Output is correct |
69 |
Correct |
366 ms |
46196 KB |
Output is correct |
70 |
Correct |
379 ms |
41572 KB |
Output is correct |