Submission #242480

# Submission time Handle Problem Language Result Execution time Memory
242480 2020-06-27T19:56:37 Z MohamedAhmed04 Tropical Garden (IOI11_garden) C++14
100 / 100
2777 ms 35820 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"
//#include "grader.cpp" 

using namespace std ;

const int MAX = 2 * 150010 ; 

int mark[MAX] , par[MAX] , vis[MAX] , to[MAX] , best[MAX] , best2[MAX] , dist[MAX][2] , cycleid[MAX] , cycle_sz[MAX] , edge[MAX][2] ;

vector< vector<int> >rev_adj(MAX) ;

int n , m ;

void Addedge(int x , int y , int val)
{
	if(best[x] == val)
	{
		if(best[y] == val)
			to[x] = y+n ;
		else
			to[x] = y ;
	}
	else if(best2[x] == val)
	{
		if(best[y] == val)
			to[x+n] = y+n ;
		else
			to[x+n] = y ;	
	}		
}

void ConstructGraph()
{
	for(int i = 0 ; i < m ; ++i)
	{
		int x = edge[i][0] ;
		int y = edge[i][1] ;
		int val = i+1 ;
		++x , ++y ;
		if(!best[x])
			best[x] = val ;
		else if(!best2[x])
			best2[x] = val ;
		if(!best[y])
			best[y] = val ;
		else if(!best2[y])
			best2[y] = val ;
		Addedge(x , y , val) ;
		Addedge(y , x , val) ;	
	}
}

void bfs(int src , bool t)
{
	for(int i = 1 ; i <= n+n ; ++i)
		dist[i][t] = -1 ;
	dist[src][t] = 0 ;
	queue<int>q ;
	q.push(src) ;
	while(!q.empty())
	{
		int node = q.front() ;
		q.pop() ;
		for(auto &child : rev_adj[node])
		{
			if(dist[child][t] == -1)
			{
				dist[child][t] = dist[node][t] + 1 ;
				q.push(child) ;
			}
		}
	}
}

int id = 0 ;

vector<int>v ;

void dfs(int node)
{
	vis[node] = 1 ;
	if(to[node])
	{
		if(mark[to[node]])
		{
			v.clear() ;
			++id ;
			int a = to[node] ;
			while(node != a)
			{
				v.push_back(node) ;
				node = par[node] ;
			}
			v.push_back(node) ;
			for(auto &i : v)
				cycleid[i] = id ;
			cycle_sz[id] = v.size() ;
			return ;
		}
		mark[node] = 1 ;
		if(!vis[to[node]])
		{
			par[to[node]] = node ;
			dfs(to[node]) ;
		}
	}
	mark[node] = 0 ;
}

void find_cycle()
{
	for(int i = 1 ; i <= n+n ; ++i)
	{
		if(vis[i])
			continue ;
		dfs(i) ;
	}
} 

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
	for(int i = 0 ; i < M ; ++i)
		edge[i][0] = R[i][0] , edge[i][1] = R[i][1] ;
	P++ ;
	n = N , m = M ;
	ConstructGraph() ;
	for(int i = 1 ; i <= n+n ; ++i)
	{
		if(to[to[i]] == 0 && to[i] > 0)
			to[i] = to[i] - n ;
	}
	for(int i = 1 ; i <= n+n ; ++i)
		rev_adj[to[i]].push_back(i) ;
	bfs(P , 0) ;
	bfs(P+n , 1) ;
	find_cycle() ;
	for(int i = 0 ; i < Q ; ++i)
	{
		int x = G[i] ;
		int ans = 0 ;
		for(int j = 1 ; j <= n ; ++j)
		{
			bool flag = false ;
			if(dist[j][0] != -1)
			{
				if(dist[j][0] == x)
					flag = true ;
				if(cycleid[P] > 0 && dist[j][0] < x && (x-dist[j][0]) % cycle_sz[cycleid[P]] == 0)
					flag = true ;
			}
			if(dist[j][1] != -1)
			{
				if(dist[j][1] == x)
					flag = true ;
				if(cycleid[P+n] > 0 && dist[j][1] < x && (x-dist[j][1]) % cycle_sz[cycleid[P+n]] == 0)
					flag = true ;
			}
			ans += flag ;
		}
		answer(ans) ;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7680 KB Output is correct
2 Correct 10 ms 7544 KB Output is correct
3 Correct 9 ms 7544 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 10 ms 7680 KB Output is correct
7 Correct 9 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 12 ms 7680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7680 KB Output is correct
2 Correct 10 ms 7544 KB Output is correct
3 Correct 9 ms 7544 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 10 ms 7680 KB Output is correct
7 Correct 9 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 12 ms 7680 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 18 ms 10488 KB Output is correct
12 Correct 31 ms 12408 KB Output is correct
13 Correct 58 ms 26604 KB Output is correct
14 Correct 89 ms 22900 KB Output is correct
15 Correct 122 ms 23284 KB Output is correct
16 Correct 87 ms 19344 KB Output is correct
17 Correct 79 ms 18040 KB Output is correct
18 Correct 32 ms 12536 KB Output is correct
19 Correct 89 ms 23260 KB Output is correct
20 Correct 122 ms 23796 KB Output is correct
21 Correct 99 ms 19704 KB Output is correct
22 Correct 83 ms 18296 KB Output is correct
23 Correct 86 ms 24592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7680 KB Output is correct
2 Correct 10 ms 7544 KB Output is correct
3 Correct 9 ms 7544 KB Output is correct
4 Correct 9 ms 7424 KB Output is correct
5 Correct 9 ms 7424 KB Output is correct
6 Correct 10 ms 7680 KB Output is correct
7 Correct 9 ms 7424 KB Output is correct
8 Correct 9 ms 7552 KB Output is correct
9 Correct 12 ms 7680 KB Output is correct
10 Correct 9 ms 7424 KB Output is correct
11 Correct 18 ms 10488 KB Output is correct
12 Correct 31 ms 12408 KB Output is correct
13 Correct 58 ms 26604 KB Output is correct
14 Correct 89 ms 22900 KB Output is correct
15 Correct 122 ms 23284 KB Output is correct
16 Correct 87 ms 19344 KB Output is correct
17 Correct 79 ms 18040 KB Output is correct
18 Correct 32 ms 12536 KB Output is correct
19 Correct 89 ms 23260 KB Output is correct
20 Correct 122 ms 23796 KB Output is correct
21 Correct 99 ms 19704 KB Output is correct
22 Correct 83 ms 18296 KB Output is correct
23 Correct 86 ms 24592 KB Output is correct
24 Correct 10 ms 7552 KB Output is correct
25 Correct 94 ms 10624 KB Output is correct
26 Correct 115 ms 12408 KB Output is correct
27 Correct 2526 ms 26604 KB Output is correct
28 Correct 872 ms 22900 KB Output is correct
29 Correct 2777 ms 23284 KB Output is correct
30 Correct 1621 ms 19456 KB Output is correct
31 Correct 1574 ms 18040 KB Output is correct
32 Correct 130 ms 12408 KB Output is correct
33 Correct 862 ms 23540 KB Output is correct
34 Correct 2767 ms 23796 KB Output is correct
35 Correct 1734 ms 19832 KB Output is correct
36 Correct 1567 ms 18424 KB Output is correct
37 Correct 647 ms 24816 KB Output is correct
38 Correct 2213 ms 35820 KB Output is correct