제출 #1173982

#제출 시각아이디문제언어결과실행 시간메모리
1173982LucaIlieFile Paths (BOI15_fil)C++20
0 / 100
2 ms3904 KiB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e4 + 1;
const int MAX_LEN = 4e6;
int 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 ( adj[u].size() > 0 )
        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 ( adj[u].size() > 0 ) {
        ancestors.push_back( u );
        for ( int v: ancestors )
            cycleLens[depth[u] - depth[v] + s]++;
    }

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

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

    if ( adj[u].size() > 0 ) {
        for ( int v: ancestors )
            cycleLens[depth[u] - depth[v] + s]--;
        ancestors.push_back( u );
    }
}

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

    int n, m;

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

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

    for ( int v = n + 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...