답안 #334578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
334578 2020-12-09T12:56:16 Z Atill83 Zagrade (COI17_zagrade) C++14
100 / 100
1477 ms 52980 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
using namespace std;
const long long INF = (long long) 1e18;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n;
string s;
vector<int> adj[MAXN];
bool erased[MAXN];
int sz[MAXN];
ll ans = 0;
unordered_map<int, int> mp;
void dfs(int v, int pr = -1){
    sz[v] = 1;
    for(int j: adj[v]){
        if(j != pr && !erased[j]){
            dfs(j, v);
            sz[v] += sz[j];
        }
    }
}

int cent(int v){
    int siz = sz[v];
    int pr = -1;
    bool cont = 1;
    while(cont){
        cont = 0;
        for(int j: adj[v]){
            if(erased[j] || j == pr) continue;
            if(2*sz[j] > siz){
                pr = v;
                v = j;
                cont = 1;
                break;
            }
        }
    }
    return v;

}

void upd(int v, int par, int val, int pre = 0, int tot = 0){
    if(s[v] == ')'){
        pre--;
        tot--;
    }else{
        pre++;
        tot = min(0, tot + 1);
    }

    //cerr<<v<<" "<<pre<<" "<<tot<<endl;
    if(tot == 0 && pre >= 0){
        mp[pre] += val;
    }

    for(int j: adj[v]){
        if(j != par && !erased[j]){
            upd(j, v, val, pre, tot);
        }
    }
}

void add(int v, int par, int pre = 0, int tot = 0){
    if(s[v] == ')'){
        pre++;
        tot = max(0, tot - 1);
    }else{
        pre--;
        tot++;
    }

    if(tot == 0 && pre >= 0){
        ans += mp[pre];
    }

    for(int j: adj[v]){
        if(!erased[j] && j != par){
            add(j, v, pre, tot);
        }
    }
}




void do_it(int v){
    dfs(v);
    v = cent(v);
    //cerr<<v<<endl;
    upd(v, -1, 1);
    ans += mp[0];
    int pr, tt;
    if(s[v] == ')')
        pr = tt = -1;
    else{
        pr = 1;
        tt = 0;
    }

    for(int j: adj[v]){
        if(erased[j])
            continue;
        upd(j, v, -1, pr, tt);
        add(j, v);
        upd(j, v, 1, pr, tt);
    }

    upd(v, -1, -1);


    erased[v] = 1;
    for(int j: adj[v])
        if(!erased[j])
            do_it(j);
}






int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);

    #ifdef Local
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/int.txt","r",stdin);
        freopen("C:/Users/Admin/Desktop/Yazilim/C/IO/out.txt","w",stdout);
    #endif

    cin>>n;
    cin>>s;
    s = '.' + 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);
    }

    do_it(1);


    cout<<ans<<endl;

    #ifdef Local
        cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
    #endif
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 6 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 10 ms 7404 KB Output is correct
8 Correct 11 ms 7404 KB Output is correct
9 Correct 11 ms 7404 KB Output is correct
10 Correct 6 ms 7404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 609 ms 42712 KB Output is correct
2 Correct 675 ms 48600 KB Output is correct
3 Correct 662 ms 46532 KB Output is correct
4 Correct 650 ms 48088 KB Output is correct
5 Correct 614 ms 46424 KB Output is correct
6 Correct 660 ms 47064 KB Output is correct
7 Correct 628 ms 46296 KB Output is correct
8 Correct 659 ms 47064 KB Output is correct
9 Correct 617 ms 46168 KB Output is correct
10 Correct 591 ms 52860 KB Output is correct
11 Correct 591 ms 46168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 7404 KB Output is correct
2 Correct 6 ms 7404 KB Output is correct
3 Correct 6 ms 7404 KB Output is correct
4 Correct 6 ms 7404 KB Output is correct
5 Correct 6 ms 7404 KB Output is correct
6 Correct 6 ms 7404 KB Output is correct
7 Correct 10 ms 7404 KB Output is correct
8 Correct 11 ms 7404 KB Output is correct
9 Correct 11 ms 7404 KB Output is correct
10 Correct 6 ms 7404 KB Output is correct
11 Correct 609 ms 42712 KB Output is correct
12 Correct 675 ms 48600 KB Output is correct
13 Correct 662 ms 46532 KB Output is correct
14 Correct 650 ms 48088 KB Output is correct
15 Correct 614 ms 46424 KB Output is correct
16 Correct 660 ms 47064 KB Output is correct
17 Correct 628 ms 46296 KB Output is correct
18 Correct 659 ms 47064 KB Output is correct
19 Correct 617 ms 46168 KB Output is correct
20 Correct 591 ms 52860 KB Output is correct
21 Correct 591 ms 46168 KB Output is correct
22 Correct 1477 ms 23000 KB Output is correct
23 Correct 1363 ms 23000 KB Output is correct
24 Correct 1382 ms 22952 KB Output is correct
25 Correct 1431 ms 22872 KB Output is correct
26 Correct 698 ms 30808 KB Output is correct
27 Correct 705 ms 27224 KB Output is correct
28 Correct 723 ms 25816 KB Output is correct
29 Correct 589 ms 52860 KB Output is correct
30 Correct 589 ms 52980 KB Output is correct
31 Correct 121 ms 22612 KB Output is correct
32 Correct 591 ms 46168 KB Output is correct