Submission #207098

# Submission time Handle Problem Language Result Execution time Memory
207098 2020-03-06T12:44:30 Z Hideo Zagrade (COI17_zagrade) C++11
10 / 100
3000 ms 34940 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);
    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:121:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
zagrade.cpp: In function 'int main()':
zagrade.cpp:126: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 Correct 78 ms 9720 KB Output is correct
2 Correct 87 ms 9848 KB Output is correct
3 Correct 79 ms 9848 KB Output is correct
4 Correct 81 ms 9720 KB Output is correct
5 Correct 81 ms 9724 KB Output is correct
6 Correct 91 ms 9848 KB Output is correct
7 Correct 91 ms 9720 KB Output is correct
8 Correct 84 ms 9720 KB Output is correct
9 Correct 84 ms 9720 KB Output is correct
10 Correct 86 ms 9816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3076 ms 34940 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 78 ms 9720 KB Output is correct
2 Correct 87 ms 9848 KB Output is correct
3 Correct 79 ms 9848 KB Output is correct
4 Correct 81 ms 9720 KB Output is correct
5 Correct 81 ms 9724 KB Output is correct
6 Correct 91 ms 9848 KB Output is correct
7 Correct 91 ms 9720 KB Output is correct
8 Correct 84 ms 9720 KB Output is correct
9 Correct 84 ms 9720 KB Output is correct
10 Correct 86 ms 9816 KB Output is correct
11 Execution timed out 3076 ms 34940 KB Time limit exceeded
12 Halted 0 ms 0 KB -