104422024-04-02 17:03:32111Labirintuscpp17Wrong answer 0/100388ms189116 KiB
#include<bits/stdc++.h>
using namespace std;

// #define int long long

struct DSU{
	vector<int>v;
	vector<pair<int,int>>s;
	
	DSU(){
	}
	
	DSU(int n):v(n,-1){
	}
	
	int find(int i){
		return v[i]<0?i:find(v[i]);
	}
	
	void unite(int a,int b){
		a=find(a);
		b=find(b);
		if(a==b){
			s.emplace_back(-1,-1);
			s.emplace_back(-1,-1);
			return;
		}
		if(a>b){
			swap(a,b);
		}
		s.emplace_back(a,v[a]);
		s.emplace_back(b,v[b]);
		v[a]+=v[b];
		v[b]=a;
	}
	
	void undo(){
		for(int i=0;i<2;i++){
			auto[a,aa]=s.back();
			s.pop_back();
			if(a!=-1){
				v[a]=aa;
			}
		}
	}
};

int R,C;
DSU d;

int x(int r,int c){
	return r*(C+1)+c;
}

void init_labyrinth(int R_,int C_,std::vector<std::vector<int>>L){
	R=R_,C=C_;
	d=DSU((R+1)*(C+1));
	for(int i=0;i+1<C;i++){
		d.unite(x(R,i),x(R,i+1));
	}
	for(int i=0;i+1<R;i++){
		d.unite(x(i,C),x(i+1,C));
	}
	for(int i=0;i<R;i++){
		for(int j=0;j<C;j++){
			if(L[i][j]){
				d.unite(x(i,j),x(i,j+1));
				d.unite(x(i,j),x(i+1,j));
				d.unite(x(i,j),x(i+1,j+1));
			}
		}
	}
}

bool can_escape(int N,std::vector<int>U,std::vector<int>V){
	for(int k=0;k<N;k++){
		int i=U[k],j=V[k];
		d.unite(x(i,j),x(i,j+1));
		d.unite(x(i,j),x(i+1,j));
		d.unite(x(i,j),x(i+1,j+1));
	}
	int ans=d.find(x(R,C-1))!=d.find(x(R-1,C));
	for(int k=0;k<N;k++){
		d.undo();
		d.undo();
		d.undo();
	}
	return ans;
}

#ifdef LOCAL
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int R,C,Q;
	cin>>R>>C>>Q;
	vector<vector<int>>L(R,vector<int>(C));
	for(int i=0;i<R;i++){
		for(int j=0;j<C;j++){
			char c;
			cin>>c;
			L[i][j]=c-'0';
		}
	}
	init_labyrinth(R,C,L);
	while(Q--){
		int N;
		cin>>N;
		vector<int>U(N),V(N);
		for(int i=0;i<N;i++){
			cin>>U[i]>>V[i];
		}
		cout<<can_escape(N,U,V)<<'\n';
	}
	return 0;
}
#endif
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1808 KiB
2Wrong answer182ms92356 KiB
subtask20/15
3Wrong answer28ms7412 KiB
4Wrong answer8ms7836 KiB
5Wrong answer125ms8528 KiB
6Wrong answer125ms10696 KiB
7Wrong answer83ms15484 KiB
8Wrong answer85ms17412 KiB
subtask30/18
9Wrong answer186ms18912 KiB
10Wrong answer187ms21452 KiB
11Wrong answer388ms110952 KiB
12Wrong answer363ms115916 KiB
13Wrong answer368ms120548 KiB
14Wrong answer386ms125076 KiB
subtask40/28
15Accepted263ms129948 KiB
16Accepted275ms133736 KiB
17Accepted277ms136312 KiB
18Accepted167ms140760 KiB
19Wrong answer272ms142376 KiB
20Accepted143ms146880 KiB
21Accepted148ms147984 KiB
22Wrong answer270ms149116 KiB
23Wrong answer266ms153836 KiB
subtask50/39
24Wrong answer37ms68476 KiB
25Wrong answer18ms68540 KiB
26Wrong answer126ms72136 KiB
27Wrong answer126ms74476 KiB
28Wrong answer123ms77324 KiB
29Wrong answer123ms77428 KiB
30Wrong answer370ms166472 KiB
31Wrong answer340ms169884 KiB
32Wrong answer244ms173448 KiB
33Wrong answer351ms175060 KiB
34Wrong answer245ms178852 KiB
35Wrong answer234ms180744 KiB
36Wrong answer289ms182696 KiB
37Wrong answer287ms185476 KiB
38Accepted293ms187404 KiB
39Wrong answer289ms189116 KiB