답안 #553483

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
553483 2022-04-26T02:56:56 Z MohamedAhmed04 Worst Reporter 3 (JOI18_worst_reporter3) C++14
0 / 100
35 ms 8480 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 1e5 + 10 ;
const int SQRT = 317 ;

int arr[MAX] ;
int n , m , q ;

vector< vector<int> >adj(MAX) ;
int vis[MAX] ;

array< pair<int , int> , SQRT>dp[MAX] ;

vector<int>topo ;

array< pair<int , int> , SQRT>Merge(array< pair<int , int> , SQRT>&a , array< pair<int , int> , SQRT>&b)
{
	array< pair<int , int> , SQRT>res ;
	int i = 0 , j = 0 ;
	for(int k = 0 ; k < SQRT ; ++k)
	{
		if(j == SQRT)
			res[k] = a[i] , ++i ;
		else if(i == SQRT)
			res[k] = b[j] , ++j ;
		else if(a[i].first >= b[j].first)
			res[k] = a[i] , ++i ;
		else
			res[k] = b[j] , ++j ;
	}
	return res ;
}

void dfs(int node)
{
	vis[node] = 1 ;
	for(int i = 0 ; i < SQRT ; ++i)
		dp[node][i] = {-1e9 , -1} ;
	dp[node][0] = {0 , node} ;
	for(auto &child : adj[node])
	{
		if(!vis[child])
			dfs(child) ;
		array< pair<int , int> , SQRT>b = dp[child] ;
		for(int i = 0 ; i < SQRT ; ++i)
		{
			if(b[i].second == -1)
				break ;
			b[i].first++ ;
		}
		dp[node] = Merge(dp[node] , b) ;
	}
	topo.push_back(node) ;
}

int id = 0 ;

int mark[MAX] ;

int SolveSmall(int node)
{
	int ans = -1 ;
	for(int i = 0 ; i < SQRT ; ++i)
	{
		if(dp[node][i].second == -1)
			break ;
		if(mark[dp[node][i].second] != id)
		{
			ans = dp[node][i].first ;
			break ;					
		}
	}
	return ans ;
}

int dp2[MAX] ;

int SolveBig(int x)
{
	for(int i = 1 ; i <= n ; ++i)
		dp2[i] = -1e9 ;
	dp2[x] = 0 ;
	int ans = -1 ;
	for(auto &node : topo)
	{
		for(auto &child : adj[node])
			dp2[child] = max(dp2[child] , dp2[node] + 1) ;
		if(mark[node] != id)
			ans = max(ans , dp2[node]) ;
	}
	return ans ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>m>>q ;
	for(int i = 0 ; i < m ; ++i)
	{
		int x , y ;
		cin>>x>>y ;
		adj[y].push_back(x) ;
	}
	for(int i = 1 ; i <= n ; ++i)
	{
		if(vis[i])
			continue ;
		dfs(i) ;
	}
	reverse(topo.begin() , topo.end()) ;
	while(q--)
	{
		++id ;
		int node , sz ;
		cin>>node>>sz ;
		for(int i = 0 ; i < sz ; ++i)
		{
			int x ;
			cin>>x ;
			mark[x] = id ;			
		}
		if(sz < SQRT)
			cout<<SolveSmall(node)<<"\n" ;
		else
			cout<<SolveBig(node)<<"\n" ;
	}
	return 0 ;
}		
# 결과 실행 시간 메모리 Grader output
1 Runtime error 35 ms 8480 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 13 ms 5292 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 35 ms 8480 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -