//#include <iostream> //cin, cout
#include <race.h>
/*
#include <fstream>
std::ifstream cin ("ex.in");
std::ofstream cout ("ex.out");
*/
// includes
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#include <array>
#include <algorithm>
#include <numeric>
#include <iomanip>
#include <unordered_set>
#include <stack>
#include <ext/pb_ds/assoc_container.hpp>
#include <random>
#include <chrono>
//usings
using namespace std;
using namespace __gnu_pbds;
// misc
#define ll long long
#define pb push_back
#define pq priority_queue
#define ub upper_bound
#define lb lower_bound
template<typename T, typename U> bool emin(T &a, const U &b){ return b < a ? a = b, true : false; }
template<typename T, typename U> bool emax(T &a, const U &b){ return b > a ? a = b, true : false; }
typedef uint64_t hash_t;
// vectors
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vpii vector<pair<int, int>>
#define vvpii vector<vector<pair<int, int>>>
#define vppipi vector<pair<int, pair<int, int>>>
#define vl vector<ll>
#define vvl vector<vl>
#define vvvl vector<vvl>
#define vpll vector<pair<ll, ll>>
#define vb vector<bool>
#define vvb vector<vb>
#define vs vector<string>
#define sz(x) (int)x.size()
#define rz resize
#define all(x) x.begin(), x.end()
// pairs
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define f first
#define s second
// sets
#define si set<int>
#define sl set<ll>
#define ss set<string>
#define in insert
template <class T> using iset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// maps
#define mii map<int, int>
#define mll map<ll, ll>
// loops
#define FR(x, z, y) for (int x = z; x < y; x++)
#define FRE(x, z, y) FR(x, z, y + 1)
#define F(x, y) FR(x, 0, y)
#define FE(x, y) F(x, y + 1)
#define A(x, y) for(auto &x : y)
vvpii a;
vl dis, sum;
vector<mll> v;
ll an = 0;
int n, m;
void pre(int x, int p, ll c, int d){
v[x][c] = d;
sum[x] = c;
dis[x] = d;
A(u, a[x]) if(u.f != p) pre(u.f, x, c + u.s, d + 1);
}
void dfs(int x, int p){
ll tar = m + 2 * sum[x];
A(u, a[x]){
if(u.f == p) continue;
dfs(u.f, x);
if(sz(v[u.f]) > sz(v[x])) swap(v[u.f], v[x]);
A(k, v[u.f]){
if(v[x].find(tar - k.f) != v[x].end()) emin(an, v[x][tar - k.f] + k.s - 2 * dis[x]);
}
A(k, v[u.f]){
if(v[x].find(k.f) == v[x].end()) v[x].in(k);
else emin(v[x][k.f], k.s);
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
if(K == 1) return 0;
n = N, m = K;
an = n + 7;
a = vvpii(n);
dis = sum = vl(n, 0);
v = vector<mll>(n);
F(i, n - 1){
a[H[i][0]].pb({H[i][1], L[i]});
a[H[i][1]].pb({H[i][0], L[i]});
}
pre(0, -1, 0, 0);
dfs(0, -1);
return (an == n + 7 ? -1 : an);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2480 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2480 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2480 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2480 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2748 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2904 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2480 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2480 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
157 ms |
32088 KB |
Output is correct |
20 |
Correct |
159 ms |
32024 KB |
Output is correct |
21 |
Correct |
151 ms |
31684 KB |
Output is correct |
22 |
Correct |
162 ms |
31460 KB |
Output is correct |
23 |
Correct |
147 ms |
44044 KB |
Output is correct |
24 |
Correct |
130 ms |
34744 KB |
Output is correct |
25 |
Correct |
103 ms |
30288 KB |
Output is correct |
26 |
Correct |
53 ms |
36180 KB |
Output is correct |
27 |
Correct |
182 ms |
51992 KB |
Output is correct |
28 |
Correct |
331 ms |
85000 KB |
Output is correct |
29 |
Correct |
298 ms |
83796 KB |
Output is correct |
30 |
Correct |
202 ms |
51868 KB |
Output is correct |
31 |
Correct |
218 ms |
51908 KB |
Output is correct |
32 |
Correct |
313 ms |
52068 KB |
Output is correct |
33 |
Correct |
345 ms |
56888 KB |
Output is correct |
34 |
Correct |
360 ms |
89712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2480 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2480 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2748 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2904 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
39 |
Correct |
157 ms |
32088 KB |
Output is correct |
40 |
Correct |
159 ms |
32024 KB |
Output is correct |
41 |
Correct |
151 ms |
31684 KB |
Output is correct |
42 |
Correct |
162 ms |
31460 KB |
Output is correct |
43 |
Correct |
147 ms |
44044 KB |
Output is correct |
44 |
Correct |
130 ms |
34744 KB |
Output is correct |
45 |
Correct |
103 ms |
30288 KB |
Output is correct |
46 |
Correct |
53 ms |
36180 KB |
Output is correct |
47 |
Correct |
182 ms |
51992 KB |
Output is correct |
48 |
Correct |
331 ms |
85000 KB |
Output is correct |
49 |
Correct |
298 ms |
83796 KB |
Output is correct |
50 |
Correct |
202 ms |
51868 KB |
Output is correct |
51 |
Correct |
218 ms |
51908 KB |
Output is correct |
52 |
Correct |
313 ms |
52068 KB |
Output is correct |
53 |
Correct |
345 ms |
56888 KB |
Output is correct |
54 |
Correct |
360 ms |
89712 KB |
Output is correct |
55 |
Correct |
14 ms |
6440 KB |
Output is correct |
56 |
Correct |
7 ms |
4956 KB |
Output is correct |
57 |
Correct |
67 ms |
29288 KB |
Output is correct |
58 |
Correct |
42 ms |
22480 KB |
Output is correct |
59 |
Correct |
79 ms |
42320 KB |
Output is correct |
60 |
Correct |
295 ms |
84048 KB |
Output is correct |
61 |
Correct |
261 ms |
55524 KB |
Output is correct |
62 |
Correct |
207 ms |
51824 KB |
Output is correct |
63 |
Correct |
343 ms |
52044 KB |
Output is correct |
64 |
Correct |
534 ms |
107232 KB |
Output is correct |
65 |
Correct |
484 ms |
105320 KB |
Output is correct |
66 |
Correct |
363 ms |
81428 KB |
Output is correct |
67 |
Correct |
205 ms |
46412 KB |
Output is correct |
68 |
Correct |
418 ms |
68828 KB |
Output is correct |
69 |
Correct |
469 ms |
73944 KB |
Output is correct |
70 |
Correct |
354 ms |
66072 KB |
Output is correct |