답안 #795985

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
795985 2023-07-28T02:58:23 Z MattTheNub 열쇠 (IOI21_keys) C++17
9 / 100
425 ms 262968 KB
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

template <class T> using v = vector<T>;
#ifdef EVAL
#define dbg(...)
#define dbg2d(...)
#else
istream &__cin = cin;
#include "../../debug.h"
__cinwrapper __cin_wrapper;
#include "../../debug.cpp"
#endif
#include <vector>

#define f first
#define s second
using ll = long long;
using int2 = pair<int, int>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct chash {
  static const int RANDOM;
  static unsigned hash_f(uint64_t x) {
    x += RANDOM;
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  static unsigned hash_comb(unsigned a, unsigned b) {
    return hash_f(a * 31 + b);
  }
  int operator()(int x) const { return hash_f(x); }
  int operator()(ll x) const { return hash_f(x); }
  template <class T1, class T2> int operator()(pair<T1, T2> x) const {
    return hash_comb((*this)(x.f), (*this)(x.s));
  }
  int operator()(const string &s) const {
    int hash = 0;
    for (char c : s) {
      hash = hash_comb(c, hash);
    }
    return hash;
  }
};
const int chash::RANDOM = rng();

template <class K, class V> using ht = gp_hash_table<K, V, chash>;
template <class T> using hs = gp_hash_table<T, null_type, chash>;

struct Edg {
  int u, v, c, i;
  friend bool operator<(Edg a, Edg b) {
    return (a.c == b.c) ? (a.i < b.i) : (a.c < b.c);
  }
};
queue<Edg> nxt;
v<int2> vis;

struct DSU {
  v<int> e;
  v<hs<int>> keys;
  v<set<Edg>> edg;
  DSU(int n) : keys(n), edg(n) { e = v<int>(n, -1); }

  int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
  bool same_set(int a, int b) { return get(a) == get(b); }
  int size(int x) { return -e[get(x)]; }

  bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x == y) {
      return false;
    }
    if (e[x] > e[y]) {
      swap(x, y);
    }
    e[x] += e[y];
    e[y] = x;
    if (edg[y].size() + keys[y].size() > edg[x].size() + keys[x].size()) {
      keys[y].swap(keys[x]);
      edg[y].swap(edg[x]);
    }
    for (auto z : keys[y]) {
      auto it = edg[x].lower_bound({0, 0, z, -1});
      while (it != edg[x].end() && it->c == z) {
        nxt.push(*it);
        it++;
        edg[x].erase(prev(it));
      }
      keys[x].insert(z);
    }
    for (auto z : edg[y]) {
      if (keys[x].find(z.c) != keys[x].end()) {
        nxt.push(z);
      } else {
        edg[y].insert(z);
      }
    }
    return true;
  }
};

std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u,
                                std::vector<int> v, std::vector<int> c) {
  int n = r.size();
  int m = u.size();
  DSU dsu(n);

  vis.assign(m, {-1, -1});

  for (int i = 0; i < m; i++) {
    dsu.edg[u[i]].insert({u[i], v[i], c[i], i});
    dsu.edg[v[i]].insert({v[i], u[i], c[i], i});

    if (r[u[i]] == c[i]) {
      nxt.push({u[i], v[i], c[i], i});
    }
    if (r[v[i]] == c[i]) {
      nxt.push({v[i], u[i], c[i], i});
    }
  }
  for (int i = 0; i < n; i++) {
    dsu.keys[i].insert(r[i]);
  }

  while (nxt.size()) {
    Edg edg = nxt.front();
    nxt.pop();

    if (vis[edg.i].s != -1 || vis[edg.i].f == edg.u)
      continue;
    else if (vis[edg.i].f == -1) {
      dbg(edg.u, edg.v);
      vis[edg.i].f = edg.u;
    } else {
      dbg(edg.u, edg.v);
      vis[edg.i].s = edg.u;
      dsu.unite(edg.u, edg.v);
    }
  }
  dbg(vis);

  hs<int> components;
  for (int i = 0; i < n; i++) {
    components.insert(dsu.get(i));
  }

  for (int i = 0; i < m; i++) {
    if (vis[i].s == -1 && vis[i].f != -1 && !dsu.same_set(u[i], v[i])) {
      components.erase(dsu.get(vis[i].f));
    }
  }
  assert(components.size());

  int mn = INT_MAX;
  for (auto x : components) {
    mn = min(mn, dsu.size(x));
  }

  hs<int> componentstoo;
  for (auto x : components) {
    if (dsu.size(x) == mn) {
      componentstoo.insert(x);
    }
  }
  assert(componentstoo.size());
  vector<int> ans(n, 0);
  for (int i = 0; i < n; i++) {
    if (componentstoo.find(dsu.get(i)) != componentstoo.end()) {
      ans[i] = 1;
    }
  }

  return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Runtime error 1 ms 596 KB Execution killed with signal 6
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Runtime error 1 ms 596 KB Execution killed with signal 6
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 425 ms 75748 KB Output is correct
11 Runtime error 418 ms 262968 KB Execution killed with signal 6
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Runtime error 1 ms 596 KB Execution killed with signal 6
16 Halted 0 ms 0 KB -