# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
419445 | NintsiChkhaidze | Zagrade (COI17_zagrade) | C++14 | 3032 ms | 26224 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
const int N = 300005;
int A[N],S[N];
ll res;
vector <int> v[N];
void dfs(int x,int par){
A[x] = A[par] + S[x];
if (A[x] == 0) res++;
if (A[x] < 0) return;
for (int j=0;j<v[x].size();j++){
int to = v[x][j];
if (to!=par && A[to] >= 0) dfs(to,x);
}
}
int main (){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
string s;
cin>>s;
s = '$' + s;
for (int i=1;i<=n;i++)
if (s[i] == '(') S[i] = 1;
else S[i]= -1;
for (int i=1;i<n;i++){
int a,b;
cin>>a>>b;
v[a].pb(b),v[b].pb(a);
}
for (int i =1;i<=n;i++){
if (S[i] == 1) dfs(i,i);
for (int j=1;j<=n;j++)
A[j] = 0;
}
cout<<res;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |