답안 #257614

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257614 2020-08-04T13:35:37 Z arnold518 Joker (BOI20_joker) C++14
0 / 100
16 ms 19576 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 4e5;
const int SQ = 200;

struct Query
{
	int l, r, k;
	bool operator < (const Query &p) { return r>p.r; }
};

int N, M, Q;
pii E[MAXN+10];
Query A[MAXN+10];
vector<Query> B[MAXN+10];
int ans[MAXN+10];

struct UF
{
	int par[MAXN+10], sz[MAXN+10], opp[MAXN+10];
	bool state;
	vector<int> V;
	void init()
	{
		int i, j;
		for(i=1; i<=N; i++) par[i]=i, sz[i]=1, opp[i]=N+i;
		for(i=1; i<=N; i++) par[i+N]=i+N, sz[i+N]=1, opp[i+N]=i;
		V.clear(); state=true;
	}
	int Find(int x)
	{
		if(x==par[x]) return x;
		return par[x]=Find(par[x]);
	}
	void Union(int x, int y)
	{
		//printf("Union %d %d\n", x, y);

		x=Find(x); y=Find(y);
		int nx=opp[x], ny=opp[y];

		if(x==y)
		{
			if(state) V.push_back(-1);
			else V.push_back(-2);
			state=false;
			return;
		}

		if(sz[x]<sz[ny]) swap(x, ny);
		if(sz[y]<sz[nx]) swap(y, nx);
		V.push_back(opp[x]); V.push_back(opp[y]); V.push_back(ny); V.push_back(nx);
		par[ny]=x; sz[x]+=sz[ny];
		par[nx]=y; sz[y]+=sz[nx];
		opp[x]=y; opp[y]=x;
	}
	void restore()
	{
		int ny, nx, x, y, oppx, oppy;
		//printf("RESTORE\n");
		assert(!V.empty());
		if(V.back()<0)
		{
			if(V.back()==-1) state=true;
			V.pop_back();
		 	return;
		}
		nx=V.back(); V.pop_back();
		ny=V.back(); V.pop_back();
		oppy=V.back(); V.pop_back();
		oppx=V.back(); V.pop_back();
		x=par[ny]; y=par[nx];
		opp[x]=oppx; opp[y]=oppy;
		sz[x]-=sz[ny]; sz[y]-=sz[nx];
		par[ny]=ny; par[nx]=nx;
	}
}uf;


int main()
{
	int i, j, k;

	scanf("%d%d%d", &N, &M, &Q);
	for(i=1; i<=M; i++) scanf("%d%d", &E[i].first, &E[i].second);
	for(i=1; i<=Q; i++)
	{
		scanf("%d%d", &A[i].l, &A[i].r);
		A[i].k=i;
		B[A[i].l/SQ].push_back(A[i]);
	}

	uf.init();
	for(i=0, j=1; i<=N/SQ; i++)
	{
		int l=max(SQ*i, 1), r=min(SQ*i+SQ-1, N);
		sort(B[i].begin(), B[i].end());

		for(; j<l; j++) uf.Union(E[j].first, E[j].second);
		int p=M, q;
		for(auto it : B[i])
		{
			for(; p>it.r; p--) uf.Union(E[p].first, E[p].second);
			for(q=l; q<it.l; q++) uf.Union(E[q].first, E[q].second);
			ans[it.k]=uf.state;
			for(q=l; q<it.l; q++) uf.restore();
		}
		for(p++; p<=N; p++) uf.restore();
	}
	for(i=1; i<=Q; i++)
	{
		if(!ans[i]) printf("YES\n");
		else printf("NO\n");
	}
}

Compilation message

Joker.cpp: In member function 'void UF::init()':
Joker.cpp:30:10: warning: unused variable 'j' [-Wunused-variable]
   int i, j;
          ^
Joker.cpp: In function 'int main()':
Joker.cpp:101:23: warning: unused variable 'r' [-Wunused-variable]
   int l=max(SQ*i, 1), r=min(SQ*i+SQ-1, N);
                       ^
Joker.cpp:87:12: warning: unused variable 'k' [-Wunused-variable]
  int i, j, k;
            ^
Joker.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d%d", &N, &M, &Q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:90:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=M; i++) scanf("%d%d", &E[i].first, &E[i].second);
                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:93:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &A[i].l, &A[i].r);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 19576 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 19576 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 19576 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 19576 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 19576 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 19576 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -