Submission #1014975

# Submission time Handle Problem Language Result Execution time Memory
1014975 2024-07-05T20:35:17 Z hyakup Werewolf (IOI18_werewolf) C++17
100 / 100
641 ms 150544 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<int> 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]);
    for( auto [id, x] : queries[cur] ){
      auto it = s[cur].lower_bound(tin[x]);
      if( it != s[cur].end() && *it <= tout[x] ) resp[id] = 1;
    }
    return;
  }
  swap( s[cur], s[adj[0][cur][0]] );
  s[cur].insert(tin[cur]);
  for( int viz : adj[0][cur] ) if( viz != adj[0][cur][0] ) for( auto x : s[viz] ) s[cur].insert(x);

  for( auto [id, x] : queries[cur] ){
    auto it = s[cur].lower_bound(tin[x]);
    if( it != s[cur].end() && *it <= 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;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24156 KB Output is correct
2 Correct 10 ms 24212 KB Output is correct
3 Correct 9 ms 24156 KB Output is correct
4 Correct 10 ms 24176 KB Output is correct
5 Correct 10 ms 24156 KB Output is correct
6 Correct 9 ms 24160 KB Output is correct
7 Correct 9 ms 24024 KB Output is correct
8 Correct 10 ms 24152 KB Output is correct
9 Correct 11 ms 24152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24156 KB Output is correct
2 Correct 10 ms 24212 KB Output is correct
3 Correct 9 ms 24156 KB Output is correct
4 Correct 10 ms 24176 KB Output is correct
5 Correct 10 ms 24156 KB Output is correct
6 Correct 9 ms 24160 KB Output is correct
7 Correct 9 ms 24024 KB Output is correct
8 Correct 10 ms 24152 KB Output is correct
9 Correct 11 ms 24152 KB Output is correct
10 Correct 13 ms 25436 KB Output is correct
11 Correct 16 ms 25332 KB Output is correct
12 Correct 19 ms 25688 KB Output is correct
13 Correct 15 ms 25692 KB Output is correct
14 Correct 14 ms 25692 KB Output is correct
15 Correct 14 ms 25688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 612 ms 149740 KB Output is correct
2 Correct 515 ms 119120 KB Output is correct
3 Correct 500 ms 115924 KB Output is correct
4 Correct 479 ms 120876 KB Output is correct
5 Correct 474 ms 122060 KB Output is correct
6 Correct 497 ms 128584 KB Output is correct
7 Correct 641 ms 150544 KB Output is correct
8 Correct 471 ms 119016 KB Output is correct
9 Correct 365 ms 116012 KB Output is correct
10 Correct 345 ms 120148 KB Output is correct
11 Correct 379 ms 121668 KB Output is correct
12 Correct 426 ms 128568 KB Output is correct
13 Correct 495 ms 126544 KB Output is correct
14 Correct 481 ms 126528 KB Output is correct
15 Correct 585 ms 126588 KB Output is correct
16 Correct 502 ms 126736 KB Output is correct
17 Correct 636 ms 149692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 24156 KB Output is correct
2 Correct 10 ms 24212 KB Output is correct
3 Correct 9 ms 24156 KB Output is correct
4 Correct 10 ms 24176 KB Output is correct
5 Correct 10 ms 24156 KB Output is correct
6 Correct 9 ms 24160 KB Output is correct
7 Correct 9 ms 24024 KB Output is correct
8 Correct 10 ms 24152 KB Output is correct
9 Correct 11 ms 24152 KB Output is correct
10 Correct 13 ms 25436 KB Output is correct
11 Correct 16 ms 25332 KB Output is correct
12 Correct 19 ms 25688 KB Output is correct
13 Correct 15 ms 25692 KB Output is correct
14 Correct 14 ms 25692 KB Output is correct
15 Correct 14 ms 25688 KB Output is correct
16 Correct 612 ms 149740 KB Output is correct
17 Correct 515 ms 119120 KB Output is correct
18 Correct 500 ms 115924 KB Output is correct
19 Correct 479 ms 120876 KB Output is correct
20 Correct 474 ms 122060 KB Output is correct
21 Correct 497 ms 128584 KB Output is correct
22 Correct 641 ms 150544 KB Output is correct
23 Correct 471 ms 119016 KB Output is correct
24 Correct 365 ms 116012 KB Output is correct
25 Correct 345 ms 120148 KB Output is correct
26 Correct 379 ms 121668 KB Output is correct
27 Correct 426 ms 128568 KB Output is correct
28 Correct 495 ms 126544 KB Output is correct
29 Correct 481 ms 126528 KB Output is correct
30 Correct 585 ms 126588 KB Output is correct
31 Correct 502 ms 126736 KB Output is correct
32 Correct 636 ms 149692 KB Output is correct
33 Correct 633 ms 119888 KB Output is correct
34 Correct 201 ms 56052 KB Output is correct
35 Correct 576 ms 120660 KB Output is correct
36 Correct 543 ms 124024 KB Output is correct
37 Correct 564 ms 119208 KB Output is correct
38 Correct 586 ms 122144 KB Output is correct
39 Correct 506 ms 136408 KB Output is correct
40 Correct 499 ms 127288 KB Output is correct
41 Correct 438 ms 117472 KB Output is correct
42 Correct 442 ms 122196 KB Output is correct
43 Correct 499 ms 128084 KB Output is correct
44 Correct 485 ms 118612 KB Output is correct
45 Correct 408 ms 137300 KB Output is correct
46 Correct 381 ms 136964 KB Output is correct
47 Correct 480 ms 126804 KB Output is correct
48 Correct 533 ms 126544 KB Output is correct
49 Correct 507 ms 126784 KB Output is correct
50 Correct 528 ms 126668 KB Output is correct
51 Correct 486 ms 126288 KB Output is correct
52 Correct 471 ms 126444 KB Output is correct