Submission #1174036

#TimeUsernameProblemLanguageResultExecution timeMemory
1174036LucaIlieFile Paths (BOI15_fil)C++20
100 / 100
138 ms8156 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 6e3 + 1;
const int MAX_LEN = 2e6;
int n, m, k, s;
bool ans[MAX_N];
int val[MAX_N], depth[MAX_N], nonCycleLens[MAX_LEN + 1], cycleLens[MAX_LEN + 1];
vector<int> adj[MAX_N];

void dfs( int u ) {
    if ( u <= n )
        nonCycleLens[depth[u] + s]++;
    for ( int v: adj[u] ) {
        depth[v] = depth[u] + val[v];
        dfs( v );
    }
}

void updateSubtree( int v, int u, int c ) {
    if ( v > n )
        return;
    cycleLens[depth[v] - depth[u] + s] += c;
    for ( int w: adj[v] )
        updateSubtree( w, u, c );
}

vector<int> ancestors;
void findAll( int u ) {
    if ( u <= n ) {
        ancestors.push_back( u );
        updateSubtree( u, u, +1 );
    }

    if ( u > n ) {
        for ( int v: ancestors )
            ans[u] |= (nonCycleLens[k - depth[u] + depth[v]] > 0); 

        int c = k - depth[u];
        for ( int d = 1; d * d <= c; d++ ) {
            if ( c % d == 0 ) {
                ans[u] |= (cycleLens[d] > 0);
                ans[u] |= (cycleLens[c / d] > 0);
            }
        }
    }

    for ( int v: adj[u] )
        findAll( v );

    if ( u <= n ) {
        updateSubtree( u, u, -1 );
        ancestors.pop_back();
    }
}

int main() {
    cin.tie( nullptr );
    ios_base::sync_with_stdio( false );

    cin >> n >> m >> k >> s;
    s++;

    for ( int v = 1; v <= n + m; v++ ) {
        int p;
        cin >> p >> val[v];
        adj[p].push_back( v );
        val[v]++;
    }

    nonCycleLens[0] = 1;
    dfs( 0 );
    findAll( 0 );

    for ( int i = n + 1; i <= n + m; i++ )
        cout << (ans[i] ? "YES\n" : "NO\n");

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...