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
#define left (node<<1),l,(l+r)>>1
#define right ((node<<1)|1),((l+r)>>1) + 1,r
using namespace std;
const int N = 300005;
int A[N],S[N],Q,dp[N],t[4*N],n;
ll res;
vector <int> v[N];
map <int,int> last;
void upd(int node,int l,int r,int idx,int val){
if (l == r) {t[node] = val; return;}
if (idx > ((l+r)>>1)) upd(right,idx,val);
else upd(left,idx,val);
t[node] = min(t[node<<1],t[((node<<1)|1)]);
}
int get(int node,int l,int r,int L,int R){
if (r < L || R < l) return 1e9;
if (L <= l && r <= R) return t[node];
int x = get(left,L,R),y = get(right,L,R);
return min(x,y);
}
void dfs(int x,int par){
A[x] = A[par] + S[x];
upd(1,1,n,x,A[x]);
if (last[A[x]]){
int id = last[A[x]];
int mn = get(1,1,n,id + 1,x) - A[x];
if (mn == 0) dp[x] = dp[id] + 1,res+=dp[x];
}
else if (A[x] == 0){
int mn = get(1,1,n,1,x);
if (mn == 0) dp[x] = 1,res+=dp[x];
}
last[A[x]] = x;
for (int j=0;j<v[x].size();j++)
if (v[x][j]!=par) dfs(v[x][j],x);
}
void dfs2(int x,int par){
A[x] = A[par] + S[x];
upd(1,1,n,x,A[x]);
if (last[A[x]]){
int id = last[A[x]];
int mn = get(1,1,n,x,id-1) - A[x];
if (mn == 0) dp[x] = dp[id] + 1,res+=dp[x];
}
else if (A[x] == 0){
int mn = get(1,1,n,x,n);
if (mn == 0) dp[x] = 1,res+=dp[x];
}
last[A[x]] = x;
for (int j=0;j<v[x].size();j++)
if (v[x][j]!=par) dfs2(v[x][j],x);
}
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);
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);
}
if (n <= 1000){
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;
return 0;
}
for (int i=1;i<=4*N;i++)
t[i] = 1e9;
dfs(1,1);
for (int i=1;i<=4*N;i++)
t[i] = 1e9;
for (int i=1;i<=n;i++)
dp[i] = 0,A[i]=0;
last.clear();
dfs2(n,n);
cout<<res;
}
Compilation message (stderr)
zagrade.cpp: In function 'void dfs(int, int)':
zagrade.cpp:38:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for (int j=0;j<v[x].size();j++)
| ~^~~~~~~~~~~~
zagrade.cpp: In function 'void dfs2(int, int)':
zagrade.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int j=0;j<v[x].size();j++)
| ~^~~~~~~~~~~~
zagrade.cpp: In function 'void DFS(int, int)':
zagrade.cpp:62:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int j=0;j<v[x].size();j++){
| ~^~~~~~~~~~~~
zagrade.cpp: In function 'int main()':
zagrade.cpp:94:14: warning: iteration 1200019 invokes undefined behavior [-Waggressive-loop-optimizations]
94 | t[i] = 1e9;
| ~~~~~^~~~~
zagrade.cpp:93:19: note: within this loop
93 | for (int i=1;i<=4*N;i++)
| ~^~~~~
zagrade.cpp:98:14: warning: iteration 1200019 invokes undefined behavior [-Waggressive-loop-optimizations]
98 | t[i] = 1e9;
| ~~~~~^~~~~
zagrade.cpp:97:19: note: within this loop
97 | for (int i=1;i<=4*N;i++)
| ~^~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |