답안 #894986

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
894986 2023-12-29T10:18:12 Z vjudge1 Zagrade (COI17_zagrade) C++17
100 / 100
2510 ms 723516 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"

const ld PI = 3.1415926535;
const int N = 3e5 + 2;
const int M = 7e6 + 1;
int mod0 = 1e9+7, mod1 = 1e9+9;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 31;

int mult(int a, int b, int mod) {
    return a * 1LL * b % mod;
}

int sum(int a, int b, int mod) {
    a %= mod; 
    if (a + b < 0)
        return a + b + mod;
    if (a + b >= mod)
        return a + b - mod;
    return a + b;
}

ll binpow(ll a, ll n, int mod) {
    if (n == 0)
        return 1;
    if (n % 2 == 1) {
        return binpow(a, n - 1, mod) * a % mod;
    } else {
        ll b = binpow(a, n / 2, mod);
        return b * b % mod;
    }
}

vector<int> g[N], ng[N];
int pc[N], used[N], sz[N], d[N];

void recalcsz(int v, int p){
    sz[v] = 1;
    for(auto u : g[v]){
        if(u != p && !used[u]){
            recalcsz(u, v);
            sz[v] += sz[u];
        }
    }
}

int getcentroid(int v, int p, int wh){
    for(auto u : g[v]){
        if(u != p && !used[u] && sz[u] > (wh / 2))
            return getcentroid(u, v, wh);
    }
    return v;
}

void decompose(int v, int p, int dp = 1){
    pc[v] = p;
    used[v] = 1;
    d[v] = dp;
    for(auto u : g[v]){
        if(!used[u]){
            recalcsz(u, -1);
            int c = getcentroid(u, -1, sz[u]);
            decompose(c, v, dp + 1);
        }
    }
}

gp_hash_table<int, pii> mp[N], mp1[N];
string s = "";

ll ans;
vector<int> dfs1(int v){
    vector<int> vs[sz(ng[v])];
    unordered_map<int, int> mpp;
    vector<int> ret = {v};
    for(int i = 0; i < sz(ng[v]); i++){
        vs[i] = dfs1(ng[v][i]);
        for(auto u : vs[i]){
            ret.pb(u);
            if(mp[u][v].S >= 0){
                if(s[v] == ')' && mp[u][v].F == 1)
                    ans++;
                ans += (ll)mpp[-mp[u][v].F];
            }
        }
        for(auto u : vs[i]){
            if(mp1[v][u].S >= mp1[v][u].F){
                mpp[mp1[v][u].F]++;
                if(mp1[v][u].F == 0)
                    ans++;
            }
        }
    }

    mpp.clear();
    for(int i = sz(ng[v]) - 1; i >= 0; i--){
        for(auto u : vs[i]){
            if(mp[u][v].S >= 0){
                ans += (ll)mpp[-mp[u][v].F];
            }
        }
        for(auto u : vs[i]){
            if(mp1[v][u].S >= mp1[v][u].F)
                mpp[mp1[v][u].F]++;
        }
    }
    return ret;
}

void nudela(int v, int p, int r, int c, int mn){
    if(s[v] == '(')
        c++;
    else
        c--;
    mn = min(mn, c);
    mp1[r][v] = {c, mn};
    for(auto u : g[v]){
        if(u != p && d[u] > d[r])
            nudela(u, v, r, c, mn);
    }
}

void neveroyatno(int v, int p, int r, int ver, int mn){
    if(v != r){
        if(s[v] == '(')
            mn = min(mn + 1, 0), ver++;
        else
            mn = min(mn - 1, -1), ver--;
        mp[v][r] = {ver, mn};
    }
    for(auto u : g[v]){
        if(d[u] > d[r] && u != p)
            neveroyatno(u, v, r, ver, mn);
    }
}

void solve(){
    int n;
    cin >> n;
    cin >> s;
    s = "@" + s;
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    recalcsz(1, -1);
    int c = getcentroid(1, -1, sz[1]);
    decompose(c, -1);
    for(int i = 1; i <= n; i++)
        ng[pc[i]].pb(i);
    for(int i = 1; i <= n; i++)
        nudela(i, -1, i, 0, 0);
    for(int i = 1; i <= n; i++)
        neveroyatno(i, -1, i, 0, 0);
    /*for(int i = 1; i <= n; i++)
        cout << d[i] << ' ';
    //cout << '\n';
    for(auto e : mp){
        //cout << e.F.F << ' ' << e.F.S << ' ' << e.S.F << ' ' << e.S.S << '\n';
    }
    //cout << '\n';
    for(auto e : mp1){
        //cout << e.F.F << ' ' << e.F.S << ' ' << e.S.F << ' ' << e.S.S << '\n';
    }*/
    dfs1(c);
    cout << ans << '\n';
}   

signed main() {
    mispertion;
    int t = 1;
    //cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 165208 KB Output is correct
2 Correct 90 ms 165200 KB Output is correct
3 Correct 91 ms 165024 KB Output is correct
4 Correct 95 ms 165184 KB Output is correct
5 Correct 91 ms 165204 KB Output is correct
6 Correct 95 ms 165204 KB Output is correct
7 Correct 90 ms 165204 KB Output is correct
8 Correct 90 ms 165204 KB Output is correct
9 Correct 90 ms 165204 KB Output is correct
10 Correct 89 ms 164948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1641 ms 719172 KB Output is correct
2 Correct 1670 ms 716600 KB Output is correct
3 Correct 1612 ms 716728 KB Output is correct
4 Correct 1692 ms 716456 KB Output is correct
5 Correct 1581 ms 716336 KB Output is correct
6 Correct 1597 ms 716924 KB Output is correct
7 Correct 1597 ms 716836 KB Output is correct
8 Correct 1656 ms 716608 KB Output is correct
9 Correct 1598 ms 716604 KB Output is correct
10 Correct 1965 ms 718196 KB Output is correct
11 Correct 1521 ms 716248 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 112 ms 165208 KB Output is correct
2 Correct 90 ms 165200 KB Output is correct
3 Correct 91 ms 165024 KB Output is correct
4 Correct 95 ms 165184 KB Output is correct
5 Correct 91 ms 165204 KB Output is correct
6 Correct 95 ms 165204 KB Output is correct
7 Correct 90 ms 165204 KB Output is correct
8 Correct 90 ms 165204 KB Output is correct
9 Correct 90 ms 165204 KB Output is correct
10 Correct 89 ms 164948 KB Output is correct
11 Correct 1641 ms 719172 KB Output is correct
12 Correct 1670 ms 716600 KB Output is correct
13 Correct 1612 ms 716728 KB Output is correct
14 Correct 1692 ms 716456 KB Output is correct
15 Correct 1581 ms 716336 KB Output is correct
16 Correct 1597 ms 716924 KB Output is correct
17 Correct 1597 ms 716836 KB Output is correct
18 Correct 1656 ms 716608 KB Output is correct
19 Correct 1598 ms 716604 KB Output is correct
20 Correct 1965 ms 718196 KB Output is correct
21 Correct 1521 ms 716248 KB Output is correct
22 Correct 2434 ms 417496 KB Output is correct
23 Correct 2385 ms 418416 KB Output is correct
24 Correct 2391 ms 413364 KB Output is correct
25 Correct 2510 ms 422304 KB Output is correct
26 Correct 1921 ms 635208 KB Output is correct
27 Correct 2009 ms 591908 KB Output is correct
28 Correct 1941 ms 571716 KB Output is correct
29 Correct 1965 ms 722332 KB Output is correct
30 Correct 1963 ms 723516 KB Output is correct
31 Correct 287 ms 217740 KB Output is correct
32 Correct 1575 ms 720592 KB Output is correct