#pragma GCC optimize("O1,O2,O3,Ofast,unroll-loops")
#include <bits/stdc++.h>
#include <string>
#include <iostream>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<int, int> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define len(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define F first
#define nl endl
#define S second
#define lb lower_bound
#define ub upper_bound
#define aint(x) x.begin(), x.end()
#define raint(x) x.rbegin(), x.rend()
#define ins insert
const int MOD = 1000000007;
#include "race.h"
/*
1.BFS numer of children (priority queue with visited)
2.Check children nodes with each other (set with upper and lower bound)
3.Save children distances to the parent node (save a set with distances to each leaf)
*/
vector<vpi> paths(2e5+3,vpi());
vector<map<int,pi>> dist(2e5+3,map<int,pi>());
vi children(2e5+3);
vi parent(2e5+3,-1);
vi parent_dist(2e5+3,-1);
void dfs(int pos,int prev){
int sum = 1;
for(auto node:paths[pos]){
if(node.F == prev)continue;
parent[node.F] = pos;
parent_dist[node.F] = node.S;
dfs(node.F,pos);
sum += children[node.F];
}
children[pos] = sum;
}
int best_path(int N, int K, int H[][2], int L[])
{
FOR(i,0,N-1){
paths[H[i][0]].pb({H[i][1],L[i]});
paths[H[i][1]].pb({H[i][0],L[i]});
}
dfs(0,-1);
// FOR(i,0,N)cout << children[i] << " ";
// cout << endl;
// FOR(i,0,N)cout << parent[i] << " ";
// cout << endl;
// FOR(i,0,N)cout << parent_dist[i] << " ";
// cout << endl;
priority_queue<pi> pq;
FOR(i,0,N){
pq.push({-children[i],i});
}
int ans = 1e9;
while(!pq.empty()){
int node = pq.top().S;pq.pop();
// cout << node << " | " << endl;
for(auto a:dist[node]){
// cout << "a " << a.F << " " << K-a.F;
if(a.F > K)continue;
if(dist[node].count(K-a.F)){
// cout << " " << dist[node][K-a.F].S << " " << a.S.S << " " << a.S.F << " " << dist[node][K-a.F].F;
if(dist[node][K-a.F].S != a.S.S){
// cout << " ans - " << a.S.F+dist[node][K-a.F].F+1;
ans = min(ans,a.S.F+dist[node][K-a.F].F+1);
}
}
// cout << endl;
}
if(parent[node] == -1)continue;
// cout << "b " << parent[node] << " " << parent_dist[node] << " " << 1 << " " << node << endl;
dist[parent[node]].insert({parent_dist[node],{1,node}});
for(auto a:dist[node]){
// cout << "c " << parent[node] << " " << parent_dist[node]+a.F << " " << a.S.F+1 << " " << node << endl;
if(parent_dist[node]+a.F == K){
ans = min(ans, a.S.F+2);
}
if(parent_dist[node]+a.F >= K)continue;
dist[parent[node]].insert({parent_dist[node]+a.F,{a.S.F+1,node}});
}
// cout << endl;
}
if(ans == 1e9)return -1;
return ans-1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
5 ms |
18780 KB |
Output is correct |
5 |
Correct |
7 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
19036 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
5 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
19036 KB |
Output is correct |
10 |
Correct |
4 ms |
18776 KB |
Output is correct |
11 |
Correct |
4 ms |
18944 KB |
Output is correct |
12 |
Correct |
4 ms |
18776 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
5 ms |
19052 KB |
Output is correct |
16 |
Correct |
4 ms |
19036 KB |
Output is correct |
17 |
Correct |
5 ms |
19036 KB |
Output is correct |
18 |
Correct |
6 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
5 ms |
18780 KB |
Output is correct |
5 |
Correct |
7 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
19036 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
5 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
19036 KB |
Output is correct |
10 |
Correct |
4 ms |
18776 KB |
Output is correct |
11 |
Correct |
4 ms |
18944 KB |
Output is correct |
12 |
Correct |
4 ms |
18776 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
5 ms |
19052 KB |
Output is correct |
16 |
Correct |
4 ms |
19036 KB |
Output is correct |
17 |
Correct |
5 ms |
19036 KB |
Output is correct |
18 |
Correct |
6 ms |
19036 KB |
Output is correct |
19 |
Correct |
4 ms |
18780 KB |
Output is correct |
20 |
Correct |
4 ms |
18800 KB |
Output is correct |
21 |
Correct |
6 ms |
19288 KB |
Output is correct |
22 |
Correct |
5 ms |
19036 KB |
Output is correct |
23 |
Correct |
5 ms |
19036 KB |
Output is correct |
24 |
Correct |
5 ms |
19036 KB |
Output is correct |
25 |
Correct |
6 ms |
19476 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
27 |
Correct |
6 ms |
19036 KB |
Output is correct |
28 |
Correct |
6 ms |
19288 KB |
Output is correct |
29 |
Correct |
5 ms |
19292 KB |
Output is correct |
30 |
Correct |
7 ms |
19288 KB |
Output is correct |
31 |
Correct |
6 ms |
19548 KB |
Output is correct |
32 |
Correct |
6 ms |
19548 KB |
Output is correct |
33 |
Correct |
7 ms |
19848 KB |
Output is correct |
34 |
Correct |
7 ms |
20316 KB |
Output is correct |
35 |
Correct |
9 ms |
20608 KB |
Output is correct |
36 |
Correct |
10 ms |
20828 KB |
Output is correct |
37 |
Correct |
13 ms |
24664 KB |
Output is correct |
38 |
Correct |
10 ms |
22620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
5 ms |
18780 KB |
Output is correct |
5 |
Correct |
7 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
19036 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
5 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
19036 KB |
Output is correct |
10 |
Correct |
4 ms |
18776 KB |
Output is correct |
11 |
Correct |
4 ms |
18944 KB |
Output is correct |
12 |
Correct |
4 ms |
18776 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
5 ms |
19052 KB |
Output is correct |
16 |
Correct |
4 ms |
19036 KB |
Output is correct |
17 |
Correct |
5 ms |
19036 KB |
Output is correct |
18 |
Correct |
6 ms |
19036 KB |
Output is correct |
19 |
Correct |
132 ms |
61252 KB |
Output is correct |
20 |
Correct |
118 ms |
62408 KB |
Output is correct |
21 |
Incorrect |
116 ms |
55152 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
19036 KB |
Output is correct |
2 |
Correct |
4 ms |
18780 KB |
Output is correct |
3 |
Correct |
4 ms |
18780 KB |
Output is correct |
4 |
Correct |
5 ms |
18780 KB |
Output is correct |
5 |
Correct |
7 ms |
18780 KB |
Output is correct |
6 |
Correct |
5 ms |
19036 KB |
Output is correct |
7 |
Correct |
4 ms |
18780 KB |
Output is correct |
8 |
Correct |
5 ms |
18780 KB |
Output is correct |
9 |
Correct |
5 ms |
19036 KB |
Output is correct |
10 |
Correct |
4 ms |
18776 KB |
Output is correct |
11 |
Correct |
4 ms |
18944 KB |
Output is correct |
12 |
Correct |
4 ms |
18776 KB |
Output is correct |
13 |
Correct |
4 ms |
19036 KB |
Output is correct |
14 |
Correct |
4 ms |
19036 KB |
Output is correct |
15 |
Correct |
5 ms |
19052 KB |
Output is correct |
16 |
Correct |
4 ms |
19036 KB |
Output is correct |
17 |
Correct |
5 ms |
19036 KB |
Output is correct |
18 |
Correct |
6 ms |
19036 KB |
Output is correct |
19 |
Correct |
4 ms |
18780 KB |
Output is correct |
20 |
Correct |
4 ms |
18800 KB |
Output is correct |
21 |
Correct |
6 ms |
19288 KB |
Output is correct |
22 |
Correct |
5 ms |
19036 KB |
Output is correct |
23 |
Correct |
5 ms |
19036 KB |
Output is correct |
24 |
Correct |
5 ms |
19036 KB |
Output is correct |
25 |
Correct |
6 ms |
19476 KB |
Output is correct |
26 |
Correct |
5 ms |
19036 KB |
Output is correct |
27 |
Correct |
6 ms |
19036 KB |
Output is correct |
28 |
Correct |
6 ms |
19288 KB |
Output is correct |
29 |
Correct |
5 ms |
19292 KB |
Output is correct |
30 |
Correct |
7 ms |
19288 KB |
Output is correct |
31 |
Correct |
6 ms |
19548 KB |
Output is correct |
32 |
Correct |
6 ms |
19548 KB |
Output is correct |
33 |
Correct |
7 ms |
19848 KB |
Output is correct |
34 |
Correct |
7 ms |
20316 KB |
Output is correct |
35 |
Correct |
9 ms |
20608 KB |
Output is correct |
36 |
Correct |
10 ms |
20828 KB |
Output is correct |
37 |
Correct |
13 ms |
24664 KB |
Output is correct |
38 |
Correct |
10 ms |
22620 KB |
Output is correct |
39 |
Correct |
132 ms |
61252 KB |
Output is correct |
40 |
Correct |
118 ms |
62408 KB |
Output is correct |
41 |
Incorrect |
116 ms |
55152 KB |
Output isn't correct |
42 |
Halted |
0 ms |
0 KB |
- |