Submission #619307

# Submission time Handle Problem Language Result Execution time Memory
619307 2022-08-02T11:04:13 Z MohamedAhmed04 Toy Train (IOI17_train) C++14
100 / 100
1871 ms 2700 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] ;

queue<int>q ;
bool ok = true ;

void Construct_Graph()
{
	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)
	{
		vis[i] = 0 ;
		for(auto &child : adj2[i])
			adj2r[child].push_back(i) ;
	}
	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
				{
					ok |= ans[i] ;
					ans[i] = 0 ;
				}
			}
			continue ;
		}
		for(auto &node : v)
			ok |= ans[node] , ans[node] = 0 ;
	}
}

void solve()
{
	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]))
			{
				ok = true ;
				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] ;
	while(ok)
	{
		ok = false ;
		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 6 ms 1620 KB Output is correct
2 Correct 7 ms 1620 KB Output is correct
3 Correct 7 ms 1572 KB Output is correct
4 Correct 7 ms 1640 KB Output is correct
5 Correct 6 ms 1620 KB Output is correct
6 Correct 7 ms 1620 KB Output is correct
7 Correct 5 ms 1748 KB Output is correct
8 Correct 6 ms 1492 KB Output is correct
9 Correct 7 ms 1620 KB Output is correct
10 Correct 5 ms 1492 KB Output is correct
11 Correct 5 ms 1620 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 724 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 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 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 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 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 2584 KB Output is correct
2 Correct 430 ms 2644 KB Output is correct
3 Correct 723 ms 2664 KB Output is correct
4 Correct 8 ms 2004 KB Output is correct
5 Correct 11 ms 2004 KB Output is correct
6 Correct 19 ms 2348 KB Output is correct
7 Correct 9 ms 1896 KB Output is correct
8 Correct 10 ms 2212 KB Output is correct
9 Correct 11 ms 2388 KB Output is correct
10 Correct 9 ms 2004 KB Output is correct
11 Correct 9 ms 2044 KB Output is correct
12 Correct 14 ms 2216 KB Output is correct
13 Correct 9 ms 1948 KB Output is correct
14 Correct 9 ms 1956 KB Output is correct
15 Correct 8 ms 2004 KB Output is correct
16 Correct 8 ms 2004 KB Output is correct
17 Correct 8 ms 1956 KB Output is correct
18 Correct 1166 ms 2076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1620 KB Output is correct
2 Correct 9 ms 1720 KB Output is correct
3 Correct 15 ms 2040 KB Output is correct
4 Correct 11 ms 2180 KB Output is correct
5 Correct 12 ms 1992 KB Output is correct
6 Correct 8 ms 1868 KB Output is correct
7 Correct 13 ms 1876 KB Output is correct
8 Correct 15 ms 2116 KB Output is correct
9 Correct 11 ms 1748 KB Output is correct
10 Correct 8 ms 1748 KB Output is correct
11 Correct 9 ms 1800 KB Output is correct
12 Correct 8 ms 1808 KB Output is correct
13 Correct 12 ms 2004 KB Output is correct
14 Correct 9 ms 1728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1876 KB Output is correct
2 Correct 10 ms 1740 KB Output is correct
3 Correct 11 ms 1748 KB Output is correct
4 Correct 11 ms 1720 KB Output is correct
5 Correct 1 ms 852 KB Output is correct
6 Correct 6 ms 1748 KB Output is correct
7 Correct 5 ms 1364 KB Output is correct
8 Correct 9 ms 1620 KB Output is correct
9 Correct 6 ms 1568 KB Output is correct
10 Correct 2 ms 980 KB Output is correct
11 Correct 5 ms 1436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1620 KB Output is correct
2 Correct 7 ms 1620 KB Output is correct
3 Correct 7 ms 1572 KB Output is correct
4 Correct 7 ms 1640 KB Output is correct
5 Correct 6 ms 1620 KB Output is correct
6 Correct 7 ms 1620 KB Output is correct
7 Correct 5 ms 1748 KB Output is correct
8 Correct 6 ms 1492 KB Output is correct
9 Correct 7 ms 1620 KB Output is correct
10 Correct 5 ms 1492 KB Output is correct
11 Correct 5 ms 1620 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 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 724 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 724 KB Output is correct
25 Correct 1 ms 724 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 724 KB Output is correct
32 Correct 173 ms 2584 KB Output is correct
33 Correct 430 ms 2644 KB Output is correct
34 Correct 723 ms 2664 KB Output is correct
35 Correct 8 ms 2004 KB Output is correct
36 Correct 11 ms 2004 KB Output is correct
37 Correct 19 ms 2348 KB Output is correct
38 Correct 9 ms 1896 KB Output is correct
39 Correct 10 ms 2212 KB Output is correct
40 Correct 11 ms 2388 KB Output is correct
41 Correct 9 ms 2004 KB Output is correct
42 Correct 9 ms 2044 KB Output is correct
43 Correct 14 ms 2216 KB Output is correct
44 Correct 9 ms 1948 KB Output is correct
45 Correct 9 ms 1956 KB Output is correct
46 Correct 8 ms 2004 KB Output is correct
47 Correct 8 ms 2004 KB Output is correct
48 Correct 8 ms 1956 KB Output is correct
49 Correct 1166 ms 2076 KB Output is correct
50 Correct 7 ms 1620 KB Output is correct
51 Correct 9 ms 1720 KB Output is correct
52 Correct 15 ms 2040 KB Output is correct
53 Correct 11 ms 2180 KB Output is correct
54 Correct 12 ms 1992 KB Output is correct
55 Correct 8 ms 1868 KB Output is correct
56 Correct 13 ms 1876 KB Output is correct
57 Correct 15 ms 2116 KB Output is correct
58 Correct 11 ms 1748 KB Output is correct
59 Correct 8 ms 1748 KB Output is correct
60 Correct 9 ms 1800 KB Output is correct
61 Correct 8 ms 1808 KB Output is correct
62 Correct 12 ms 2004 KB Output is correct
63 Correct 9 ms 1728 KB Output is correct
64 Correct 9 ms 1876 KB Output is correct
65 Correct 10 ms 1740 KB Output is correct
66 Correct 11 ms 1748 KB Output is correct
67 Correct 11 ms 1720 KB Output is correct
68 Correct 1 ms 852 KB Output is correct
69 Correct 6 ms 1748 KB Output is correct
70 Correct 5 ms 1364 KB Output is correct
71 Correct 9 ms 1620 KB Output is correct
72 Correct 6 ms 1568 KB Output is correct
73 Correct 2 ms 980 KB Output is correct
74 Correct 5 ms 1436 KB Output is correct
75 Correct 298 ms 2404 KB Output is correct
76 Correct 459 ms 2660 KB Output is correct
77 Correct 788 ms 2568 KB Output is correct
78 Correct 802 ms 2632 KB Output is correct
79 Correct 768 ms 2700 KB Output is correct
80 Correct 10 ms 2004 KB Output is correct
81 Correct 13 ms 2260 KB Output is correct
82 Correct 18 ms 2124 KB Output is correct
83 Correct 16 ms 1972 KB Output is correct
84 Correct 18 ms 2132 KB Output is correct
85 Correct 15 ms 2328 KB Output is correct
86 Correct 15 ms 2244 KB Output is correct
87 Correct 13 ms 2388 KB Output is correct
88 Correct 26 ms 2288 KB Output is correct
89 Correct 16 ms 2204 KB Output is correct
90 Correct 27 ms 2360 KB Output is correct
91 Correct 27 ms 2380 KB Output is correct
92 Correct 52 ms 2276 KB Output is correct
93 Correct 49 ms 2212 KB Output is correct
94 Correct 52 ms 2324 KB Output is correct
95 Correct 52 ms 2240 KB Output is correct
96 Correct 130 ms 2024 KB Output is correct
97 Correct 1177 ms 2364 KB Output is correct
98 Correct 1642 ms 2352 KB Output is correct
99 Correct 1871 ms 2380 KB Output is correct
100 Correct 1144 ms 2200 KB Output is correct