답안 #406953

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
406953 2021-05-18T09:13:23 Z Kevin_Zhang_TW 장난감 기차 (IOI17_train) C++17
0 / 100
1311 ms 262148 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 300010;

#include "train.h"

int dp[MAX_N], w[MAX_N], sum[MAX_N], inst[MAX_N], n;
int belong[MAX_N];
vector<int> edge[MAX_N];

bool dfs(int x, int s, int v = 0) {
	static vector<int> stk, allstk;

	if (dp[x] != -1) return dp[x] == s;

	stk.pb(x);
	allstk.pb(x);
	inst[x] = true;
	v += w[x];
	sum[x] = v;

	bool ret = true, ch = false;

	// A owns it
	if (belong[x] == 1) {
		dp[x] = 0;
		for (int u : edge[x]) {
			if (inst[u]) {
				if (w[u] || sum[x] != sum[u]) {
					dp[x] = 1;
					break;
				}
				continue;
			}
			if (dfs(u, 1, v)) {
				dp[x] = 1;
				break;
			}
		}
		if (dp[x]) ch = true;
	}
	else {
		dp[x] = 1;
		for (int u : edge[x]) {
			if (inst[u]) {
				if (w[u] == 0 && sum[x] == sum[u]) {
					dp[x] = 0;
					break;
				}
				continue;
			}
			if (dfs(u, 0, v)) {
				dp[x] = 0;
				break;
			}
		}
		if (dp[x] == 0) ch = true;
	}

	ret = dp[x] != s;

//	if (ch) {
//		int u;
//		do {
//			u = allstk.back();
//			dp[u] = -1;
//			allstk.pop_back();
//		} while (u != x) ;
//	}
	
	dp[x] = -1;

	stk.pop_back();
	inst[x] = false;

	return ret;
}

std::vector<int> who_wins(std::vector<int> a, std::vector<int> r, std::vector<int> u, std::vector<int> v) {
	n = a.size();
	for (int i = 0;i < n;++i) {
		w[i] = r[i];
		belong[i] = a[i];
	}
	for (int i = 0;i < u.size();++i) {
		edge[ u[i] ].pb( v[i] );
	}


	vector<int> res(n);
	for (int i = 0;i < n;++i) {
		memset(dp, -1, sizeof(dp));
		res[i] = dfs(i, 1);
	}

	return res;
}

Compilation message

train.cpp: In function 'bool dfs(int, int, int)':
train.cpp:37:19: warning: variable 'ch' set but not used [-Wunused-but-set-variable]
   37 |  bool ret = true, ch = false;
      |                   ^~
train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:100:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |  for (int i = 0;i < u.size();++i) {
      |                 ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 213 ms 9336 KB 3rd lines differ - on the 2nd token, expected: '0', found: '1'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 8524 KB 3rd lines differ - on the 3rd token, expected: '1', found: '0'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1094 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1311 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1147 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 213 ms 9336 KB 3rd lines differ - on the 2nd token, expected: '0', found: '1'
2 Halted 0 ms 0 KB -