Submission #619290

# Submission time Handle Problem Language Result Execution time Memory
619290 2022-08-02T10:58:26 Z MohamedAhmed04 Toy Train (IOI17_train) C++14
15 / 100
2000 ms 2388 KB
#include "train.h"
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 5000 + 10 ;

int arr[MAX] , mark[MAX] , charging[MAX] ;
int n , m ;

vector< vector<int> >adj(MAX) , adjr(MAX) ;
vector< vector<int> >adj2(MAX) , adj2r(MAX) ;

int vis[MAX] , ans[MAX] ;
int deg[MAX] , deg2[MAX] ;

void Construct_Graph()
{
	queue<int>q ;
	for(int i = 1 ; i <= n ; ++i)
	{
		mark[i] = 0 , deg[i] = deg2[i] ;
		if(charging[i] && ans[i])
			q.push(i) , mark[i] = 1 ;
	}
	while(!q.empty())
	{
		int node = q.front() ;
		q.pop() ;
		for(auto &child : adjr[node])
		{
			deg[child]-- ;
			if(mark[child])
				continue ;
			if((arr[child]) || (!deg[child]))
			{
				mark[child] = 1 ;
				q.push(child) ;
			}
		}
	}
	for(int i = 1 ; i <= n ; ++i)
	{
		adj2[i].clear() , adj2r[i].clear() ;
		if(mark[i])
			continue ;
		for(auto &child : adj[i])
		{
			if(!mark[child])
			{
				adj2[i].push_back(child) ;
			}
		}
	}
}

// don't forget self-loop

vector<int>v , topo ;

void dfs(int node)
{
	vis[node] = 1 ;
	for(auto &child : adj2r[node])
	{
		if(!vis[child])
			dfs(child) ;
	}
	topo.push_back(node) ;
}

void dfs2(int node)
{
	vis[node] = 1 ;
	v.push_back(node) ;
	for(auto &child : adj2[node])
	{
		if(!vis[child])
			dfs2(child) ;
	}
}

void Find_Cycles()
{
	topo.clear() ;
	for(int i = 1 ; i <= n ; ++i)
	{
		for(auto &child : adj2[i])
			adj2r[child].push_back(i) ;
	}
	memset(vis , 0 , sizeof(vis)) ;
	for(int i = 1 ; i <= n ; ++i)
	{
		if(!vis[i])
			dfs(i) ;
	}
	reverse(topo.begin() , topo.end()) ;
	memset(vis , 0 , sizeof(vis)) ;
	for(auto &i : topo)
	{
		if(vis[i])
			continue ;
		v.clear() ;
		dfs2(i) ;
		if(v.size() == 1)
		{
			for(auto &child : adj2[i])
			{
				if(child == i) //self loop
					ans[i] = 0 ;
			}
			continue ;
		}
		for(auto &node : v)
			ans[node] = 0 ;
	}
}

void solve()
{
	queue<int>q ;
	for(int i = 1 ; i <= n ; ++i)
	{
		deg[i] = deg2[i] ;
		if(!ans[i])
			q.push(i) ;
	}
	while(!q.empty())
	{
		int node = q.front() ;
		q.pop() ;
		for(auto &child : adjr[node])
		{
			deg[child]-- ;
			if(!ans[child])
				continue ;
			if((!arr[child]) || (!deg[child]))
			{
				ans[child] = 0 ;
				q.push(child) ;
			}
		}
	}
}

std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) 
{
	n = a.size() , m = u.size() ;
	for(int i = 1 ; i <= n ; ++i)
		ans[i] = 1 , arr[i] = a[i-1] , charging[i] = r[i-1] ;
	for(int i = 0 ; i < m ; ++i)
		deg[u[i]+1]++ , adj[u[i]+1].push_back(v[i]+1) , adjr[v[i]+1].push_back(u[i]+1) ;
	for(int i = 1 ; i <= n ; ++i)
		deg2[i] = deg[i] ;
	for(int _ = 0 ; _ < n ; ++_)
	{
		Construct_Graph() ;
		Find_Cycles() ;
		solve() ;
	}
	vector<int>ans2 ;
	for(int i = 1 ; i <= n ; ++i)
		ans2.push_back(ans[i]) ;
	return ans2 ;
}
# Verdict Execution time Memory Grader output
1 Correct 1862 ms 1636 KB Output is correct
2 Correct 1790 ms 1664 KB Output is correct
3 Correct 1838 ms 1652 KB Output is correct
4 Correct 1852 ms 1616 KB Output is correct
5 Correct 1844 ms 1656 KB Output is correct
6 Correct 1808 ms 1564 KB Output is correct
7 Correct 1925 ms 1772 KB Output is correct
8 Correct 1350 ms 1496 KB Output is correct
9 Correct 1810 ms 1580 KB Output is correct
10 Correct 1896 ms 1620 KB Output is correct
11 Correct 1804 ms 1640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 784 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 2 ms 724 KB Output is correct
9 Correct 1 ms 784 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 780 KB Output is correct
14 Correct 1 ms 784 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2078 ms 2388 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1358 ms 1596 KB Output is correct
2 Execution timed out 2045 ms 1620 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2090 ms 1876 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1862 ms 1636 KB Output is correct
2 Correct 1790 ms 1664 KB Output is correct
3 Correct 1838 ms 1652 KB Output is correct
4 Correct 1852 ms 1616 KB Output is correct
5 Correct 1844 ms 1656 KB Output is correct
6 Correct 1808 ms 1564 KB Output is correct
7 Correct 1925 ms 1772 KB Output is correct
8 Correct 1350 ms 1496 KB Output is correct
9 Correct 1810 ms 1580 KB Output is correct
10 Correct 1896 ms 1620 KB Output is correct
11 Correct 1804 ms 1640 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 784 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 2 ms 724 KB Output is correct
20 Correct 1 ms 784 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 724 KB Output is correct
24 Correct 1 ms 780 KB Output is correct
25 Correct 1 ms 784 KB Output is correct
26 Correct 1 ms 724 KB Output is correct
27 Correct 1 ms 724 KB Output is correct
28 Correct 1 ms 724 KB Output is correct
29 Correct 1 ms 724 KB Output is correct
30 Correct 1 ms 724 KB Output is correct
31 Correct 1 ms 784 KB Output is correct
32 Execution timed out 2078 ms 2388 KB Time limit exceeded
33 Halted 0 ms 0 KB -