Submission #168272

# Submission time Handle Problem Language Result Execution time Memory
168272 2019-12-12T07:36:27 Z egekabas Zagrade (COI17_zagrade) C++14
10 / 100
3000 ms 89052 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<ll, ll> inc[300009];
map<ll, ll> out[300009];
void seccalc(ll v, ll p){

    out[v].clear();
    inc[v].clear();
    if(a[v] == -1)
        out[v][a[v]] = 1;
    if(a[v] == 1)
        inc[v][a[v]] = 1;
    for(auto u : g[v]){
        if(u == p || block[u])
            continue;
        seccalc(u, v);
        for(auto k : out[u]){
            if(a[v]+k.ff <= 0)
                out[v][a[v]+k.ff] += k.ss;
        }
        for(auto k : inc[u]){
            if(a[v]+k.ff >= 0)
                inc[v][a[v]+k.ff] += k.ss;
        }
        inc[u].clear();
        out[u].clear();
    }

}

void maincalc(ll v){
    findsize(v, -1);
    v = findcentroid(v, -1, sz[v]);
    out[v].clear();
    inc[v].clear();
    for(auto u : g[v]){
        if(block[u])
            continue;
        seccalc(u, v);
        //cout << v << " "<< u << " ==>\n";
        for(auto k : out[u]){
            if(a[v]+k.ff > 0)
                continue;
            //cout << k.ff << " " << k.ss << "\n";
            if(a[v]+k.ff == 0)
                ans += k.ss;
            ans += inc[v][-(a[v]+k.ff)]*k.ss;
        }
        for(auto k : inc[u]){
            if(a[v]+k.ff < 0)
                continue;

            //cout << k.ff << " " << k.ss << "\n";
            if(a[v]+k.ff == 0)
                ans += k.ss;
            ans += out[v][-(a[v]+k.ff)]*k.ss;
        }
        
        for(auto k : out[u]){
            if(k.ff <= 0)
                out[v][k.ff] += k.ss;
        }
        for(auto k : inc[u]){
            if(k.ff >= 0)
                inc[v][k.ff] += k.ss;
        }
        inc[u].clear();
        out[u].clear();
    }
    inc[v].clear();
    out[v].clear();
    block[v] = 1;
    for(auto u : g[v]){
        if(!block[u])
            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:121:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 36 ms 35704 KB Output is correct
2 Correct 35 ms 35704 KB Output is correct
3 Correct 35 ms 35704 KB Output is correct
4 Correct 36 ms 35704 KB Output is correct
5 Correct 35 ms 35704 KB Output is correct
6 Correct 36 ms 35704 KB Output is correct
7 Correct 36 ms 35704 KB Output is correct
8 Correct 36 ms 35704 KB Output is correct
9 Correct 35 ms 35704 KB Output is correct
10 Correct 34 ms 35704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3032 ms 89052 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 36 ms 35704 KB Output is correct
2 Correct 35 ms 35704 KB Output is correct
3 Correct 35 ms 35704 KB Output is correct
4 Correct 36 ms 35704 KB Output is correct
5 Correct 35 ms 35704 KB Output is correct
6 Correct 36 ms 35704 KB Output is correct
7 Correct 36 ms 35704 KB Output is correct
8 Correct 36 ms 35704 KB Output is correct
9 Correct 35 ms 35704 KB Output is correct
10 Correct 34 ms 35704 KB Output is correct
11 Execution timed out 3032 ms 89052 KB Time limit exceeded
12 Halted 0 ms 0 KB -