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"
#define MAX_N 500000
#define ff first
#define ss second
using namespace std;
const int M = 2e5 + 5;
vector<pair<int, int>> adj[M];
int vis[M], dis[M], edge[M], dp[M][105];
int answer = INT_MAX, Ka;
void dfs(int nd, int pr = -1){
dp[nd][0] = 0;
for(int i = 1; i <= Ka; ++i)
dp[nd][i] = 1e9;
for(auto &i : adj[nd])
if(i.ff != pr){
dfs(i.ff, nd);
// jogaby tazele
for(int a1 = 0; a1 + i.ss <= Ka; ++a1){
int a2 = Ka - a1 - i.ss;
answer = min(answer, dp[nd][a1] + dp[i.ff][a2] + 1);
}
// dp-ni tazele
for(int k = 0; k + i.ss <= Ka; ++k)
dp[nd][k+i.ss] = min(dp[nd][k+i.ss], dp[i.ff][k] + 1);
}
}
int best_path(int n, int k, int H[][2], int L[]){
Ka = k;
for(int i = 0; i < n - 1; ++i)
adj[H[i][0]].push_back({H[i][1], L[i]}), adj[H[i][1]].push_back({H[i][0], L[i]});
if(n <= 1e3 + 5){
queue<int> q;
for(int ad = 0; ad < n; ++ad){
for(int i = 0; i <= n; ++i)
vis[i] = 0, dis[i] = 0, edge[i] = 0;
while(!q.empty())
q.pop();
q.push(ad);
vis[ad] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
for(auto &i : adj[x])
if(!vis[i.ff]){
vis[i.ff] = 1;
q.push(i.ff);
dis[i.ff] = dis[x] + i.ss;
edge[i.ff] = edge[x] + 1;
}
}
// dis[i] = ad-den i-e cenli uzaklyk
// tree-da dine bir path bar
for(int i = 0; i < n; ++i)
if(dis[i] == k)
answer = min(answer, edge[i]);
}
}
else if(k <= 100)
dfs(1);
return (answer == INT_MAX ? -1 : answer);
assert(false);
}
//static int N, K;
//static int H[MAX_N][2];
//static int L[MAX_N];
//static int solution;
//
//inline
//void my_assert(int e) {if (!e) abort();}
//
//void read_input()
//{
// int i;
// my_assert(2==scanf("%d %d",&N,&K));
// for(i=0; i<N-1; i++)
// my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
// my_assert(1==scanf("%d",&solution));
//}
//
//int main(){
// int ans;
// read_input();
// ans = best_path(N,K,H,L);
// if(ans==solution)
// printf("Correct.\n");
// else
// printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);
//
// return 0;
//}
# | 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... |