답안 #1014958

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1014958 2024-07-05T19:23:37 Z hyakup 늑대인간 (IOI18_werewolf) C++17
0 / 100
647 ms 149864 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
#define bug(x) cout << #x << " " << x << endl;
#define pii pair<int, int>

const int maxn = 2e5 + 10;
const int logn = 20;

int rep[maxn], tin[maxn], tout[maxn], pai[2][logn][maxn], sub[maxn], tempo;
vector<int> resp, adj[2][maxn];
vector<pii> queries[maxn];
set<pii> s[maxn];

int find( int a ){ return (( a == rep[a] ) ? a : rep[a] = find(rep[a]) ); }
void join( int a, int b, int t ){
  a = find(a); b = find(b);
  if( a == b ) return;
  if( (a > b) == (bool)t ) swap( a, b );
  rep[b] = a;
  // cout << b << " -> " << a << endl;
  adj[t][a].push_back(b);
}

void dfsInit( int cur, int anc, int t ){
  if( t ) tin[cur] = ++tempo;
  pai[t][0][cur] = anc;
  for( int i = 1; i < logn; i++ ) pai[t][i][cur] = pai[t][i - 1][pai[t][i - 1][cur]];
  for( int viz : adj[t][cur] ) dfsInit( viz, cur, t );
  if( t ) tout[cur] = tempo;
}

int binary_lift1( int a, int l ){
  for( int i = logn - 1; i >= 0; i-- ) if( pai[1][i][a] >= l ) a = pai[1][i][a];
  return a;
}

int binary_lift2( int a, int r ){
  for( int i = logn - 1; i >= 0; i-- ) if( pai[0][i][a] <= r ) a = pai[0][i][a];
  return a;
}

void dfs( int cur, int pai ){
  // bug(cur);
  sub[cur] = 1;
  for( int& viz : adj[0][cur] ){
    dfs( viz, cur );
    sub[cur] += sub[viz];
    if( sub[viz] > sub[adj[0][cur][0]] ) swap( viz, adj[0][cur][0] );
  }
  if( adj[0][cur].empty() ){
    s[cur].insert({ tin[cur], cur });
    return;
  }
  swap( s[cur], s[adj[0][cur][0]] );
  s[cur].insert({ tin[cur], cur });
  for( int viz : adj[0][cur] ) if( viz != adj[0][cur][0] ) for( auto p : s[viz] ) s[cur].insert(p);

  for( auto [id, x] : queries[cur] ){
    auto it = s[cur].lower_bound({ tin[x], -1 });
    if( it != s[cur].end() && it->first <= tout[x] ) resp[id] = 1;
  }
}

vector<int> check_validity(int N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R ){
  int n = N, m = (int)X.size(), q = (int)L.size();
  // bug(n); bug(m); bug(q);
  resp.resize(q);
  vector<pii> edges(m);
  for( int i = 0; i < m; i++ ) edges[i] = pii(min(X[i], Y[i]), max(X[i], Y[i]) );

  // arvore inicial - humano
  sort( edges.begin(), edges.end(), [&]( pii a, pii b ){
    return a.first > b.first;
  });
  for( int i = 0; i < n; i++ ) rep[i] = i;
  for( auto [a, b] : edges ) join( a, b, 1 );
  dfsInit( 0, 0, 1 );

  // cout << endl << endl;

  // arvore final - lobisomem
  sort( edges.begin(), edges.end(), [&]( pii a, pii b ){
    return a.second < b.second;
  });
  for( int i = 0; i < n; i++ ) rep[i] = i;
  for( auto [a, b] : edges ) join( a, b, 0 );
  dfsInit( n - 1, n - 1, 0 );

  // responde
  for( int i = 0; i < q; i++ ){
    int p1 = binary_lift1( S[i], L[i] ), p2 = binary_lift2( E[i], R[i] );
    // bug(p1);
    // bug(p2);
    queries[p2].push_back({ i, p1 });
  }

  dfs(n - 1, n - 1);
  // for( int i = 0; i < q; i++ ) cout << resp[i] << " ";
  return resp;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 25436 KB Output is correct
2 Incorrect 7 ms 25436 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 25436 KB Output is correct
2 Incorrect 7 ms 25436 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 647 ms 149864 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 25436 KB Output is correct
2 Incorrect 7 ms 25436 KB Output isn't correct
3 Halted 0 ms 0 KB -