Submission #785092

# Submission time Handle Problem Language Result Execution time Memory
785092 2023-07-17T04:57:44 Z 이동현(#10024) Zagrade (COI17_zagrade) C++17
10 / 100
321 ms 36104 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

const int NS = (int)3e5 + 4;
int cnt[NS];

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    string s;
    cin >> s;
    vector<vector<int>> way(n);
    for(int i = 1; i < n; ++i){
        int x, y;
        cin >> x >> y;
        --x, --y;
        way[x].push_back(y);
        way[y].push_back(x);
    }

    int ans = 0;

    vector<int> sz(n), chk(n);
    auto getsz = [&](auto&&self, int x, int pr)->void{
        sz[x] = 1;
        for(auto&nxt:way[x]){
            if(nxt == pr || chk[nxt]) continue;
            self(self, nxt, x);
            sz[x] += sz[nxt];
        }
    };
    auto getcent = [&](auto&&self, int x, int pr, int asz)->int{
        for(auto&nxt:way[x]){
            if(nxt == pr || chk[nxt]) continue;
            if(sz[nxt] * 2 > asz){
                return self(self, nxt, x, asz);
            }
        }
        return x;
    };
    auto sol = [&](auto&&self, int x)->void{
        getsz(getsz, x, -1);
        x = getcent(getcent, x, -1, sz[x]);
        chk[x] = 1;

        // cout << x << endl;

        vector<int> rb;

        int op = 0;
        auto dfs1 = [&](auto&&self, int x, int pr, int now, int mx)->void{
            now += (s[x] == '(' ? 1 : -1);
            mx = max(mx, now);
            if(now >= mx){
                rb.push_back(now);
                ++cnt[now];
                if(!op && !now) ++ans;
                // cout << x << " LEFT " << now << endl;
            }

            for(auto&nxt:way[x]){
                if(nxt == pr || chk[nxt]){
                    continue;
                }
                self(self, nxt, x, now, mx);
            }
        };

        auto dfs2 = [&](auto&&self, int x, int pr, int now, int mx)->void{
            now += (s[x] == ')' ? 1 : -1);
            mx = max(mx, now);
            if(now >= mx){
                ans += cnt[now];
                // cout << x << " RIGHT " << now << endl;
            }

            for(auto&nxt:way[x]){
                if(nxt == pr || chk[nxt]){
                    continue;
                }
                self(self, nxt, x, now, mx);
            }
        };

        for(op = 0; op < 2; ++op){
            if(s[x] == '(' && !op){
                rb.push_back(1);
                ++cnt[1];
            }
            for(auto&nxt:way[x]){
                if(chk[nxt]){
                    continue;
                }

                dfs2(dfs2, nxt, x, 0, 0);
                dfs1(dfs1, nxt, x, (s[x] == '(' ? 1 : -1), (s[x] == '(' ? 1 : 0));
            }

            for(auto&i:rb){
                cnt[i] = 0;
            }
            rb.clear();

            reverse(way[x].begin(), way[x].end());
        }

        for(auto&nxt:way[x]){
            if(chk[nxt]){
                continue;
            }

            self(self, nxt);
        }
    };

    sol(sol, 0);

    cout << ans << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 306 ms 33540 KB Output is correct
2 Correct 321 ms 33448 KB Output is correct
3 Correct 300 ms 33568 KB Output is correct
4 Correct 307 ms 33512 KB Output is correct
5 Correct 311 ms 33560 KB Output is correct
6 Correct 317 ms 34360 KB Output is correct
7 Correct 300 ms 33488 KB Output is correct
8 Correct 316 ms 34324 KB Output is correct
9 Correct 303 ms 33604 KB Output is correct
10 Correct 282 ms 36104 KB Output is correct
11 Incorrect 270 ms 34904 KB Output isn't correct
12 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 336 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 306 ms 33540 KB Output is correct
12 Correct 321 ms 33448 KB Output is correct
13 Correct 300 ms 33568 KB Output is correct
14 Correct 307 ms 33512 KB Output is correct
15 Correct 311 ms 33560 KB Output is correct
16 Correct 317 ms 34360 KB Output is correct
17 Correct 300 ms 33488 KB Output is correct
18 Correct 316 ms 34324 KB Output is correct
19 Correct 303 ms 33604 KB Output is correct
20 Correct 282 ms 36104 KB Output is correct
21 Incorrect 270 ms 34904 KB Output isn't correct
22 Halted 0 ms 0 KB -