이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "race.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <iomanip>
#include <cmath>
#include <limits>
#include <map>
#include <utility>
#include <cctype>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <functional>
#include <iterator>
using namespace std;
#define OVL(x,s) for(auto y:x) cout<<y<<s; cout<<"\n";
void dbg_out() { cout << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cout << ' ' << H; dbg_out(T...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#define endl "\n"
#define pb push_back
#define F first
#define S second
#define ll long long
#define mod 1000000007
#define all(v) v.begin(),v.end()
const int MAXN=100+100;
const int MAXT=200;
const int INF=1e9;
// vector<vector<int>> arr(MAXN,vector<int>(MAXT,INF));
vector<pair<int,int>> v[MAXN];
int n,k;
int depth[MAXN];
map<int,int> mp[MAXN];
int ans=INF;
int dfs(int node,int par){
if(v[node].size()==1&&v[node][0].F==par) return -1;
// return -1;
// mp[node].clear();
mp[node][0]=0;
for(auto x:v[node]){
if(x.F==par) continue;
int a=dfs(x.F,node);
// dbg(x.F,node,a);
if(a==-1){
// dbg(x.F,node);
mp[node][x.S]=1;
}else{
// dbg(node);
map<int,int> newmp;
for(auto & [key,value]:mp[x.F]){
if(key==0) continue;
newmp[key+x.S]=value+1;
}
for(auto &[key,value]:newmp){
int target=k-key;
// dbg(key,value,target,mp[node].count(target));
if(target<0) continue;
if(mp[node].count(target)){
ans=min(ans,mp[node][target]+value);
}
}
for(auto [key,val]:mp[x.F]){
if(mp[node].count(key+x.S))
mp[node][key+x.S]=min(mp[node][key+x.S],val+1);
else mp[node][key+x.S]=val+1;
}
}
}
return 1;
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N;
k=K;
for(int i=0;i<n-1;i++){
int a=H[i][0];
int b=H[i][1];
int c=L[i];
v[a].pb({b,c});
v[b].pb({a,c});
}
dfs(0,0);
// dbg(ans);
if(ans==INF)
return -1;
return ans;
}
#ifdef IOI
#define MAX_N 500000
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;
}
#endif
# | 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... |