답안 #249671

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
249671 2020-07-15T14:13:18 Z quocnguyen1012 늑대인간 (IOI18_werewolf) C++14
100 / 100
896 ms 141592 KB
#include <bits/stdc++.h>

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ar array

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

const int maxn = 4e5 + 5;

int N, M, Q;
int par[maxn], sz[maxn];
vector<int> adj[maxn], tree[maxn];
vector<ar<int, 3>> wolf, people;
int tin[maxn];
int st[maxn], en[maxn];
set<int> node[maxn];
int now[maxn], nsz[maxn];

int finds(int u)
{
  if(par[u] == u) return u;
  return par[u] = finds(par[u]);
}

void merges(int u, int v, bool ok)
{
  u = finds(u); v = finds(v);
  if(u == v) return;
  if(sz[u] < sz[v]) swap(u, v);
  if(!ok) tree[u].eb(v);
  else{
    for(int it : node[v])
      node[u].insert(it);
  }
  par[v] = u;
  sz[u] += sz[v];
}

void dfs(int u)
{
  static int nTime = 0;
  tin[u] = ++nTime;
  for(int v : tree[u]) dfs(v);
}

std::vector<int> check_validity(int _N, std::vector<int> X, std::vector<int> Y,
                                std::vector<int> S, std::vector<int> E,
                                std::vector<int> L, std::vector<int> R)
{
  N = _N;
  M = X.size();
  Q = S.size();
  vector<int> res(Q);
  iota(par, par + N, 0);
  fill(sz, sz + N, 1);
  for(int i = 0; i < M; ++i){
    adj[X[i]].eb(Y[i]);
    adj[Y[i]].eb(X[i]);
  }
  for(int i = 0; i < Q; ++i){
    people.pb({L[i], S[i], i});
    wolf.pb({R[i], E[i], i});
  }
  sort(people.rbegin(), people.rend());
  sort(wolf.begin(), wolf.end());
  for(int i = N - 1, ptr = 0; i >= 0; --i){
    for(int e : adj[i]){
      if(e > i){
        merges(e, i, 0);
      }
    }
    while(ptr < Q && people[ptr][0] == i){
      int id = people[ptr][2];
      int u = finds(people[ptr][1]);
      now[id] = u;
      //if(id == 1) cerr << sz[u] << ' ';
      nsz[id] = sz[u];
      ++ptr;
    }
  }
  /*
  for(int i = 0; i < N; ++i){
    for(int j : tree[i]) cerr << j << ' ';
    cerr << '\n';
  }*/
  for(int i = 0; i < N; ++i){
    if(par[i] == i){
      dfs(i);
      break;
    }
  }
  for(int i = 0; i < Q; ++i){
    st[i] = tin[now[i]];
    en[i] = tin[now[i]] + nsz[i] - 1;
    ///cerr << st[i] << ' ' << en[i] << '\n';
  }
  for(int i = 0; i < N; ++i){
    node[i].insert(tin[i]);
  }
  iota(par, par + N, 0);
  fill(sz, sz + N, 1);
  for(int i = 0, ptr = 0; i < N; ++i){
    for(int j : adj[i]) if(j < i) merges(i, j, 1);
    while(ptr < Q && wolf[ptr][0] == i){
      int u = finds(wolf[ptr][1]);
      int id = wolf[ptr][2];
      auto it = node[u].lower_bound(st[id]);
      if(it != node[u].end() && *(it) <= en[id])
        res[id] = true;
      ++ptr;
    }
  }
  return res;
}


#ifdef LOCAL

signed main(void)
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  #ifdef LOCAL
    freopen("A.INP", "r", stdin);
    freopen("A.OUT", "w", stdout);
  #endif // LOCAL
  int N, M, Q; cin >> N >> M >> Q;
  std::vector<int> X(M), Y(M), S(Q), E(Q), L(Q), R(Q);
  for(auto & it : X) cin >> it;
  for(auto & it : Y) cin >> it;
  for(auto & it : S) cin >> it;
  for(auto & it : E) cin >> it;
  for(auto & it : L) cin >> it;
  for(auto & it : R) cin >> it;

  std::vector<int> A = check_validity(N, X, Y, S, E, L, R);
  for (size_t i = 0; i < A.size(); ++i) {
    printf("%d\n", A[i]);
  }
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 38008 KB Output is correct
2 Correct 21 ms 38016 KB Output is correct
3 Correct 21 ms 38008 KB Output is correct
4 Correct 21 ms 37888 KB Output is correct
5 Correct 22 ms 38008 KB Output is correct
6 Correct 22 ms 38008 KB Output is correct
7 Correct 22 ms 38008 KB Output is correct
8 Correct 21 ms 38016 KB Output is correct
9 Correct 22 ms 38008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 38008 KB Output is correct
2 Correct 21 ms 38016 KB Output is correct
3 Correct 21 ms 38008 KB Output is correct
4 Correct 21 ms 37888 KB Output is correct
5 Correct 22 ms 38008 KB Output is correct
6 Correct 22 ms 38008 KB Output is correct
7 Correct 22 ms 38008 KB Output is correct
8 Correct 21 ms 38016 KB Output is correct
9 Correct 22 ms 38008 KB Output is correct
10 Correct 28 ms 38912 KB Output is correct
11 Correct 28 ms 38904 KB Output is correct
12 Correct 29 ms 39168 KB Output is correct
13 Correct 28 ms 38776 KB Output is correct
14 Correct 27 ms 38776 KB Output is correct
15 Correct 32 ms 39032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 762 ms 141512 KB Output is correct
2 Correct 502 ms 97176 KB Output is correct
3 Correct 531 ms 102808 KB Output is correct
4 Correct 663 ms 111128 KB Output is correct
5 Correct 653 ms 112536 KB Output is correct
6 Correct 708 ms 119124 KB Output is correct
7 Correct 783 ms 141592 KB Output is correct
8 Correct 503 ms 97176 KB Output is correct
9 Correct 522 ms 102808 KB Output is correct
10 Correct 561 ms 111128 KB Output is correct
11 Correct 607 ms 112452 KB Output is correct
12 Correct 695 ms 119964 KB Output is correct
13 Correct 551 ms 96372 KB Output is correct
14 Correct 543 ms 96584 KB Output is correct
15 Correct 533 ms 96664 KB Output is correct
16 Correct 582 ms 96600 KB Output is correct
17 Correct 896 ms 141508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 38008 KB Output is correct
2 Correct 21 ms 38016 KB Output is correct
3 Correct 21 ms 38008 KB Output is correct
4 Correct 21 ms 37888 KB Output is correct
5 Correct 22 ms 38008 KB Output is correct
6 Correct 22 ms 38008 KB Output is correct
7 Correct 22 ms 38008 KB Output is correct
8 Correct 21 ms 38016 KB Output is correct
9 Correct 22 ms 38008 KB Output is correct
10 Correct 28 ms 38912 KB Output is correct
11 Correct 28 ms 38904 KB Output is correct
12 Correct 29 ms 39168 KB Output is correct
13 Correct 28 ms 38776 KB Output is correct
14 Correct 27 ms 38776 KB Output is correct
15 Correct 32 ms 39032 KB Output is correct
16 Correct 762 ms 141512 KB Output is correct
17 Correct 502 ms 97176 KB Output is correct
18 Correct 531 ms 102808 KB Output is correct
19 Correct 663 ms 111128 KB Output is correct
20 Correct 653 ms 112536 KB Output is correct
21 Correct 708 ms 119124 KB Output is correct
22 Correct 783 ms 141592 KB Output is correct
23 Correct 503 ms 97176 KB Output is correct
24 Correct 522 ms 102808 KB Output is correct
25 Correct 561 ms 111128 KB Output is correct
26 Correct 607 ms 112452 KB Output is correct
27 Correct 695 ms 119964 KB Output is correct
28 Correct 551 ms 96372 KB Output is correct
29 Correct 543 ms 96584 KB Output is correct
30 Correct 533 ms 96664 KB Output is correct
31 Correct 582 ms 96600 KB Output is correct
32 Correct 896 ms 141508 KB Output is correct
33 Correct 872 ms 109724 KB Output is correct
34 Correct 409 ms 73176 KB Output is correct
35 Correct 742 ms 101272 KB Output is correct
36 Correct 742 ms 112920 KB Output is correct
37 Correct 708 ms 102168 KB Output is correct
38 Correct 731 ms 109372 KB Output is correct
39 Correct 668 ms 93380 KB Output is correct
40 Correct 832 ms 106688 KB Output is correct
41 Correct 762 ms 102772 KB Output is correct
42 Correct 734 ms 111896 KB Output is correct
43 Correct 815 ms 100952 KB Output is correct
44 Correct 768 ms 102304 KB Output is correct
45 Correct 633 ms 93720 KB Output is correct
46 Correct 597 ms 93336 KB Output is correct
47 Correct 509 ms 96408 KB Output is correct
48 Correct 512 ms 96284 KB Output is correct
49 Correct 536 ms 96492 KB Output is correct
50 Correct 513 ms 96152 KB Output is correct
51 Correct 808 ms 106268 KB Output is correct
52 Correct 870 ms 106264 KB Output is correct