-
Notifications
You must be signed in to change notification settings - Fork 3
/
AasanHai.cpp
74 lines (60 loc) · 1.14 KB
/
AasanHai.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Author - Kevin Mathew
// Birla Institute of Technology, Mesra
ll n, b[500010], vis[500010];
vector<ll> tree[500010];
vector<ll> levels[500010];
void te(){
cin >> n;
for(ll i=1;i<=n;i++){
vis[i] = 0;
char c;
cin >> c;
if(c == '(')
b[i] = 0;
else
b[i] = 1;
}
for(ll i=0;i<n-1;i++){
ll u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
queue<pair<ll, ll> > q;
q.push(make_pair(1, 0));
while(!q.empty()){
pair<ll, ll> p = q.front(); q.pop();
ll u = p.first;
ll l = p.second;
vis[u] = 1;
levels[l].push_back(u);
for(ll v : tree[u]){
if(vis[v] != 1)
q.push(make_pair(v, l + 1));
}
}
ll ans = 0;
for(ll i=0;levels[i].size()>0;i++){
ll l = 0, r = 0;
for(ll j=0;j<levels[i].size();j++)
if(b[levels[i][j]] == 0)
l++;
else
r++;
ans += (min(l, r));
}
cout << ans << "\n";
}
int main()
{
freopen("input.txt", "r", stdin); //Comment
freopen("output.txt", "w", stdout); //this out.
ios::sync_with_stdio(false); //Not
cin.tie(NULL); //this.
cout.tie(0); //or this.
te();
return 0;
}