SHARE
TWEET

Untitled

a guest Oct 15th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7. typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
  8.  
  9. #define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
  10. char _;
  11. #define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
  12. #define all(a) a.begin(),a.end()
  13. #define println printf("\n");
  14. #define readln(x) getline(cin,x);
  15. #define pb push_back
  16. #define endl "\n"
  17. #define INT_INF 0x3f3f3f3f
  18. #define LL_INF 0x3f3f3f3f3f3f3f3f
  19. #define MOD 1000000007
  20. #define MOD2 1494318097
  21. #define SEED 131
  22. #define mp make_pair
  23. #define fastio cin.tie(0); cin.sync_with_stdio(0);
  24.  
  25. #define MAXN 3005
  26.  
  27. typedef unsigned long long ull;
  28. typedef long long ll;
  29. typedef long double ld;
  30. typedef unordered_map<int,int> umii;
  31. typedef pair<int,int> pii;
  32. typedef pair<double,double> pdd;
  33. typedef pair<ll,ll> pll;
  34. typedef pair<int,pii> triple;
  35. typedef int8_t byte;
  36.  
  37. mt19937 g1(time(0));
  38.  
  39. int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);}
  40. ll randlong(ll a,ll b){return uniform_int_distribution<long long>(a, b)(g1);}
  41.  
  42. ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
  43. ll lcm(ll a, ll b){return a*b/gcd(a,b);}
  44. ll fpow(ll  b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
  45. ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}
  46.  
  47. int num_nodes,vals[MAXN];
  48. ll sz[MAXN],dp[MAXN];
  49. ll res=LLONG_MAX;
  50. vector<int> connections[MAXN];
  51. vector<pii> edges;
  52.  
  53. void init(int node, int prev){
  54.     sz[node] = vals[node];
  55.     for(int check:connections[node]){
  56.         if(check == prev) continue;
  57.         init(check,node);
  58.         sz[node]+=sz[check];
  59.         dp[node]+=dp[check]+sz[check];
  60.     }
  61. }
  62.  
  63. void solve(int node, int prev, ll &tmp){
  64.     tmp = min(tmp,dp[node]);
  65.     ll odp = dp[node], osz = sz[node];
  66.     for(int check:connections[node]){
  67.         if(check == prev) continue;
  68.         ll odp2 = dp[check], osz2 = sz[check];
  69.  
  70.         dp[node]-=(dp[check]+sz[check]);
  71.         sz[node]-=sz[check];
  72.  
  73.         dp[check]+=dp[node]+sz[node];
  74.         sz[check]+=sz[node];
  75.  
  76.         solve(check,node,tmp);
  77.  
  78.         dp[check] = odp2, sz[check] = osz2;
  79.         dp[node] = odp, sz[node] = osz;
  80.     }
  81. }
  82.  
  83. int main(){
  84.     scanf("%d",&num_nodes);
  85.     for(int i=1; i<=num_nodes; i++)
  86.         scanf(" %d",&vals[i]);
  87.     for(int i=1; i<num_nodes; i++){
  88.         int a,b; scanf(" %d %d",&a,&b);
  89.         connections[a].pb(b);
  90.         connections[b].pb(a);
  91.         edges.pb(mp(a,b));
  92.     }
  93.     for(pii check:edges){
  94.         memset(dp,0,sizeof dp);
  95.         int a = check.first, b = check.second;
  96.         ll f = LLONG_MAX/10, s = LLONG_MAX/10;
  97.         init(a,b); solve(a,b,f);
  98.         init(b,a); solve(b,a,s);
  99.         res = min(res,f+s);
  100.     }
  101.     printf("%lld\n",res);
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top