제출 #766666

#제출 시각아이디문제언어결과실행 시간메모리
766666marvinthang늑대인간 (IOI18_werewolf)C++17
100 / 100
569 ms95592 KiB
/*************************************
*    author: marvinthang             *
*    created: 26.06.2023 10:38:52    *
*************************************/

#include "werewolf.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
    #include "debug.h"
#else
    #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
    #define DB(...) 23
    #define db(...) 23
    #define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

// end of template

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 M = X.size();
	int Q = S.size();
	vector <int> lab(N, -1);
	vector <vector <int>> adj(N), L_at(N), R_at(N), tree_adj(N);

	REP(i, M) {
		adj[X[i]].push_back(Y[i]);
		adj[Y[i]].push_back(X[i]);
	}

	REP(i, Q) {
		L_at[L[i]].push_back(i);
		R_at[R[i]].push_back(i);
	}

	function <int(int)> find = [&] (int u) {
		return lab[u] < 0 ? u : lab[u] = find(lab[u]);
	};

	int nw = N;
	REPD(i, N) {
		int u;
		for (int v: adj[i]) if (v >= i) {
			if ((u = find(i)) == (v = find(v))) continue;
			if (lab[u] > lab[v]) swap(u, v);

			lab.push_back(lab[u] + lab[v]);
			lab[u] = lab[v] = nw++;
			tree_adj.push_back(vector<int> {u, v});
		}
		for (int id: L_at[i]) S[id] = find(S[id]);
	}

	int timeDfs = 0;
	vector <int> tin(nw), tout(nw);
	function <void(int)> dfs = [&] (int u) {
		tin[u] = ++timeDfs;
		for (int v: tree_adj[u]) dfs(v);
		tout[u] = timeDfs;
	};
	dfs(nw - 1);
	lab.assign(N, -1);
	vector <set <int>> s(N);

	vector <int> res(Q);
	REP(i, N) {
		int u;
		s[i].insert(tin[i]);
		for (int v: adj[i]) if (v <= i) {
			if ((u = find(i)) == (v = find(v))) continue;
			if (s[u].size() < s[v].size()) swap(u, v);
			lab[u] += lab[v];
			lab[v] = u;
			for (int x: s[v]) s[u].insert(x);
			s[v].clear();
		}
		for (int id: R_at[i]) {
			auto it = s[find(E[id])].lower_bound(tin[S[id]]);
			res[id] = it != s[find(E[id])].end() && *it <= tout[S[id]];
		}
	}
	return res;
}

#ifdef LOCAL
#include <cstdio>
#include <cstdlib>
#include <vector>
#include "werewolf.h"

namespace {

int read_int() {
  int x;
  if (scanf("%d", &x) != 1) {
    fprintf(stderr, "Error while reading input\n");
    exit(1);
  }
  return x;
}

}  // namespace

int main() {
	file("werewolf");
  int N = read_int();
  int M = read_int();
  int Q = read_int();
  std::vector<int> X(M), Y(M), S(Q), E(Q), L(Q), R(Q);
  for (int j = 0; j < M; ++j) {
    X[j] = read_int();
    Y[j] = read_int();
  }
  for (int i = 0; i < Q; ++i) {
    S[i] = read_int();
    E[i] = read_int();
    L[i] = read_int();
    R[i] = read_int();
  }

  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]);
  }
  return 0;
}
#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...