답안 #168282

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
168282 2019-12-12T08:42:14 Z egekabas Zagrade (COI17_zagrade) C++14
10 / 100
1908 ms 63444 KB
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long   ll;
typedef unsigned long long   ull;
typedef long double ld;
typedef pair<ll, ll>    pll;
typedef pair<ull, ull>    pull;
typedef pair<ll, ll>  pii;
typedef pair<ld, ld>  pld;
ll n;
ll a[300009];
vector<ll> g[300009];
ll sz[300009];
ll block[300009];
ll ans;
void findsize(ll v, ll p){
    sz[v] = 1;
    for(auto u : g[v]){
        if(u == p || block[u])
            continue;
        findsize(u, v);
        sz[v] += sz[u];
    }
}
ll findcentroid(ll v, ll p, ll tot){
    for(auto u : g[v]){
        if(u == p || block[u])
            continue;
        if(sz[u] >= tot/2)
            return findcentroid(u, v, tot);
    }
    return v;
}
map<int, int> mpp1;
map<int, int> mpp2;

void dp1(ll v, ll p, ll cur, ll bal){
    cur += a[v];
    if(bal < 0 && a[v] == 1)
        bal += a[v];
    if(a[v] == -1)
        bal += a[v];
    if(bal == 0 && cur >= 0)
        mpp1[cur]++;
    for(auto u : g[v]){
        if(u == p || block[u])
            continue;
        dp1(u, v, cur, bal);
    }
}
void dp2(ll v, ll p, ll cur, ll bal){
    cur += a[v];
    if(bal > 0 && a[v] == -1)
        bal += a[v];
    if(a[v] == 1)
        bal += a[v];
    if(bal == 0 && cur <= 0)
        mpp2[cur]++;
    for(auto u : g[v]){
        if(u == p || block[u])
            continue;
        dp2(u, v, cur, bal);
    }
}
    
void maincalc(ll v){
    findsize(v, -1);
    v = findcentroid(v, -1, sz[v]);
    map<int, int> cur1;
    map<int, int> cur2;
    for(auto u : g[v]){
        if(block[u])
            continue;
        dp1(u, v, 0, 0);
        dp2(u, v, 0, 0);
        //cout << v << " ----- " << u << "\n";
        for(auto u : mpp1){
            //cout << u.ff << " " << u.ss << "\n";
            if(u.ff + a[v] < 0)
                continue;
            if(u.ff + a[v] == 0)
                ans += u.ss;
            ans += cur2[-(u.ff+a[v])]*u.ss;
        }
        for(auto u : mpp2){
            if(u.ff + a[v] > 0)
                continue;
            if(u.ff + a[v] == 0)
                ans += u.ss;
            ans += cur1[-(u.ff+a[v])]*u.ss;
        }
        for(auto u : mpp1){
            if(u.ff < 0)
                continue;
            cur1[u.ff] += u.ss;
        }
        for(auto u : mpp2){
            if(u.ff > 0)
                continue;
            cur2[u.ff] += u.ss;
        }
        mpp1.clear();
        mpp2.clear();
    }
    cur1.clear();
    cur2.clear();
    block[v] = 1;
    for(auto u : g[v]){
        if(block[u] == 0)
            maincalc(u);
    }
}
    
ll input(){
    char c;
    cin >> c;
    if(c == '(')
        return 1;
    if(c == ')')
        return -1;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    cin >> n;
    for(ll i = 1; i <= n; ++i)
        a[i] = input();
    for(ll i = 1; i < n; ++i){
        ll t1, t2;
        cin >> t1 >> t2;
        g[t1].pb(t2);
        g[t2].pb(t1);
    }
    maincalc(1);
    cout << ans << "\n";
}

Compilation message

zagrade.cpp: In function 'll input()':
zagrade.cpp:125:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7544 KB Output is correct
2 Correct 9 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7416 KB Output is correct
6 Correct 9 ms 7548 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 8 ms 7420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 633 ms 37924 KB Output is correct
2 Correct 1112 ms 50780 KB Output is correct
3 Correct 633 ms 42360 KB Output is correct
4 Correct 982 ms 48680 KB Output is correct
5 Correct 634 ms 42392 KB Output is correct
6 Correct 761 ms 45148 KB Output is correct
7 Correct 633 ms 42360 KB Output is correct
8 Correct 756 ms 45236 KB Output is correct
9 Correct 640 ms 42312 KB Output is correct
10 Correct 1908 ms 63444 KB Output is correct
11 Incorrect 431 ms 42232 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7544 KB Output is correct
2 Correct 9 ms 7416 KB Output is correct
3 Correct 9 ms 7416 KB Output is correct
4 Correct 9 ms 7416 KB Output is correct
5 Correct 9 ms 7416 KB Output is correct
6 Correct 9 ms 7548 KB Output is correct
7 Correct 9 ms 7416 KB Output is correct
8 Correct 9 ms 7544 KB Output is correct
9 Correct 9 ms 7416 KB Output is correct
10 Correct 8 ms 7420 KB Output is correct
11 Correct 633 ms 37924 KB Output is correct
12 Correct 1112 ms 50780 KB Output is correct
13 Correct 633 ms 42360 KB Output is correct
14 Correct 982 ms 48680 KB Output is correct
15 Correct 634 ms 42392 KB Output is correct
16 Correct 761 ms 45148 KB Output is correct
17 Correct 633 ms 42360 KB Output is correct
18 Correct 756 ms 45236 KB Output is correct
19 Correct 640 ms 42312 KB Output is correct
20 Correct 1908 ms 63444 KB Output is correct
21 Incorrect 431 ms 42232 KB Output isn't correct