Submission #767280

# Submission time Handle Problem Language Result Execution time Memory
767280 2023-06-26T14:59:24 Z APROHACK Toy Train (IOI17_train) C++14
5 / 100
2000 ms 1388 KB
#include "train.h"
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define past 2
#define ancestor 1
#define unvisited 0
using namespace std;
int n;
vector<bool>owner, charging;
vector<int>adj[5001];
bool isCycle[5001][2] ; // 0 Negative 1 Positive

short int vis[5001];
vector<int>nodos;
void memss(){
	for(int i = 0 ; i < n ; i ++)vis[i] = 0;
}

bool dfs(int node){
	////cout << "were " << node << endl;
	bool isA = owner[node];
	bool isCharging = charging[node];
	vis[node] = 1;
	nodos.pb(node);
	
	for(int i = 0 ; i < adj[node].size() ; i ++){
		if(!vis[adj[node][i]]){
			if(dfs(adj[node][i]) == isA){
				nodos.pop_back();
				vis[node] = 0;
				return isA;
			}
		}else{
			bool hayC = false;
			for(int j = nodos.size()-1 ; j>= 0 ; j --){
				if(charging[nodos[j]])hayC = true;
				if(nodos[j] == adj[node][i])break;
			}
			if((hayC && isA) or (!hayC && !isA)){
				nodos.pop_back();
				vis[node] = 0;
			}
			if(hayC && isA)return true;
			else if(!hayC && !isA)return false;
		}
	}
	nodos.pop_back();
	vis[node] = 0;
	return !isA;
}

void recorrer(int node){
	vis[node] = ancestor;
	nodos.pb(node);
	for(int i = 0 ; i < adj[node].size() ; i ++){
		if(vis[adj[node][i]] == past)continue;
		if(vis[adj[node][i]] == ancestor){
			bool positive = false;
			for(int j = nodos.size()-1 ; j >= 0 ; j --){
				if(charging[nodos[j]]){
					positive = true;
					break;
				}
				if(nodos[j] == adj[node][i])break;
			}
			for(int j = nodos.size() - 1 ; j >= 0 ; j --){
				if(positive)isCycle[nodos[j]][1] = true;
				else isCycle[nodos[j]][0] = true;
				if(nodos[j] == adj[node][i])break;
			}
		}else{
			recorrer(adj[node][i]);
		}
	}
	vis[node] = past;
	nodos.pop_back();
}
int ansFor[5001];
bool dfs2(int node){
	bool isA = owner[0];
	if(isA and isCycle[node][1])return true;
	if(!isA and isCycle[node][0])return false;
	if(ansFor[node] != -1)return ansFor[node];
	vis[node] = past;
	for(int i = 0 ; i < adj[node].size() ; i ++){
		if(vis[adj[node][i] ] == past)continue;
		if(dfs2(adj[node][i]) == isA){
			vis[node] = unvisited;
			ansFor[node] = (isA ? 1 : 0);
			return ansFor[node];
		}
	}
	vis[node] = unvisited;
	ansFor[node] = (!isA ? 1 : 0);
	return ansFor[node];
}

std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
	n = a.size();
	vector<int> res;
	memss();
	bool all1 = true, all0 = true;
	for(int i = 0 ; i < n ; i ++){
		owner.pb(a[i]);
		res.pb(0);
		charging.pb(r[i]);
		if(a[i] == 0)all1 = false;
		else all0 = false;
	}
	for(int i = 0 ; i < u.size() ; i ++){
		adj[u[i]].pb(v[i]);
	}
	if(all0 or all1){
		memset(ansFor, -1, sizeof ansFor);
		for(int i = 0 ; i < n ; i ++){
			if(vis[i] == unvisited)recorrer(i);
		}
		memss();
		for(int i = 0 ; i < n ; i ++){
			if(dfs2(i)){
				res[i] = 1;
			}else res[i] = 0;
			
		}
	}else{
		for(int i = 0 ; i < n ; i ++){
			if(dfs(i)){
				res[i] = 1;
			}else res[i] = 0;
			
		}
	}
	return res;
}

Compilation message

train.cpp: In function 'bool dfs(int)':
train.cpp:29:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |  for(int i = 0 ; i < adj[node].size() ; i ++){
      |                  ~~^~~~~~~~~~~~~~~~~~
train.cpp:25:7: warning: unused variable 'isCharging' [-Wunused-variable]
   25 |  bool isCharging = charging[node];
      |       ^~~~~~~~~~
train.cpp: In function 'void recorrer(int)':
train.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |  for(int i = 0 ; i < adj[node].size() ; i ++){
      |                  ~~^~~~~~~~~~~~~~~~~~
train.cpp: In function 'bool dfs2(int)':
train.cpp:88:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |  for(int i = 0 ; i < adj[node].size() ; i ++){
      |                  ~~^~~~~~~~~~~~~~~~~~
train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:113:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |  for(int i = 0 ; i < u.size() ; i ++){
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 724 KB Output is correct
2 Correct 5 ms 724 KB Output is correct
3 Correct 4 ms 724 KB Output is correct
4 Correct 6 ms 724 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 11 ms 724 KB Output is correct
7 Correct 3 ms 724 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 3 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Incorrect 2 ms 340 KB 3rd lines differ - on the 6th token, expected: '0', found: '1'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1236 KB Output is correct
2 Correct 5 ms 1236 KB Output is correct
3 Correct 5 ms 1236 KB Output is correct
4 Correct 9 ms 1236 KB Output is correct
5 Correct 8 ms 1388 KB Output is correct
6 Correct 9 ms 1236 KB Output is correct
7 Correct 9 ms 1300 KB Output is correct
8 Correct 6 ms 1200 KB Output is correct
9 Correct 5 ms 1228 KB Output is correct
10 Incorrect 5 ms 1204 KB 3rd lines differ - on the 220th token, expected: '1', found: '0'
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 1108 KB Output is correct
2 Correct 5 ms 976 KB Output is correct
3 Correct 6 ms 1204 KB Output is correct
4 Incorrect 8 ms 1236 KB 3rd lines differ - on the 664th token, expected: '0', found: '1'
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2072 ms 1112 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 724 KB Output is correct
2 Correct 5 ms 724 KB Output is correct
3 Correct 4 ms 724 KB Output is correct
4 Correct 6 ms 724 KB Output is correct
5 Correct 3 ms 724 KB Output is correct
6 Correct 11 ms 724 KB Output is correct
7 Correct 3 ms 724 KB Output is correct
8 Correct 3 ms 724 KB Output is correct
9 Correct 3 ms 724 KB Output is correct
10 Correct 3 ms 724 KB Output is correct
11 Correct 3 ms 724 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Incorrect 2 ms 340 KB 3rd lines differ - on the 6th token, expected: '0', found: '1'
15 Halted 0 ms 0 KB -