Submission #409538

# Submission time Handle Problem Language Result Execution time Memory
409538 2021-05-21T04:12:28 Z Kevin_Zhang_TW Toy Train (IOI17_train) C++17
100 / 100
1052 ms 1856 KB
#include <bits/stdc++.h>
#include "train.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...); }
void debug(auto l, auto r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif

const int MAX_N = 5005;

int n;
vector<int> rev_edge[MAX_N];
vector<pair<int,int>> alle;
bool rm[MAX_N], is_a[MAX_N], vis[MAX_N];

int out_deg[MAX_N];

void dfs1(int x) {
	if (rm[x] || vis[x]) return;
	vis[x] = true;
	for (int u : rev_edge[x]) {
		if (is_a[u] || --out_deg[u] == 0)
			dfs1(u);
	}
}

void dfs2(int x) {
	if (rm[x] || vis[x]) return;
	vis[x] = true;
	for (int u : rev_edge[x]) {
		if (!is_a[u] || --out_deg[u] == 0)
			dfs2(u);
	}
}

void init_g() {
	fill(out_deg, out_deg + n, 0);
	fill(vis, vis + n, false);
	for (auto [a, b] : alle) {
		if (rm[a] || rm[b]) continue;
		++out_deg[a];
	}
}

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 < u.size();++i) {
		alle.pb(u[i], v[i]);
		rev_edge[v[i]].pb(u[i]);
	}
	vector<int> res(n);

	for (int i = 0;i < n;++i)
		is_a[i] = a[i];

	int node_cnt = n;
	while (node_cnt) {
		init_g();
		for (int i = 0;i < n;++i) if (!rm[i]) {
			if (r[i] == 1) dfs1(i);
		}
		if (count(vis, vis + n, true) == node_cnt) {
			for (int i = 0;i < n;++i)
				if (!rm[i]) res[i] = 1;
			break;
		}
		vector<int> bwin;
		for (int i = 0;i < n;++i) if (!rm[i]) {
			if (!vis[i]) bwin.pb(i);
		}
		init_g();
		for (int i : bwin) dfs2(i);
		for (int i = 0;i < n;++i) if (!rm[i]) {
			if (vis[i] == true) {
				rm[i] = true;
				--node_cnt;
			}
		}
	}

	return res;
}

Compilation message

train.cpp: In function 'std::vector<int> who_wins(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
train.cpp:57:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |  for (int i = 0;i < u.size();++i) {
      |                 ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 844 KB Output is correct
2 Correct 6 ms 844 KB Output is correct
3 Correct 6 ms 944 KB Output is correct
4 Correct 6 ms 844 KB Output is correct
5 Correct 6 ms 844 KB Output is correct
6 Correct 7 ms 904 KB Output is correct
7 Correct 5 ms 844 KB Output is correct
8 Correct 6 ms 844 KB Output is correct
9 Correct 6 ms 832 KB Output is correct
10 Correct 5 ms 844 KB Output is correct
11 Correct 4 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 416 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 1536 KB Output is correct
2 Correct 273 ms 1708 KB Output is correct
3 Correct 396 ms 1736 KB Output is correct
4 Correct 9 ms 1608 KB Output is correct
5 Correct 10 ms 1480 KB Output is correct
6 Correct 10 ms 1480 KB Output is correct
7 Correct 8 ms 1356 KB Output is correct
8 Correct 9 ms 1452 KB Output is correct
9 Correct 8 ms 1376 KB Output is correct
10 Correct 8 ms 1428 KB Output is correct
11 Correct 8 ms 1356 KB Output is correct
12 Correct 8 ms 1404 KB Output is correct
13 Correct 11 ms 1608 KB Output is correct
14 Correct 9 ms 1584 KB Output is correct
15 Correct 9 ms 1608 KB Output is correct
16 Correct 8 ms 1608 KB Output is correct
17 Correct 10 ms 1620 KB Output is correct
18 Correct 581 ms 1248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 972 KB Output is correct
2 Correct 8 ms 1356 KB Output is correct
3 Correct 8 ms 1480 KB Output is correct
4 Correct 9 ms 1448 KB Output is correct
5 Correct 9 ms 1480 KB Output is correct
6 Correct 8 ms 1448 KB Output is correct
7 Correct 9 ms 1356 KB Output is correct
8 Correct 8 ms 1356 KB Output is correct
9 Correct 8 ms 1416 KB Output is correct
10 Correct 8 ms 1356 KB Output is correct
11 Correct 8 ms 1356 KB Output is correct
12 Correct 8 ms 1356 KB Output is correct
13 Correct 9 ms 1480 KB Output is correct
14 Correct 8 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1208 KB Output is correct
2 Correct 9 ms 1480 KB Output is correct
3 Correct 9 ms 1452 KB Output is correct
4 Correct 9 ms 1416 KB Output is correct
5 Correct 2 ms 460 KB Output is correct
6 Correct 6 ms 844 KB Output is correct
7 Correct 13 ms 1228 KB Output is correct
8 Correct 11 ms 1196 KB Output is correct
9 Correct 6 ms 1200 KB Output is correct
10 Correct 2 ms 588 KB Output is correct
11 Correct 6 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 844 KB Output is correct
2 Correct 6 ms 844 KB Output is correct
3 Correct 6 ms 944 KB Output is correct
4 Correct 6 ms 844 KB Output is correct
5 Correct 6 ms 844 KB Output is correct
6 Correct 7 ms 904 KB Output is correct
7 Correct 5 ms 844 KB Output is correct
8 Correct 6 ms 844 KB Output is correct
9 Correct 6 ms 832 KB Output is correct
10 Correct 5 ms 844 KB Output is correct
11 Correct 4 ms 844 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 416 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 145 ms 1536 KB Output is correct
33 Correct 273 ms 1708 KB Output is correct
34 Correct 396 ms 1736 KB Output is correct
35 Correct 9 ms 1608 KB Output is correct
36 Correct 10 ms 1480 KB Output is correct
37 Correct 10 ms 1480 KB Output is correct
38 Correct 8 ms 1356 KB Output is correct
39 Correct 9 ms 1452 KB Output is correct
40 Correct 8 ms 1376 KB Output is correct
41 Correct 8 ms 1428 KB Output is correct
42 Correct 8 ms 1356 KB Output is correct
43 Correct 8 ms 1404 KB Output is correct
44 Correct 11 ms 1608 KB Output is correct
45 Correct 9 ms 1584 KB Output is correct
46 Correct 9 ms 1608 KB Output is correct
47 Correct 8 ms 1608 KB Output is correct
48 Correct 10 ms 1620 KB Output is correct
49 Correct 581 ms 1248 KB Output is correct
50 Correct 7 ms 972 KB Output is correct
51 Correct 8 ms 1356 KB Output is correct
52 Correct 8 ms 1480 KB Output is correct
53 Correct 9 ms 1448 KB Output is correct
54 Correct 9 ms 1480 KB Output is correct
55 Correct 8 ms 1448 KB Output is correct
56 Correct 9 ms 1356 KB Output is correct
57 Correct 8 ms 1356 KB Output is correct
58 Correct 8 ms 1416 KB Output is correct
59 Correct 8 ms 1356 KB Output is correct
60 Correct 8 ms 1356 KB Output is correct
61 Correct 8 ms 1356 KB Output is correct
62 Correct 9 ms 1480 KB Output is correct
63 Correct 8 ms 1356 KB Output is correct
64 Correct 9 ms 1208 KB Output is correct
65 Correct 9 ms 1480 KB Output is correct
66 Correct 9 ms 1452 KB Output is correct
67 Correct 9 ms 1416 KB Output is correct
68 Correct 2 ms 460 KB Output is correct
69 Correct 6 ms 844 KB Output is correct
70 Correct 13 ms 1228 KB Output is correct
71 Correct 11 ms 1196 KB Output is correct
72 Correct 6 ms 1200 KB Output is correct
73 Correct 2 ms 588 KB Output is correct
74 Correct 6 ms 1228 KB Output is correct
75 Correct 236 ms 1524 KB Output is correct
76 Correct 300 ms 1744 KB Output is correct
77 Correct 411 ms 1736 KB Output is correct
78 Correct 416 ms 1856 KB Output is correct
79 Correct 410 ms 1740 KB Output is correct
80 Correct 9 ms 1480 KB Output is correct
81 Correct 10 ms 1480 KB Output is correct
82 Correct 11 ms 1456 KB Output is correct
83 Correct 13 ms 1480 KB Output is correct
84 Correct 10 ms 1480 KB Output is correct
85 Correct 10 ms 1468 KB Output is correct
86 Correct 12 ms 1456 KB Output is correct
87 Correct 9 ms 1356 KB Output is correct
88 Correct 14 ms 1356 KB Output is correct
89 Correct 11 ms 1464 KB Output is correct
90 Correct 18 ms 1456 KB Output is correct
91 Correct 18 ms 1452 KB Output is correct
92 Correct 41 ms 1468 KB Output is correct
93 Correct 32 ms 1448 KB Output is correct
94 Correct 33 ms 1356 KB Output is correct
95 Correct 39 ms 1432 KB Output is correct
96 Correct 98 ms 1356 KB Output is correct
97 Correct 635 ms 1356 KB Output is correct
98 Correct 955 ms 1472 KB Output is correct
99 Correct 1052 ms 1384 KB Output is correct
100 Correct 578 ms 1216 KB Output is correct