Submission #422151

# Submission time Handle Problem Language Result Execution time Memory
422151 2021-06-09T19:00:44 Z MohamedAhmed04 Werewolf (IOI18_werewolf) C++14
100 / 100
995 ms 128324 KB
#include <bits/stdc++.h>
#include "werewolf.h"
//#include "grader.cpp"

using namespace std ;

const int MAX = 2e5 + 10 ;

int n , m , q ;

int par[MAX] ;
int par2[MAX] ;

int P[MAX][20] , P2[MAX][20] ;

vector< vector<int> >adj(MAX) , adj1(MAX) , adj2(MAX) ;

void init()
{
	for(int i = 1 ; i <= n ; ++i)
		par[i] = par2[i] = i ;
}


int root(int node)
{
	if(par[node] == node)
		return node ;
	return (par[node] = root(par[node])) ;
}

void Union(int x , int y)
{
	int a = root(x) ;
	int b = root(y) ;
	if(a < b)
		swap(a , b) ;
	par[b] = a , P[b][0] = a ;
	adj1[a].push_back(b) ;
}

int root2(int node)
{
	if(par2[node] == node)
		return node ;
	return (par2[node] = root2(par2[node])) ;
}

void Union2(int x , int y)
{
	int a = root2(x) ;
	int b = root2(y) ;
	if(a > b)
		swap(a , b) ;
	par2[b] = a , P2[b][0] = a ;
	adj2[a].push_back(b) ;
}

int in[MAX] , out[MAX] ;
int tim = 0 ;

void dfs1(int node)
{
	in[node] = ++tim ;
	for(int j = 1 ; j < 18 ; ++j)
		P[node][j] = P[P[node][j-1]][j-1] ;
	for(auto &child : adj1[node])
		dfs1(child) ;
	out[node] = tim ;
}

int sz[MAX] ;

void dfs2(int node)
{
	for(int j = 1 ; j < 18 ; ++j)
		P2[node][j] = P2[P2[node][j-1]][j-1] ;
	sz[node] = 1 ;
	for(auto &child : adj2[node])
	{
		dfs2(child) ;
		sz[node] += sz[child] ;
	}
}

void preprocess()
{
	init() ;
	for(int i = 0 ; i < n ; ++i)
	{
		for(auto &child : adj[i])
		{
			if(child < i && root(child) != root(i))
				Union(child , i) ;
		}
	}
	for(int i = n-1 ; i >= 0 ; --i)
	{
		for(auto &child : adj[i])
		{
			if(child > i && root2(child) != root2(i))
				Union2(child , i) ;
		}
	}
	P[n-1][0] = n-1 ;
	dfs1(n-1) ;
	dfs2(0) ;
}

int get(int node , int node2)
{
	for(int j = 17 ; j >= 0 ; --j)
	{
		if(P[node][j] <= node2)
			node = P[node][j] ;
	}
	return node ;
}

int get2(int node , int node2)
{
	for(int j = 17 ; j >= 0 ; --j)
	{
		if(P2[node][j] >= node2)
			node = P2[node][j] ;
	}
	return node ;
}

set<int>s[MAX] ;
vector< pair<int , int> >queries[MAX] ;
vector<int>ans ;

void dfs3(int node)
{
	int big = -1 , bigchild = -1 ;
	for(auto &child : adj2[node])
	{
		if(sz[child] > big)
			big = sz[child] , bigchild = child ;
	}
	for(auto &child : adj2[node])
		dfs3(child) ;
	if(bigchild != -1)
		swap(s[node] , s[bigchild]) ;
	s[node].insert(in[node]) ;
	for(auto &child : adj2[node])
	{
		if(child == bigchild)
			continue ;
		for(auto &i : s[child])
			s[node].insert(i) ;
		s[child].clear() ;
	}
	for(auto &p : queries[node])
	{
		int l = in[p.first] , r = out[p.first] ;
		auto it = s[node].lower_bound(l) ;
		if(it == s[node].end())
			ans[p.second] = 0 ;
		else if((*it) > r)
			ans[p.second] = 0 ;
		else
			ans[p.second] = 1 ;
	}
}

std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
                                std::vector<int> S, std::vector<int> E,
                                std::vector<int> L, std::vector<int> R) 
{
	n = N , m = X.size() , q = S.size() ;
	for(int i = 0 ; i < m ; ++i)
		adj[X[i]].push_back(Y[i]) , adj[Y[i]].push_back(X[i]) ;
	preprocess() ;
	for(int i = 0 ; i < q ; ++i)
		queries[get2(S[i] , L[i])].emplace_back(get(E[i] , R[i]) , i) ;
	ans = vector<int>(q) ;
	dfs3(0) ;
	return ans ;
}
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28564 KB Output is correct
2 Correct 15 ms 28460 KB Output is correct
3 Correct 16 ms 28452 KB Output is correct
4 Correct 15 ms 28424 KB Output is correct
5 Correct 16 ms 28508 KB Output is correct
6 Correct 16 ms 28492 KB Output is correct
7 Correct 16 ms 28484 KB Output is correct
8 Correct 16 ms 28448 KB Output is correct
9 Correct 16 ms 28512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28564 KB Output is correct
2 Correct 15 ms 28460 KB Output is correct
3 Correct 16 ms 28452 KB Output is correct
4 Correct 15 ms 28424 KB Output is correct
5 Correct 16 ms 28508 KB Output is correct
6 Correct 16 ms 28492 KB Output is correct
7 Correct 16 ms 28484 KB Output is correct
8 Correct 16 ms 28448 KB Output is correct
9 Correct 16 ms 28512 KB Output is correct
10 Correct 22 ms 29772 KB Output is correct
11 Correct 21 ms 29692 KB Output is correct
12 Correct 22 ms 29768 KB Output is correct
13 Correct 22 ms 29772 KB Output is correct
14 Correct 22 ms 29816 KB Output is correct
15 Correct 23 ms 29884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 846 ms 105812 KB Output is correct
2 Correct 780 ms 103536 KB Output is correct
3 Correct 740 ms 110252 KB Output is correct
4 Correct 733 ms 109888 KB Output is correct
5 Correct 726 ms 110400 KB Output is correct
6 Correct 804 ms 111896 KB Output is correct
7 Correct 835 ms 112308 KB Output is correct
8 Correct 659 ms 111944 KB Output is correct
9 Correct 607 ms 109620 KB Output is correct
10 Correct 612 ms 109524 KB Output is correct
11 Correct 642 ms 109276 KB Output is correct
12 Correct 709 ms 110380 KB Output is correct
13 Correct 780 ms 128056 KB Output is correct
14 Correct 794 ms 128152 KB Output is correct
15 Correct 823 ms 128096 KB Output is correct
16 Correct 793 ms 128120 KB Output is correct
17 Correct 836 ms 112316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28564 KB Output is correct
2 Correct 15 ms 28460 KB Output is correct
3 Correct 16 ms 28452 KB Output is correct
4 Correct 15 ms 28424 KB Output is correct
5 Correct 16 ms 28508 KB Output is correct
6 Correct 16 ms 28492 KB Output is correct
7 Correct 16 ms 28484 KB Output is correct
8 Correct 16 ms 28448 KB Output is correct
9 Correct 16 ms 28512 KB Output is correct
10 Correct 22 ms 29772 KB Output is correct
11 Correct 21 ms 29692 KB Output is correct
12 Correct 22 ms 29768 KB Output is correct
13 Correct 22 ms 29772 KB Output is correct
14 Correct 22 ms 29816 KB Output is correct
15 Correct 23 ms 29884 KB Output is correct
16 Correct 846 ms 105812 KB Output is correct
17 Correct 780 ms 103536 KB Output is correct
18 Correct 740 ms 110252 KB Output is correct
19 Correct 733 ms 109888 KB Output is correct
20 Correct 726 ms 110400 KB Output is correct
21 Correct 804 ms 111896 KB Output is correct
22 Correct 835 ms 112308 KB Output is correct
23 Correct 659 ms 111944 KB Output is correct
24 Correct 607 ms 109620 KB Output is correct
25 Correct 612 ms 109524 KB Output is correct
26 Correct 642 ms 109276 KB Output is correct
27 Correct 709 ms 110380 KB Output is correct
28 Correct 780 ms 128056 KB Output is correct
29 Correct 794 ms 128152 KB Output is correct
30 Correct 823 ms 128096 KB Output is correct
31 Correct 793 ms 128120 KB Output is correct
32 Correct 836 ms 112316 KB Output is correct
33 Correct 862 ms 111904 KB Output is correct
34 Correct 321 ms 62792 KB Output is correct
35 Correct 920 ms 116644 KB Output is correct
36 Correct 853 ms 111936 KB Output is correct
37 Correct 926 ms 115344 KB Output is correct
38 Correct 880 ms 112960 KB Output is correct
39 Correct 904 ms 118508 KB Output is correct
40 Correct 949 ms 125272 KB Output is correct
41 Correct 778 ms 113384 KB Output is correct
42 Correct 736 ms 110256 KB Output is correct
43 Correct 995 ms 122988 KB Output is correct
44 Correct 831 ms 114624 KB Output is correct
45 Correct 782 ms 116996 KB Output is correct
46 Correct 852 ms 116900 KB Output is correct
47 Correct 816 ms 128260 KB Output is correct
48 Correct 824 ms 128100 KB Output is correct
49 Correct 832 ms 128324 KB Output is correct
50 Correct 818 ms 128116 KB Output is correct
51 Correct 907 ms 125788 KB Output is correct
52 Correct 894 ms 125756 KB Output is correct