Submission #419605

#TimeUsernameProblemLanguageResultExecution timeMemory
419605keta_tsimakuridzeZagrade (COI17_zagrade)C++14
100 / 100
1520 ms52008 KiB
#include<bits/stdc++.h> #define f first #define s second #define int long long using namespace std; const int N=3e5+5,mod=1e9+7; int t,ans,sz[N],c[N],fix[N],n,centroid,ans_; vector<int>V[N]; map<int,int> cnt; void get_szs(int u,int p){ sz[u] = 1; for(int i=0;i<V[u].size();i++){ if(V[u][i]==p || fix[V[u][i]]) continue; get_szs(V[u][i],u); sz[u] += sz[V[u][i]]; } } int find_centroid(int u,int p,int Sz){ for(int i=0;i<V[u].size();i++){ if(V[u][i]==p || fix[V[u][i]]) continue; if(sz[V[u][i]] >Sz/2) return find_centroid(V[u][i],u,Sz); } return u; } void half2(int u,int p,int cur,int mn,int x,int y) { cur += c[u]; x+=(c[u]==1); mn = min(mn,cur); y+=(c[u]==-1); if(y-x >= abs(mn))ans += cnt[y-x]; if(mn+c[centroid] >= 0 && cur+c[centroid] == 0 ) ans_++; for(int i=0;i<V[u].size();i++){ if(V[u][i] == p || fix[V[u][i]]) continue; half2(V[u][i],u,cur,mn,x,y); } } void half1(int u,int p,int cur,int mx,int x,int y) { cur += c[u]; x+=(c[u]==1); mx = max(mx,cur); y+=(c[u]==-1); if(cur-mx >= 0) cnt[x-y]++; if(cur-mx >= 0 && cur==0) ans_++; for(int i=0;i<V[u].size();i++) { if(V[u][i] == p || fix[V[u][i]]) continue; half1(V[u][i],u,cur,mx,x,y); } } void centroid_decomp(int u){ get_szs(u,0); int C = find_centroid(u,0,sz[u]); fix[C] = 1; centroid=C; cnt.clear(); ans_ = 0; for(int i=0;i<V[C].size();i++){ if(fix[V[C][i]]) continue; half2(V[C][i],C,0,0,0,0); half1(V[C][i],C,c[C],max(0ll,c[C]),c[C] == 1,c[C]==-1); } reverse(V[C].begin(),V[C].end()); cnt.clear(); for(int i=0;i<V[C].size();i++){ if(fix[V[C][i]]) continue; half2(V[C][i],C,0,0,0,0); half1(V[C][i],C,c[C],max(0ll,c[C]),c[C] == 1,c[C]==-1); } ans += ans_/2; for(int i=0;i<V[C].size();i++){ if(fix[V[C][i]]) continue; centroid_decomp(V[C][i]); } } main(){ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n; for(int i=1;i<=n;i++){ char ch; cin>>ch; if(ch=='(') c[i] = 1; else c[i] = -1; } for(int i=2;i<=n;i++){ int u,v; cin>>u>>v; V[u].push_back(v); V[v].push_back(u); } centroid_decomp(1); cout<<ans; }

Compilation message (stderr)

zagrade.cpp: In function 'void get_szs(long long int, long long int)':
zagrade.cpp:12:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
zagrade.cpp: In function 'long long int find_centroid(long long int, long long int, long long int)':
zagrade.cpp:19:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
zagrade.cpp: In function 'void half2(long long int, long long int, long long int, long long int, long long int, long long int)':
zagrade.cpp:34:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |  for(int i=0;i<V[u].size();i++){
      |              ~^~~~~~~~~~~~
zagrade.cpp: In function 'void half1(long long int, long long int, long long int, long long int, long long int, long long int)':
zagrade.cpp:48:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |  for(int i=0;i<V[u].size();i++) {
      |              ~^~~~~~~~~~~~
zagrade.cpp: In function 'void centroid_decomp(long long int)':
zagrade.cpp:61:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |  for(int i=0;i<V[C].size();i++){
      |              ~^~~~~~~~~~~~
zagrade.cpp:68:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   68 |  for(int i=0;i<V[C].size();i++){
      |              ~^~~~~~~~~~~~
zagrade.cpp:73:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |  for(int i=0;i<V[C].size();i++){
      |              ~^~~~~~~~~~~~
zagrade.cpp: At global scope:
zagrade.cpp:78:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   78 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...