#include <bits/stdc++.h>
#define endl "\n"
#define double long double
#define precise(x) \
fixed << setprecision(x)
#ifndef ONLINE_JUDGE
#define debug(var) cerr << (#var) << ": " << var << endl
#else
#define debug(var)
#endif
using namespace std;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using max_heap = priority_queue<T>;
template <class T>
ostream &operator<<(ostream &os, min_heap<T> H)
{
while (!H.empty())
{
os << H.top() << " ";
H.pop();
}
os << endl;
return os << "";
}
template <class T>
ostream &operator<<(ostream &os, max_heap<T> H)
{
while (!H.empty())
{
os << H.top() << " ";
H.pop();
}
os << endl;
return os << "";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P)
{
return os << P.first << " " << P.second;
}
template <class T>
ostream &operator<<(ostream &os, vector<T> arr)
{
for (long long i = 0; i < (long long)arr.size(); i++)
{
os << arr[i] << " ";
}
return os << "";
}
template <class T>
ostream &operator<<(ostream &os, vector<vector<T>> matrix)
{
os << endl;
for (long long i = 0; i < (long long)matrix.size(); i++)
{
for (long long j = 0; j < (long long)matrix[i].size(); j++)
{
os << matrix[i][j] << " ";
}
os << endl;
}
return os << "";
}
template <class T>
ostream &operator<<(ostream &os, set<T> S)
{
for (auto s : S)
{
os << s << " ";
}
return os << "";
}
template <class T>
ostream &operator<<(ostream &os, multiset<T> S)
{
for (auto s : S)
{
os << s << " ";
}
return os << "";
}
template <class L, class R>
ostream &operator<<(ostream &os, map<L, R> M)
{
os << endl;
for (auto m : M)
{
os << "key: " << m.first << endl;
os << "value: " << m.second << endl;
}
return os << "";
}
template <class L, class R>
ostream &operator<<(ostream &os, multimap<L, R> M)
{
os << endl;
for (auto m : M)
{
os << m << endl;
}
return os << "";
}
const long long max_n = 2e5 + 10;
vector<vector<long long>> adj;
vector<vector<pair<long long, long long>>> g;
map<long long, multiset<long long>> vals;
long long depth[max_n];
long long arr[max_n];
long long sz[max_n];
long long heavy = -1;
long long K;
long long ans = 1e9;
long long sack_size(long long node, long long cur_depth, long long cur, long long parent = 0)
{
arr[node] = cur;
depth[node] = cur_depth;
sz[node] = 1;
long long max_sz = 0;
for (long long i = 0; i < (long long)g[node].size(); i++)
{
if (g[node][i].first == parent)
{
continue;
}
long long temp = sack_size(g[node][i].first, cur_depth + 1, cur + g[node][i].second, node);
sz[node] += temp;
if (temp > max_sz)
{
max_sz = temp;
swap(adj[node][0], adj[node][i]);
}
}
return sz[node];
}
void sack_add(long long node, long long parent, long long root, long long type)
{
if (type == 0)
{
vals[arr[node]].erase(vals[arr[node]].find(depth[node]));
if (vals[arr[node]].size() == 0)
{
vals.erase(arr[node]);
}
}
else if (type == 1)
{
long long need = K - arr[node] + 2 * arr[root];
if (vals.find(need) != vals.end())
{
long long temp = *vals[need].begin() + depth[node] - 2 * depth[root];
if (ans > temp)
{
ans = temp;
}
}
}
else
{
vals[arr[node]].insert(depth[node]);
}
for (long long i = 0; i < (long long)adj[node].size(); i++)
{
if (adj[node][i] != parent && adj[node][i] != heavy)
{
sack_add(adj[node][i], node, root, type);
}
}
}
void sack_dfs(long long node, long long parent, long long keep)
{
for (long long i = 1; i < (long long)adj[node].size(); i++)
{
if (adj[node][i] != parent)
{
sack_dfs(adj[node][i], node, 0);
}
}
if (sz[node] != 1)
{
sack_dfs(adj[node][0], node, 1);
heavy = adj[node][0];
}
vals[arr[node]].insert(depth[node]);
long long need = K + arr[node];
if (vals.find(need) != vals.end())
{
long long temp = *vals[need].begin() - depth[node];
if (ans > temp)
{
ans = temp;
}
}
for (long long i = 1; i < (long long)adj[node].size(); i++)
{
if (adj[node][i] != parent)
{
sack_add(adj[node][i], node, node, 1);
sack_add(adj[node][i], node, node, 2);
}
}
heavy = -1;
if (!keep)
{
sack_add(node, parent, node, 0);
}
}
int best_path(int N_temp, int K_temp, int (*edge)[2], int *weight)
{
long long N = N_temp;
K = K_temp;
g.resize(N + 1);
adj.resize(N + 1);
for (long long i = 0; i < N - 1; i++)
{
edge[i][0]++;
edge[i][1]++;
g[edge[i][0]].push_back({edge[i][1], weight[i]});
g[edge[i][1]].push_back({edge[i][0], weight[i]});
adj[edge[i][0]].push_back(edge[i][1]);
adj[edge[i][1]].push_back(edge[i][0]);
}
sack_size(1, 1, 1);
sack_dfs(1, 0, 0);
if (ans == 1e9)
{
ans = -1;
}
return ans;
}
// long long32_t main()
// {
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// long long no_of_testcases = 1;
// // cin >> no_of_testcases;
// for (long long i = 1; i <= no_of_testcases; i++)
// {
// // cout << "Case #" << i << ": ";
// solve();
// }
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
564 KB |
Output is correct |
22 |
Correct |
3 ms |
588 KB |
Output is correct |
23 |
Correct |
3 ms |
588 KB |
Output is correct |
24 |
Correct |
3 ms |
588 KB |
Output is correct |
25 |
Correct |
3 ms |
568 KB |
Output is correct |
26 |
Correct |
3 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
568 KB |
Output is correct |
28 |
Correct |
3 ms |
588 KB |
Output is correct |
29 |
Correct |
3 ms |
588 KB |
Output is correct |
30 |
Correct |
3 ms |
588 KB |
Output is correct |
31 |
Correct |
3 ms |
588 KB |
Output is correct |
32 |
Correct |
3 ms |
588 KB |
Output is correct |
33 |
Correct |
3 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
588 KB |
Output is correct |
35 |
Correct |
2 ms |
588 KB |
Output is correct |
36 |
Correct |
2 ms |
588 KB |
Output is correct |
37 |
Correct |
2 ms |
588 KB |
Output is correct |
38 |
Correct |
2 ms |
628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
490 ms |
22588 KB |
Output is correct |
20 |
Correct |
520 ms |
22524 KB |
Output is correct |
21 |
Correct |
469 ms |
22536 KB |
Output is correct |
22 |
Correct |
429 ms |
22576 KB |
Output is correct |
23 |
Correct |
667 ms |
23280 KB |
Output is correct |
24 |
Correct |
356 ms |
22220 KB |
Output is correct |
25 |
Correct |
166 ms |
27780 KB |
Output is correct |
26 |
Correct |
93 ms |
32552 KB |
Output is correct |
27 |
Correct |
526 ms |
44648 KB |
Output is correct |
28 |
Correct |
510 ms |
82884 KB |
Output is correct |
29 |
Correct |
508 ms |
81240 KB |
Output is correct |
30 |
Correct |
501 ms |
44772 KB |
Output is correct |
31 |
Correct |
548 ms |
44736 KB |
Output is correct |
32 |
Correct |
692 ms |
44756 KB |
Output is correct |
33 |
Correct |
724 ms |
42800 KB |
Output is correct |
34 |
Correct |
986 ms |
60812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
564 KB |
Output is correct |
22 |
Correct |
3 ms |
588 KB |
Output is correct |
23 |
Correct |
3 ms |
588 KB |
Output is correct |
24 |
Correct |
3 ms |
588 KB |
Output is correct |
25 |
Correct |
3 ms |
568 KB |
Output is correct |
26 |
Correct |
3 ms |
588 KB |
Output is correct |
27 |
Correct |
2 ms |
568 KB |
Output is correct |
28 |
Correct |
3 ms |
588 KB |
Output is correct |
29 |
Correct |
3 ms |
588 KB |
Output is correct |
30 |
Correct |
3 ms |
588 KB |
Output is correct |
31 |
Correct |
3 ms |
588 KB |
Output is correct |
32 |
Correct |
3 ms |
588 KB |
Output is correct |
33 |
Correct |
3 ms |
588 KB |
Output is correct |
34 |
Correct |
2 ms |
588 KB |
Output is correct |
35 |
Correct |
2 ms |
588 KB |
Output is correct |
36 |
Correct |
2 ms |
588 KB |
Output is correct |
37 |
Correct |
2 ms |
588 KB |
Output is correct |
38 |
Correct |
2 ms |
628 KB |
Output is correct |
39 |
Correct |
490 ms |
22588 KB |
Output is correct |
40 |
Correct |
520 ms |
22524 KB |
Output is correct |
41 |
Correct |
469 ms |
22536 KB |
Output is correct |
42 |
Correct |
429 ms |
22576 KB |
Output is correct |
43 |
Correct |
667 ms |
23280 KB |
Output is correct |
44 |
Correct |
356 ms |
22220 KB |
Output is correct |
45 |
Correct |
166 ms |
27780 KB |
Output is correct |
46 |
Correct |
93 ms |
32552 KB |
Output is correct |
47 |
Correct |
526 ms |
44648 KB |
Output is correct |
48 |
Correct |
510 ms |
82884 KB |
Output is correct |
49 |
Correct |
508 ms |
81240 KB |
Output is correct |
50 |
Correct |
501 ms |
44772 KB |
Output is correct |
51 |
Correct |
548 ms |
44736 KB |
Output is correct |
52 |
Correct |
692 ms |
44756 KB |
Output is correct |
53 |
Correct |
724 ms |
42800 KB |
Output is correct |
54 |
Correct |
986 ms |
60812 KB |
Output is correct |
55 |
Correct |
30 ms |
3096 KB |
Output is correct |
56 |
Correct |
31 ms |
2496 KB |
Output is correct |
57 |
Correct |
333 ms |
23112 KB |
Output is correct |
58 |
Correct |
128 ms |
22876 KB |
Output is correct |
59 |
Correct |
129 ms |
42712 KB |
Output is correct |
60 |
Correct |
481 ms |
84084 KB |
Output is correct |
61 |
Correct |
545 ms |
49812 KB |
Output is correct |
62 |
Correct |
466 ms |
47232 KB |
Output is correct |
63 |
Correct |
657 ms |
47124 KB |
Output is correct |
64 |
Correct |
1538 ms |
56620 KB |
Output is correct |
65 |
Correct |
1440 ms |
66328 KB |
Output is correct |
66 |
Correct |
522 ms |
80356 KB |
Output is correct |
67 |
Correct |
468 ms |
46768 KB |
Output is correct |
68 |
Correct |
843 ms |
62900 KB |
Output is correct |
69 |
Correct |
860 ms |
63304 KB |
Output is correct |
70 |
Correct |
811 ms |
60192 KB |
Output is correct |