제출 #1174031

#제출 시각아이디문제언어결과실행 시간메모리
1174031LucaIlieFile Paths (BOI15_fil)C++20
33 / 100
23 ms8052 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 );
    }
}

vector<int> ancestors;
void findAll( int u ) {
    if ( u <= n ) {
        ancestors.push_back( u );
        for ( int v: ancestors )
            cycleLens[depth[u] - depth[v] + s]++;
    }

    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 ) {
        for ( int v: ancestors )
            cycleLens[depth[u] - depth[v] + s]--;
        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...