Submission #207103

# Submission time Handle Problem Language Result Execution time Memory
207103 2020-03-06T13:21:09 Z Hideo Zagrade (COI17_zagrade) C++11
0 / 100
211 ms 74888 KB
//#pragma GCC optimize ("Ofast")
//#pragma GCC target ("avx,avx2,fma")

#include <bits/stdc++.h>
using namespace std;

#define all(s) s.begin(), s.end()
#define ok puts("ok")
#define ll long long
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define vi vector < int >
#define pi pair < int, int >
#define pii pair < int, pi >
#define next next123

const int N = 3e5 + 7;
const int INF = 1e9 + 7;

int d[N], sub[N], st[N], fin[N];
int n;
ll ans;
string s;

vi g[N];

void dfs (int v, int p = 0){
    sub[v] = 1;
    for (int to : g[v]){
        if (!d[to] && to != p){
            dfs(to, v);
            sub[v] += sub[to];
        }
    }
}

int fc (int v){
    bool good = true;
    int sz = sub[v] / 2, p = 0;
    while (good){
        good = false;
        for (int to : g[v]){
            if (!d[to] && to != p && sub[to] > sz){
                good = true;
                p = v; v = to;
                break;
            }
        }
    }
    return v;
}

vi nw;

void calc (int v, int p, int ad = 0, int bal = 0, int mnb = 0, int mxb = 0){
    if (s[v] == '(')
        bal++;
    else
        bal--;
    mxb = max(mxb, bal);
    mnb = min(mnb, bal);
    if (mxb == bal){
        if (bal){
            nw.pb(bal);
            if (mxb == bal)
                ans += fin[bal + ad - (!ad)];
        }
        else {
            nw.pb(INF);
            if (mxb == bal && ad)
                ans += fin[bal + 1];
        }
    }
    if (mnb == bal){
        if (bal){
            nw.pb(bal);
            if (mnb == bal)
                ans += st[-bal + (!ad) - ad];
        }
        else {
            nw.pb(-INF);
            if (mnb == bal && !ad)
                ans += st[bal + 1];
        }
    }
    for (int to : g[v])
        if (!d[to] && to != p)
            calc(to, v, ad, bal, mnb, mxb);
}

void cd (int v = 1){
    dfs(v);
    assert(false);
    v = fc(v);
    memset(fin, 0, sizeof(fin));
    memset(st, 0, sizeof(st));
    fin[0] = st[0] = 1;
    d[v] = 1;
    for (int to : g[v]){
        if (!d[to]){
            nw.clear();
            calc(to, v, (s[v] == '('));
            for (int it : nw){
                if (it == -INF)
                    fin[0]++;
                else if (it == INF)
                    st[0]++;
                else if (it < 0)
                    fin[-it]++;
                else
                    st[it]++;
            }
        }
    }
    for (int to : g[v])
        if (!d[to])
            cd(to);
}

main(){
    cin >> n >> s;
    s = ' ' + s;
    for (int i = 1; i < n; i++){
        int a, b;
        scanf("%d%d", &a, &b);
        g[a].pb(b);
        g[b].pb(a);
    }
    cd();
    cout << ans;
}

Compilation message

zagrade.cpp:122:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
zagrade.cpp: In function 'int main()':
zagrade.cpp:127:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d", &a, &b);
         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 14840 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 211 ms 74888 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 14840 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -