This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
ll t = 1;
const ll M = 1e9 + 7;
ll mod_add(ll a, ll b){
return ((a%M) + (b %M))%M;
}
ll mod_mul(ll a, ll b){
return ((a%M)*(b%M))%M;
}
const ll MAX = 1e18;
ll lcm(ll a , ll b){
return (a*b)/__gcd(a,b);
}
int dfs(vector<pair<int,int>> adj[], int l[], int s , int p , int &k,
map<int,int> min_length[] , int &n){
int ans = n;
for(auto x:adj[s]){
int u = x.first, e = l[x.second];
if(u != p){
min_length[s][e] = 1;
int res = dfs(adj , l , u , s , k,min_length, n);
ans = min(ans , res);
for(auto path: min_length[u]){
int cost = path.first + e;
int length = path.second + 1;
if(min_length[s].find(cost) == min_length[s].end()){
min_length[s][cost] = length;
}
else{
min_length[s][cost] = min(min_length[s][cost] , length);
}
if(cost == k){
ans = min(ans , length);
}
}
min_length[u].clear();
}
}
for(auto path:min_length[s]){
int cost = path.first ,length = path.second;
if(min_length[s].find(k - cost) != min_length[s].end()){
ans = min(ans , length + min_length[s][k - cost]);
}
}
return ans;
}
int best_path(int n, int k , int h[][2], int l[]){
map<int,int> min_length[n];
vector<pair<int,int>> adj[n];
for(int i = 0;i<(n-1) ; i++){
adj[h[i][0]].push_back({h[i][1],i});
adj[h[i][1]].push_back({h[i][0],i});
}
int ans = dfs(adj , l, 0, -1,k,min_length,n);
return (ans==n)? -1:ans;
}
// void solve(){
// int n , k ; cin>>n>>k;
// int h[n-1][2];
// int l[n-1];
// for(int i = 0;i<(n -1);i++){
// cin>>h[i][0]>>h[i][1];
// }
// for(int i = 0 ; i<(n - 1); i++){
// cin>>l[i];
// }
// cout<<best_path(n, k , h , l);
// }
// int main(){
// freopen("input.txt" , "r" , stdin);
// freopen("output.txt" , "w", stdout);
// cin>>t;
// while(t--){
// solve();
// }
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |