답안 #714885

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
714885 2023-03-25T11:46:32 Z Ahmed57 Zagrade (COI17_zagrade) C++14
100 / 100
1231 ms 45956 KB
#include <bits/stdc++.h>

using namespace std;
#define int long long
long long sz[300007];long long all = 0;
long long vis[300007];
vector<int> adj[300007];
int ss = 0;
long long zz[300007];
string s;
long long c = 0;
void calc(int no,int pr,int ch,int bra){
    if(ss==0){
        if(s[no-1]=='('){bra++;ch--;ch=max(ch,0LL);}
        if(s[no-1]==')'){bra--;ch++;}
        if(bra>=0&&ch==0)zz[bra]++;
    }else if(ss==1){
        if(s[no-1]=='('){bra++;ch++;}
        if(s[no-1]==')'){bra--;ch--;ch=max(ch,0LL);}
        if(bra<=0&&ch==0){
            all+=zz[-bra];
        }
        //if(bra==0&&ch==0)c++;
    }else{
        if(s[no-1]=='('){bra++;ch--;ch=max(ch,0LL);}
        if(s[no-1]==')'){bra--;ch++;}
        if(bra>=0&&ch==0)zz[bra]--;
    }
    for(auto i:adj[no]){
        if(i==pr||vis[i])continue;
        calc(i,no,ch,bra);
    }
}
int dfs(int no,int pr){
    sz[no] = 1;
    for(auto i:adj[no]){
        if(i==pr||vis[i]!=0)continue;
        sz[no]+=dfs(i,no);
    }
    return sz[no];
}
int get_centroid(int no,int ss,int pr){
    for(auto i:adj[no]){
        if(pr==i||vis[i]!=0)continue;
        if(sz[i]*2>ss){
            return get_centroid(i,ss,no);
        }
    }
    return no;
}
void centroid(int no){
    int cen = get_centroid(no,dfs(no,-1),-1);
    for(int i = 0;i<=max(1000LL,sz[cen]+5);i++){
        zz[i] = 0;
    }
    zz[0] = 1;
    vis[cen] = 1;
    for(auto i:adj[cen]){
        if(vis[i])continue;
        ss= 0;
        calc(i,-1,0,0);
    }
    if(s[cen-1]==')')all+=zz[1];
    for(auto i:adj[cen]){
        if(vis[i])continue;
        ss = 2;
        calc(i,-1,0,0);
        ss = 1;
        calc(i,-1,(s[cen-1]=='('?1:0),(s[cen-1]=='('?1:-1));
        //cout<<all<<" "<<i<<endl;
        ss = 0;
        calc(i,-1,0,0);
    }
    for(auto i:adj[cen]){
        if(vis[i])continue;
        centroid(i);
    }
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;
    cin>>n>>s;
    for(int i = 0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    centroid(1);
    cout<<all<<endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 5 ms 7388 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 5 ms 7388 KB Output is correct
7 Correct 5 ms 7364 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 4 ms 7388 KB Output is correct
10 Correct 4 ms 7372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 451 ms 41800 KB Output is correct
2 Correct 437 ms 41664 KB Output is correct
3 Correct 483 ms 41604 KB Output is correct
4 Correct 470 ms 41620 KB Output is correct
5 Correct 524 ms 41660 KB Output is correct
6 Correct 477 ms 41640 KB Output is correct
7 Correct 490 ms 41588 KB Output is correct
8 Correct 560 ms 41684 KB Output is correct
9 Correct 585 ms 41624 KB Output is correct
10 Correct 487 ms 41668 KB Output is correct
11 Correct 436 ms 41600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 5 ms 7380 KB Output is correct
3 Correct 5 ms 7388 KB Output is correct
4 Correct 5 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 5 ms 7388 KB Output is correct
7 Correct 5 ms 7364 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 4 ms 7388 KB Output is correct
10 Correct 4 ms 7372 KB Output is correct
11 Correct 451 ms 41800 KB Output is correct
12 Correct 437 ms 41664 KB Output is correct
13 Correct 483 ms 41604 KB Output is correct
14 Correct 470 ms 41620 KB Output is correct
15 Correct 524 ms 41660 KB Output is correct
16 Correct 477 ms 41640 KB Output is correct
17 Correct 490 ms 41588 KB Output is correct
18 Correct 560 ms 41684 KB Output is correct
19 Correct 585 ms 41624 KB Output is correct
20 Correct 487 ms 41668 KB Output is correct
21 Correct 436 ms 41600 KB Output is correct
22 Correct 1042 ms 28852 KB Output is correct
23 Correct 972 ms 28884 KB Output is correct
24 Correct 1120 ms 29068 KB Output is correct
25 Correct 1231 ms 29932 KB Output is correct
26 Correct 706 ms 34552 KB Output is correct
27 Correct 630 ms 31844 KB Output is correct
28 Correct 624 ms 30824 KB Output is correct
29 Correct 419 ms 45880 KB Output is correct
30 Correct 410 ms 45880 KB Output is correct
31 Correct 144 ms 28484 KB Output is correct
32 Correct 377 ms 45956 KB Output is correct