# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
120140 | Boxworld | 늑대인간 (IOI18_werewolf) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;
//#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
typedef vector<int> Vec;
const int maxN=400100;
Vec G[maxN];
int Fp[maxN],Fw[maxN],vis1[maxN],vis2[maxN],a[maxN];
int find1(int x){return x==Fp[x]?x:Fp[x]=find1(Fp[x]);}
int find2(int x){return x==Fw[x]?x:Fw[x]=find2(Fw[x]);}
void uni1(int x,int y){
int Fx=find1(x),Fy=find1(y);
if(Fx==Fy)return;
Fp[Fx]=Fy;
}
void uni2(int x,int y){
int Fx=find2(x),Fy=find2(y);
if(Fx==Fy)return;
Fw[Fx]=Fy;
}
void dfs1(int x,int l){
for (int i=0;i<a[x];i++)
if (!vis1[G[x][i]]&&G[x][i]>=l){
vis1[G[x][i]]=1;
dfs1(G[x][i],l);
}
}
bool dfs2(int x,int l,int r){
for (int i=0;i<a[x];i++)
if (!vis2[G[x][i]]&&G[x][i]<=r){
vis2[G[x][i]]=1;
if (l<=G[x][i]&&G[x][i]<=r)return 1;
else return dfs2(G[x][i],l,r);
}
}
Vec check_validity(int N,Vec X,Vec Y,Vec S,Vec E,Vec L,Vec R) {
int M=X.size(),Q=S.size();
Vec A;
A.clear();
if(M==N-1){
for (int q=0;q<Q;q++){
memset(a,0,sizeof(a));
memset(vis1,0,sizeof(vis1));
memset(vis2,0,sizeof(vis2));
for (int i=0;i<M;i++){
G[X[i]].push_back(Y[i]);
G[Y[i]].push_back(X[i]);
a[X[i]]++;a[Y[i]]++;
}
dfs1(S[q],L[q]);//people
if(dfs2(E[q],L[q],R[q]))A.push_back(1);//werewolf
else A.push_back(0);
}
}else{
for (int q=0;q<Q;q++){
for (int i=0;i<N;i++)Fp[i]=i,Fw[i]=i;
for (int i=0;i<M;i++){
if (X[i]>=L[q]&&Y[i]>=L[q])uni1(X[i],Y[i]);
if (X[i]<=R[q]&&Y[i]<=R[q])uni2(X[i],Y[i]);
}
int no=1;
for (int i=L[q];i<=R[q];i++)
if (find1(Fp[S[q]])==find1(Fp[i])&&find2(Fw[E[q]])==find2(Fw[i])){
no=0;A.push_back(1);break;
}
if(no)A.push_back(0);
}
}
return A;
}
int main() {
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 (int i = 0; i < A.size(); ++i) {
printf("%d\n", A[i]);
}
return 0;
}