Advertisement
Fahim_7861

sofdor-ali-s-black-and-white-tree

Jan 16th, 2021
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef unsigned long long ull;
  5. typedef pair<ll,ll>pll;
  6. typedef pair<ll,pair<ll,ll>>plll;
  7. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
  8. #define vll(v) v.begin(),v.end()
  9. #define all(x) x.rbegin(),x.rend()
  10. #define min3(a, b, c) min(a, min(b, c))
  11. #define max3(a, b, c) max(a, max(b, c))
  12. #define F first
  13. #define S second
  14. #define in freopen("input.txt", "r", stdin)
  15. #define out freopen("output.txt", "w", stdout)
  16. #define minheap int,vector<int>,greater<int>
  17. #define pb push_back
  18. #define eb emplace_back
  19. #define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
  20. #define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
  21. #define bug cout<<"BUG"<<endl;
  22. const int Max = 2e6 + 10;
  23. const int Mod = 1e9 + 7;
  24. const double PI =3.141592653589793238463;
  25. bool compare(const pair<ll,ll> &a, const pair<ll,ll> &b)
  26. {
  27. return (a.first > b.first);
  28. }
  29. ll lcm(ll a,ll b)
  30. {
  31. if(a==0 || b==0)return 0;
  32.  
  33. return a/__gcd(a,b)*b;
  34. }
  35.  
  36. void input(ll ara[],ll n)
  37. {
  38. for(ll i=0; i<n; i++)cin>>ara[i];
  39. }
  40. void print(ll ara[],ll n)
  41. {
  42. for(ll i=0; i<n; i++)
  43. cout<<ara[i]<<" ";
  44. cout<<endl;
  45. }
  46. typedef struct{
  47.  
  48. ll black,white;
  49. }Node;
  50.  
  51. vector<ll>v[Max+10];
  52.  
  53.  
  54. Node dp[Max+10];
  55.  
  56. ll mx=0;
  57.  
  58. ll cost[Max+10];
  59. void dfs(ll s,ll p)
  60. {
  61.  
  62. for(auto x : v[s])
  63. {
  64. if(x!=p)
  65. {
  66. dfs(x,s);
  67.  
  68. dp[s].black+=dp[x].black;
  69. dp[s].white+=dp[x].white;
  70. }
  71. }
  72.  
  73. dp[s].black+=(cost[s]==0);
  74. dp[s].white+=(cost[s]==1);
  75.  
  76. mx=max(mx,dp[s].black-dp[s].white);
  77.  
  78. return ;
  79. }
  80.  
  81. int main()
  82. {
  83.  
  84. fastread();
  85.  
  86. ll i,j,n,m,p,a,sum=0,k,t,b,c,d,cnt=0,q,l,r,ans=0;
  87.  
  88. bool flag=false;
  89.  
  90. string str;
  91.  
  92. cin>>n;
  93.  
  94. for(i=0; i<n; i++) cin>>cost[i];
  95.  
  96. for(i=1; i<n; i++)
  97. {
  98. cin>>a>>b;
  99.  
  100. v[a].eb(b);
  101.  
  102. v[b].eb(a);
  103. }
  104.  
  105. dfs(0,0);
  106.  
  107. cout<<dp[0].white+mx<<endl;
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. }
  119.  
  120. //problem link : https://toph.co/p/sofdor-ali-s-black-and-white-tree
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement