Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- vector<vector<int>> adj;
- // costs array
- vector<double> C;
- vector<double> dp;
- //pV is parent of node V
- void dfs(int V, int pV){
- //traverse over all children
- double sum=0;
- double count=0;
- for(auto son: adj[V]){
- if(son == pV) continue;
- dfs(son, V);
- sum+=dp[son];
- count++;
- }
- if(count>0)
- sum/=count;
- sum+=C[V];
- dp[V]=sum;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin>>n;
- adj=vector<vector<int>> (n);
- C=vector<double> (n,0.0);
- for(int i=0;i<n-1;i++){
- int x,y;
- cin>>x>>y;
- adj[x].push_back(y);
- adj[y].push_back(x);
- }
- for(int i=0;i<n;i++){
- double cost_Vi;
- cin>>cost_Vi;
- C[i]=cost_Vi;
- }
- int v_strat_best=0;
- double min_cost=100000000;
- for(int i=0;i<n;i++)
- {
- dp=vector<double> (n,0.0);
- dfs(i,-1);
- if(dp[i]<min_cost)
- {
- v_strat_best=i;
- min_cost=dp[i];
- }
- }
- cout << v_strat_best <<" "<< min_cost;
- }
- /*
- input:
- 4
- 0 1
- 1 2
- 1 3
- 1
- 1
- 1
- 100
- output:
- 1 35
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement