Submission #1034719

# Submission time Handle Problem Language Result Execution time Memory
1034719 2024-07-25T17:09:37 Z ALeonidou Toy Train (IOI17_train) C++17
0 / 100
1965 ms 2880 KB
#include "train.h"
#include <bits/stdc++.h>

using namespace std;

#define ll int
#define sz(x) (ll)x.size()
#define endl "\n"
#define pb push_back
#define F first
#define S second

typedef vector <ll> vi;
typedef pair <ll,ll> ii;
typedef vector <ii> vii;

#define dbg(x) cout<<#x<<": "<<x<<endl;
#define dbg2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl;
#define dbg3(x,y,z) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<" "<<#z<<": "<<z<<endl;

void printVct(vi &v){
	for (ll i =0; i<sz(v); i++){
		cout<<v[i]<<" ";
	}
	cout<<endl;
}

void printVct2D(vector <vi> &adj){
	cout<<endl;
	for (ll i =0; i<sz(adj); i++){
		cout<<i<<": ";
		for (ll j =0; j<sz(adj[i]); j++){
			cout<<adj[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
}

vector <vi> adj, radj, cadj, fadj;
vi cadj_type;	//0: neutral, 1:charged, 2: not charged
vi vis;
vi r,a,u,v;
stack <ll> st;
void dfs(ll u){
	vis[u] = 1;
	for (ll i= 0; i<sz(adj[u]); i++){
		if (!vis[adj[u][i]]){
			dfs(adj[u][i]);
		}
	}
	st.push(u);
}

void dfs2(ll u){
	vis[u] = sz(cadj);
	cadj.back().pb(u);
	if (r[u]) cadj_type.back() = 1;
	for (ll i =0; i<sz(radj[u]); i++){
		ll c = radj[u][i];
		if (!vis[c]){
			dfs2(c);
		}
		else if (vis[c] < sz(cadj)){
			fadj[vis[c]-1].pb(sz(cadj)-1);
		}
	}
}

bool can_reach_neutral_dfs(ll u, ll p = -1){
	if (cadj_type[u] == 2) return true;
	for (ll i =0; i<sz(fadj[u]); i++){
		ll c = fadj[u][i];
		if (c != u){
			if (can_reach_neutral_dfs(c)){
				return true;
			}
		}
	}
	return false;
}

vi who_wins(vi A, vi R, vi U, vi V) {
	r = R, a = A, u = U, v = V;;
	ll n = sz(a), m = sz(u);
	
	adj.assign(n,vi());
	radj.assign(n, vi());
	vi self_cycles(n, 0);
	for(ll i =0; i<m; i++){
		if (v[i] == u[i]) self_cycles[v[i]] = 1;
		adj[u[i]].pb(v[i]);
		radj[v[i]].pb(u[i]);
	}

	vis.assign(n, 0);
	for (ll i =0; i<n; i++){
		if (!vis[i]){
			dfs(i);
			// dbg(i);
		}
	}

	vis.assign(n, 0);
	while (!st.empty()){
		ll f = st.top();
		st.pop();
		if (vis[f]) continue;
		cadj.pb(vi());
		fadj.pb(vi());
		cadj_type.pb(0);
		dfs2(f);
		if (cadj_type.back() == 0){
			if (sz(cadj.back()) > 1 || self_cycles[cadj.back().back()]){
				cadj_type.back() = 2;
			}
		}
		else if (sz(cadj.back()) == 1 && !self_cycles[cadj.back().back()]){
			cadj_type.back() = 0;
		}
	}

	// printVct(self_cycles);
	// printVct2D(cadj);
	// printVct(cadj_type);
	// printVct2D(fadj);
	
	vi ans(n, 1);
	for (ll i =0; i<sz(cadj); i++){
		if (can_reach_neutral_dfs(i, -1)){
			for (ll j = 0; j<sz(cadj[i]); j++){
				ans[cadj[i][j]] = 0;
			}
		}
	}
	
	return ans;
}


/*
7 9
1 1 1 1 1 1 1
0 1 0 0 0 1 0
0 2
0 1
2 3
1 4
4 5
5 3
3 4
3 6
6 6
*/
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2140 KB 3rd lines differ - on the 14th token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 344 KB 3rd lines differ - on the 2nd token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 39 ms 2652 KB Output is correct
2 Correct 20 ms 2880 KB Output is correct
3 Correct 6 ms 2396 KB Output is correct
4 Incorrect 6 ms 2272 KB 3rd lines differ - on the 1st token, expected: '1', found: '0'
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 1880 KB Output is correct
2 Correct 42 ms 2396 KB Output is correct
3 Incorrect 1965 ms 2368 KB 3rd lines differ - on the 25th token, expected: '0', found: '1'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 2136 KB 3rd lines differ - on the 2nd token, expected: '0', found: '1'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 2140 KB 3rd lines differ - on the 14th token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -