Submission #251491

# Submission time Handle Problem Language Result Execution time Memory
251491 2020-07-21T13:25:55 Z Vimmer Zagrade (COI17_zagrade) C++14
100 / 100
1525 ms 52236 KB
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define N 300010

using namespace std;

typedef long long ll;

map<int,int> mp;

vector <int> g[N];

int sz[N], n, bl[N], need[N], res[N];

ll ans = 0;

bool mk[N];

string s;

void dfs(int v, int p)
{
    sz[v] = 1;

    for (auto it : g[v])
    {
        if (it == p || mk[it]) continue;

        dfs(it, v);

        sz[v] += sz[it];
    }
}

int fnd_cent(int v, int p)
{
    if (p != -1) {sz[p] -= sz[v]; sz[v] += sz[p];}

    for (auto it : g[v])
        if (!mk[it] && sz[it] + sz[it] > sz[v]) return fnd_cent(it, v);

    return v;
}

void rec(int v, int p, bool f)
{
    if (need[v] == abs(bl[v]) && bl[v] <= 0 && res[v] == 0) mp[abs(bl[v])]++;

    if (!f && bl[v] == 0 && need[v] == 0 && res[v] == 0) ans++;

    for (auto it : g[v])
    {
        if (mk[it] || it == p) continue;

        if (!f)
        {
            bl[it] = bl[v];

            need[it] = need[v];

            res[it] = res[v];

            if (s[it] == '(') {res[it]++; bl[it]++;}
              else {bl[it]--; if (res[it] > 0) res[it]--; else need[it]++;}
        }

        rec(it, v, f);
    }
}

void dec(int v, int p)
{
    if (need[v] == abs(bl[v]) && bl[v] <= 0 && res[v] == 0) mp[abs(bl[v])]--;

    for (auto it : g[v])
    {
        if (mk[it] || it == p) continue;

        dec(it, v);
    }
}

void dfser(int v, int p, int need, int b)
{
    if (s[v] == '(') {b++; need--; need = max(need, 0);}
      else {need++; b--;}

    if (b >= 0 && need == 0) ans += ll(mp[b]);

    for (auto it : g[v])
    {
        if (mk[it] || it == p) continue;

        dfser(it, v, need, b);
    }
}
void calc(int v)
{
    mp.clear();

    mk[v] = 1;

    bl[v] = need[v] = res[v] = 0;

    if (s[v] == '(') {res[v]++; bl[v]++;}
      else {bl[v]--; need[v] = 1;}

    rec(v, -1, 0);

    for (auto it : g[v])
    {
        if (mk[it]) continue;

        dec(it, -1);

        dfser(it, -1, 0, 0);

        rec(it, -1, 1);
    }

    for (auto it : g[v])
    {
        if (mk[it]) continue;

        int root = fnd_cent(it, -1);

        if (root != -1)
        {
            dfs(root, -1);

            calc(root);
        }
    }
}
int main()
{
    ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    cin >> n;

    cin >> s;

    for (int i = 1; i < n; i++)
    {
        int x, y;

        cin >> x >> y;

        x--; y--;

        g[x].pb(y);

        g[y].pb(x);
    }

    dfs(0, -1);

    int root = fnd_cent(0, -1);

    dfs(root, -1);

    calc(root);

    cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7424 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Correct 6 ms 7424 KB Output is correct
4 Correct 6 ms 7424 KB Output is correct
5 Correct 6 ms 7424 KB Output is correct
6 Correct 6 ms 7424 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 7 ms 7424 KB Output is correct
9 Correct 6 ms 7424 KB Output is correct
10 Correct 5 ms 7424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 581 ms 45024 KB Output is correct
2 Correct 933 ms 47776 KB Output is correct
3 Correct 592 ms 45048 KB Output is correct
4 Correct 903 ms 47120 KB Output is correct
5 Correct 657 ms 45068 KB Output is correct
6 Correct 656 ms 45968 KB Output is correct
7 Correct 577 ms 45200 KB Output is correct
8 Correct 596 ms 46092 KB Output is correct
9 Correct 602 ms 45452 KB Output is correct
10 Correct 1012 ms 52236 KB Output is correct
11 Correct 452 ms 45068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7424 KB Output is correct
2 Correct 6 ms 7424 KB Output is correct
3 Correct 6 ms 7424 KB Output is correct
4 Correct 6 ms 7424 KB Output is correct
5 Correct 6 ms 7424 KB Output is correct
6 Correct 6 ms 7424 KB Output is correct
7 Correct 8 ms 7424 KB Output is correct
8 Correct 7 ms 7424 KB Output is correct
9 Correct 6 ms 7424 KB Output is correct
10 Correct 5 ms 7424 KB Output is correct
11 Correct 581 ms 45024 KB Output is correct
12 Correct 933 ms 47776 KB Output is correct
13 Correct 592 ms 45048 KB Output is correct
14 Correct 903 ms 47120 KB Output is correct
15 Correct 657 ms 45068 KB Output is correct
16 Correct 656 ms 45968 KB Output is correct
17 Correct 577 ms 45200 KB Output is correct
18 Correct 596 ms 46092 KB Output is correct
19 Correct 602 ms 45452 KB Output is correct
20 Correct 1012 ms 52236 KB Output is correct
21 Correct 452 ms 45068 KB Output is correct
22 Correct 1288 ms 26508 KB Output is correct
23 Correct 1525 ms 26376 KB Output is correct
24 Correct 1137 ms 26504 KB Output is correct
25 Correct 1231 ms 26636 KB Output is correct
26 Correct 681 ms 32648 KB Output is correct
27 Correct 678 ms 29836 KB Output is correct
28 Correct 693 ms 28780 KB Output is correct
29 Correct 1025 ms 52156 KB Output is correct
30 Correct 1036 ms 52032 KB Output is correct
31 Correct 131 ms 26116 KB Output is correct
32 Correct 460 ms 44988 KB Output is correct