제출 #412609

#제출 시각아이디문제언어결과실행 시간메모리
412609jeqchoTropical Garden (IOI11_garden)C++17
69 / 100
5041 ms84564 KiB
#include "garden.h"
#include "gardenlib.h"

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpi;

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)

#define pb push_back
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define fi first
#define se second

int const n=15e4+3;
vpi adj[n];
int const LOGK=31;
int nxt[n][2];
pii lift[n][LOGK][2];

bool dfs(int now, int k, int P)
{
	bool f=0;
	R0F(pw,LOGK)
	{
		int d=1<<pw;
		if(d>k)continue;
		pii res = lift[now][pw][f];
		now=res.fi;
		f=res.se;
		k-=d;
	}
	if(now==P)return 1;
	return 0;
}

void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
	F0R(i,M)
	{
		int u = R[i][0];
		int v = R[i][1];
		adj[u].pb({M-i,v});
		adj[v].pb({M-i,u});
	}
	F0R(i,N)
	{
		sort(all(adj[i]));
		reverse(all(adj[i]));
		F0R(j,2)
		{
			nxt[i][j]=-1;
		}
		if(!adj[i].empty())
		{
			nxt[i][0]=adj[i][0].se;
			nxt[i][1]=adj[i][0].se;
			if(sz(adj[i])>1)
			{
				nxt[i][1]=adj[i][1].se;
			}
		}
	}
	F0R(i,N)
	{
		if(nxt[i][0]==-1)
		{
			lift[i][0][0] = {i,0};
			lift[i][0][1] = {i,0};
		}
		else
		{
			lift[i][0][0].fi = nxt[i][0];
			if(nxt[nxt[i][0]][0] == i)
			{
				lift[i][0][0].se = 1;
			}
			else
			{
				lift[i][0][0].se = 0;
			}
			lift[i][0][1].fi = nxt[i][1];
			if(nxt[nxt[i][1]][0] == i)
			{
				lift[i][0][1].se = 1;
			}
			else
			{
				lift[i][0][1].se = 0;
			}
		}
	}
	FOR(j,1,LOGK)
	{
		F0R(i,N)
		{
			pii half = lift[i][j-1][0];
			lift[i][j][0]=lift[half.fi][j-1][half.se];
			half = lift[i][j-1][1];
			lift[i][j][1]=lift[half.fi][j-1][half.se];
		}
	}
	F0R(j,Q)
	{
		int ans=0;
		F0R(i,N)
		{
			ans += dfs(i,G[j],P);
		}
		answer(ans);
	}
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...