제출 #495681

#제출 시각아이디문제언어결과실행 시간메모리
495681Pietra경주 (Race) (IOI11_race)C++14
0 / 100
2 ms4940 KiB
#include<bits/stdc++.h>
using namespace std ; 
#include "race.h"

const int maxn = 2e5 + 5 ; 
const int inf = 1e9 ; 

int k, f[maxn], mark[maxn], n, ans, sz[maxn] ; 
vector<pair<int,int>> grafo[maxn] ; 

void dfs(int v, int p){

	sz[v] = 1 ; 

	for(auto a : grafo[v]){
		if(a.first == p) continue ; 
		dfs(a.first, v) ; 
		sz[v] += sz[a.first] ; 
	}

}

int find_cent(int v, int p, int szz){

	for(auto a : grafo[v]){
		if(a.first == p || mark[a.first] || 2*sz[a.first] <= szz) continue ; 
		find_cent(a.first, v, szz) ; 
	}

	return v ; 

}

void make_count(bool type, int v, int p, int dist, int lvl){

	if(dist > k) return ; 

	if(v == 0) f[dist] = inf ; 
	else if(type == 0) ans = min(ans, f[k-dist] + lvl) ;
	else f[dist] = min(f[dist], lvl) ;  

	for(auto a : grafo[v]){
		if(a.first == p || mark[a.first]) continue ; 
		make_count(type, a.first, v, dist + a.second, lvl + 1) ; 
	}

}

void make_graph(int v, int p){

	dfs(v, p) ; 

	int c = find_cent(v, p, sz[v]) ; 

	mark[c] = 1 ; 

	f[0] = 1 ; 

	for(auto a : grafo[c]){
		if(a.first == p || mark[a.first]) continue ;
		make_count(0, a.first, c, a.second, 1) ; 
	}

	for(auto a : grafo[c]){
		if(a.first == p || mark[a.first]) continue ;; 
		make_count(1, a.first, c, a.second, 1) ; 
	}

	for(auto a : grafo[c]){
		if(a.first == p || mark[a.first]) continue ;
		make_graph(a.first, c) ; 
	}

}

int best_path(int N, int K, int H[maxn][2], int L[maxn]) {
  
  for(int i = 1 ; i <= N ; i++){
  	H[i][0]++, H[i][1]++ ; 
  	grafo[H[i][0]].push_back({H[i][1], L[i]}) ; grafo[H[i][1]].push_back({H[i][0], L[i]}) ; 
  }

  k = K ; 

  for(int i = 1 ; i <= K ; i++) f[i] = inf ; 
  ans = inf ; 
  make_graph(1, 0) ; 
  
  return (ans == inf ? -1 : ans) ; 

}

// int32_t main(){

// 	ans = inf ; 

// 	cin >> n >> K ; 

// 	for(int i = 1 ; i < n ; i++){
// 		int a, b, c ; 
// 		cin >> a >> b >> c ; 
// 		a++, b++ ; 
// 		grafo[a].push_back({b, c}), grafo[b].push_back({a, c}) ; 
// 	}

// 	for(int i = 1 ; i <= K ; i++) f[i] = inf ; 

//     make_graph(1, 0) ; 
  
//     cout << ans << "\n" ; 

// }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...