답안 #371298

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
371298 2021-02-26T12:26:05 Z KoD 장난감 기차 (IOI17_train) C++17
5 / 100
11 ms 3308 KB
#include <bits/stdc++.h>
#include "train.h"

template <class T>
using Vec = std::vector<T>;

Vec<int> who_wins(Vec<int> a, Vec<int> r, Vec<int> u, Vec<int> v) {
	const int n = (int) a.size();
	const int m = (int) u.size();
	{
		bool sub1 = true;
		Vec<bool> loop(n);
		Vec<bool> right(n);
		for (int i = 0; i < m; ++i) {
			if (u[i] == v[i]) {
				loop[u[i]] = true;
			}
			else if (u[i] + 1 == v[i]) {
				right[u[i]] = true;
			}
			else {
				sub1 = false;
				break;
			}
		}
		if (sub1) {
			Vec<int> ret(n);
			for (int i = n - 1; i >= 0; --i) {
				if (a[i]) {
					ret[i] = 0;
					if (r[i] && loop[i]) {
						ret[i] = 1;
					}
					if (right[i] && ret[i + 1]) {
						ret[i] = 1;
					}
				}
				else {
					ret[i] = 1;
					if (!r[i] && loop[i]) {
						ret[i] = 0;
					}
					if (right[i] && !ret[i + 1]) {
						ret[i] = 0;
					}
				}
			}
			return ret;
		}
	}
	Vec<Vec<int>> graph(n);
	Vec<Vec<int>> revgraph(n);
	for (int i = 0; i < m; ++i) {
		graph[u[i]].push_back(v[i]);
		revgraph[v[i]].push_back(u[i]);
	}
	Vec<int> order;
	Vec<char> done(n);
	auto dfs1 = [&](auto dfs1, const int u) -> void {
		if (done[u]) {
			return;
		}
		done[u] = true;
		for (const auto v: graph[u]) {
			dfs1(dfs1, v);
		}
		order.push_back(u);
	};
	for (int i = 0; i < n; ++i) {
		dfs1(dfs1, i);
	}
	std::fill(done.begin(), done.end(), true);
	Vec<Vec<int>> group;
	auto dfs2 = [&](auto dfs2, const int u) -> void {
		if (done[u]) {
			return;
		}
		done[u] = true;
		group.back().push_back(u);
		for (const int v: revgraph[u]) {
			dfs2(dfs2, v);
		}
	};
	while (!order.empty()) {
		const auto u = order.back();
		order.pop_back();
		if (!done[u]) {
			group.push_back({ });
			dfs2(dfs2, u);
		}
	}
	const int g = (int) group.size();
	Vec<int> belong(n);
	for (int i = 0; i < g; ++i) {
		for (const auto u: group[i]) {
			belong[u] = i;
		}
	}
	Vec<Vec<int>> newgraph(g);
	for (int i = 0; i < m; ++i) {
		const auto x = belong[u[i]];
		const auto y = belong[v[i]];
		if (x != y) {
			newgraph[x].push_back(y);
		}
	}
	Vec<int> ok(g);
	for (int i = g - 1; i >= 0; --i) {
		for (const auto x: group[i]) {
			if (r[x]) {
				ok[i] = true;
			}
		}
		for (const auto y: newgraph[i]) {
			if (ok[y]) {
				ok[i] = true;
			}
		}
	}
	Vec<int> ret(n);
	for (int i = 0; i < n; ++i) {
		ret[i] = ok[belong[i]];
	}
	return ret;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 492 KB Output is correct
2 Correct 4 ms 492 KB Output is correct
3 Correct 4 ms 492 KB Output is correct
4 Correct 4 ms 492 KB Output is correct
5 Correct 4 ms 492 KB Output is correct
6 Correct 4 ms 492 KB Output is correct
7 Correct 4 ms 492 KB Output is correct
8 Correct 4 ms 492 KB Output is correct
9 Correct 4 ms 492 KB Output is correct
10 Correct 4 ms 492 KB Output is correct
11 Correct 3 ms 492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 492 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 10 ms 3308 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 9 ms 2668 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 11 ms 3052 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 492 KB Output is correct
2 Correct 4 ms 492 KB Output is correct
3 Correct 4 ms 492 KB Output is correct
4 Correct 4 ms 492 KB Output is correct
5 Correct 4 ms 492 KB Output is correct
6 Correct 4 ms 492 KB Output is correct
7 Correct 4 ms 492 KB Output is correct
8 Correct 4 ms 492 KB Output is correct
9 Correct 4 ms 492 KB Output is correct
10 Correct 4 ms 492 KB Output is correct
11 Correct 3 ms 492 KB Output is correct
12 Runtime error 1 ms 492 KB Execution killed with signal 11
13 Halted 0 ms 0 KB -