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;
const int INF = 1e9;
vector<pair<int, int>> adj[M];
int vis[M], dis[M], edge[M], dp[M][105];
int answer = INF, Ka;
void dfs(int nd, int pr = -1){
dp[nd][0] = 0;
for(int i = 1; i <= Ka; ++i)
dp[nd][i] = INF;
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 == INF ? -1 : answer);
assert(false);
}
//int H[MAX_N][2], L[MAX_N], solution;
//
//int main(){
// int n, k;
// scanf("%d%d", &n, &k);
// for(int i = 0; i < n - 1; ++i) scanf("%d%d%d", &H[i][0], &H[i][1], &L[i]);
// scanf("%d", &solution);
// int answer = best_path(n, k, H, L);
// if(answer == solution)
// puts("Correct.");
// else
// printf("Incorrect. Returned %d, Expected %d.", answer, 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... |