Submission #254797

# Submission time Handle Problem Language Result Execution time Memory
254797 2020-07-30T15:35:45 Z model_code Viruses (BOI20_viruses) C++17
100 / 100
171 ms 21624 KB
// O(K * L^3 * log)

#include <bits/stdc++.h>

using namespace std;

const int MK = 1000, ML = 51;

namespace AhoCorasick {
	struct node {
		int next[2];
		bool leaf;
		int p;
		char pch;
		int link;
		int go[2];
	};
	
	node t[ML + 1];
	int sz;
	
	void readString() {
		int len;
		ignore = scanf("%d", &len);
		
		int v = 0;
		for (int i = 0, c; i < len; i++) {
			ignore = scanf("%d", &c);
			
			if (t[v].next[c] == -1) {
				memset(t[sz].next, -1, sizeof t[sz].next);
				memset(t[sz].go, -1, sizeof t[sz].go);
				t[sz].link = -1;
				t[sz].p = v;
				t[sz].pch = c;
				t[v].next[c] = sz++;
			}
			
			v = t[v].next[c];
		}
		
		t[v].leaf = true;
	}
	
	int go(int v, int c);
	
	int get_link(int v) {
		if (t[v].link == -1) {
			if (v == 0 || t[v].p == 0) t[v].link = 0;
			else t[v].link = go(get_link(t[v].p), t[v].pch);
		
			get_link(t[v].link);
			if (t[t[v].link].leaf) t[v].leaf = true;
		}
		
		return t[v].link;
	}

	int go(int v, int c) {
		if (t[v].go[c] == -1) {
			if (t[v].next[c] != -1) t[v].go[c] = t[v].next[c];
			else t[v].go[c] = v == 0 ? 0 : go(get_link(v), c);
		}
		
		return t[v].go[c];
	}
	
	int to[ML][2];
	int create(int m) {
		t[0].p = t[0].link = -1;
		memset(t[0].next, -1, sizeof t[0].next);
		memset(t[0].go, -1, sizeof t[0].go);
		sz = 1;
		
		for (int i = 0; i < m; i++) readString();
		
		int cnt = 0;
		map<int, int> mp;
		for (int i = 0; i < sz; i++) {
			get_link(i);
			if (t[i].leaf) continue;
			
			mp[i] = cnt++;
		}
		
		for (auto p : mp) {
			int v = p.first;
			for (int c = 0; c < 2; c++) {
				int u = go(v, c);
				if (t[u].leaf) to[p.second][c] = -1;
				else to[p.second][c] = mp[u];
			}
		}
		
		return cnt;
	}
}

typedef unsigned long long ull;

struct Int {
	static const ull INF;
	
	ull x;
	
	Int(ull x = INF) : x(x) { }
	
	Int operator + (const Int& rhs) const {
		if (x == INF || rhs.x == INF) return INF;
		
		return min(x + rhs.x, INF);
	}
	
	bool operator < (const Int& rhs) const {
		if (x == INF) return false;
		if (rhs.x == INF) return true;
		return x < rhs.x;
	}
};

const ull Int::INF = 1ULL << 63;

int a[MK], b[3 * MK], firstB[MK], lastB[MK];
vector<int> mutations[MK];

void simplifyMutations(int& n, int g) {
	int it = lastB[n - 1];
	
	for (int i = 0; i < n; i++) {
		int k = lastB[i] - firstB[i];
		if (k <= 2) continue;
		
		a[n] = g;
		firstB[n] = firstB[i];
		lastB[n] = lastB[i] - 1;
		
		firstB[i] = it;
		b[it++] = g;
		b[it++] = b[lastB[i] - 1];
		lastB[i] = it;
		
		g++;
		n++;
	}
	
	for (int i = 0; i < n; i++) {
		for (int j = firstB[i]; j < lastB[i]; j++) {
			mutations[b[j]].push_back(i);
		}
	}
}

int L;
Int dp[MK][ML][ML];
set<tuple<ull, int, int, int>> S;

void update(int f, int s, int e, const Int& val) {
	if (val < dp[f][s][e]) {
		if (dp[f][s][e].x != Int::INF) {
			S.erase(make_tuple(dp[f][s][e].x, f, s, e));
		}
		
		dp[f][s][e] = val;
		
		S.emplace(val.x, f, s, e);
	}
}

void recalcMutationFast(int mut, int f, int s, int e) {
	int x = a[mut];
	int y = b[firstB[mut]];

	int k = lastB[mut] - firstB[mut];
	if (k == 1) {
		update(x, s, e, dp[y][s][e]);
		
		return;
	}
	
	assert(k == 2);
	
	int z = b[firstB[mut] + 1];
	
	if (y == f) {
		for (int t = 0; t < L; t++) {
			update(x, s, t, dp[y][s][e] + dp[z][e][t]);
		}
	}
	
	if (z == f) {
		for (int t = 0; t < L; t++) {
			update(x, t, e, dp[y][t][s] + dp[z][s][e]);
		}
	}
}

int main() {
	int g, n, m;
	ignore = scanf("%d %d %d", &g, &n, &m);
	
	for (int i = 0, it = 0, k; i < n; i++) {
		ignore = scanf("%d %d", a + i, &k);
		
		firstB[i] = it;
		for (int j = 0; j < k; j++) {
			ignore = scanf("%d", b + (it++));
		}
		
		lastB[i] = it;
	}
	
	simplifyMutations(n, g);
	
	L = AhoCorasick::create(m);
	
	for (int s = 0; s < L; s++) {
		for (int c = 0; c < 2; c++) {
			if (AhoCorasick::to[s][c] == -1) continue;
			
			update(c, s, AhoCorasick::to[s][c], 1);
		}
	}
	
	while (S.empty() == false) {
		int f, s, e;
		tie(ignore, f, s, e) = *S.begin();
		
		S.erase(S.begin());
		
		for (int i : mutations[f]) recalcMutationFast(i, f, s, e);
	}
	
	for (int i = 2; i < g; i++) {
		Int ans;
		for (int s = 0; s < L; s++)
			for (int e = 0; e < L; e++)
				ans = min(dp[i][s][e], ans);
		
		if (ans.x == Int::INF) printf("%s\n", "YES");
		else printf("%s %llu\n", "NO", ans.x);
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 20736 KB Output is correct
2 Correct 11 ms 20736 KB Output is correct
3 Correct 10 ms 20736 KB Output is correct
4 Correct 10 ms 20736 KB Output is correct
5 Correct 11 ms 20736 KB Output is correct
6 Correct 11 ms 20736 KB Output is correct
7 Correct 12 ms 20736 KB Output is correct
8 Correct 12 ms 20736 KB Output is correct
9 Correct 11 ms 20736 KB Output is correct
10 Correct 16 ms 20776 KB Output is correct
11 Correct 11 ms 20736 KB Output is correct
12 Correct 10 ms 20736 KB Output is correct
13 Correct 15 ms 20736 KB Output is correct
14 Correct 15 ms 20736 KB Output is correct
15 Correct 11 ms 20736 KB Output is correct
16 Correct 14 ms 20736 KB Output is correct
17 Correct 10 ms 20736 KB Output is correct
18 Correct 12 ms 20736 KB Output is correct
19 Correct 12 ms 20736 KB Output is correct
20 Correct 11 ms 20736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 20736 KB Output is correct
2 Correct 11 ms 20736 KB Output is correct
3 Correct 11 ms 20736 KB Output is correct
4 Correct 11 ms 20736 KB Output is correct
5 Correct 12 ms 20736 KB Output is correct
6 Correct 12 ms 20736 KB Output is correct
7 Correct 13 ms 20736 KB Output is correct
8 Correct 13 ms 20736 KB Output is correct
9 Correct 12 ms 20736 KB Output is correct
10 Correct 16 ms 20736 KB Output is correct
11 Correct 10 ms 20736 KB Output is correct
12 Correct 12 ms 20736 KB Output is correct
13 Correct 11 ms 20736 KB Output is correct
14 Correct 11 ms 20736 KB Output is correct
15 Correct 16 ms 20736 KB Output is correct
16 Correct 10 ms 20736 KB Output is correct
17 Correct 11 ms 20736 KB Output is correct
18 Correct 11 ms 20736 KB Output is correct
19 Correct 11 ms 20736 KB Output is correct
20 Correct 11 ms 20736 KB Output is correct
21 Correct 12 ms 20736 KB Output is correct
22 Correct 10 ms 20736 KB Output is correct
23 Correct 11 ms 20736 KB Output is correct
24 Correct 10 ms 20736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 20736 KB Output is correct
2 Correct 11 ms 20736 KB Output is correct
3 Correct 12 ms 20736 KB Output is correct
4 Correct 12 ms 20736 KB Output is correct
5 Correct 13 ms 20736 KB Output is correct
6 Correct 13 ms 20736 KB Output is correct
7 Correct 12 ms 20736 KB Output is correct
8 Correct 12 ms 20736 KB Output is correct
9 Correct 10 ms 20736 KB Output is correct
10 Correct 12 ms 20736 KB Output is correct
11 Correct 10 ms 20736 KB Output is correct
12 Correct 11 ms 20736 KB Output is correct
13 Correct 12 ms 20736 KB Output is correct
14 Correct 11 ms 20736 KB Output is correct
15 Correct 11 ms 20736 KB Output is correct
16 Correct 11 ms 20736 KB Output is correct
17 Correct 11 ms 20736 KB Output is correct
18 Correct 11 ms 20736 KB Output is correct
19 Correct 11 ms 20736 KB Output is correct
20 Correct 11 ms 20736 KB Output is correct
21 Correct 10 ms 20736 KB Output is correct
22 Correct 12 ms 20736 KB Output is correct
23 Correct 10 ms 20736 KB Output is correct
24 Correct 11 ms 20736 KB Output is correct
25 Correct 15 ms 20864 KB Output is correct
26 Correct 11 ms 20736 KB Output is correct
27 Correct 171 ms 20992 KB Output is correct
28 Correct 16 ms 20736 KB Output is correct
29 Correct 109 ms 20736 KB Output is correct
30 Correct 110 ms 20736 KB Output is correct
31 Correct 19 ms 20736 KB Output is correct
32 Correct 16 ms 20736 KB Output is correct
33 Correct 14 ms 20736 KB Output is correct
34 Correct 106 ms 20736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 20736 KB Output is correct
2 Correct 11 ms 20736 KB Output is correct
3 Correct 10 ms 20736 KB Output is correct
4 Correct 10 ms 20736 KB Output is correct
5 Correct 11 ms 20736 KB Output is correct
6 Correct 11 ms 20736 KB Output is correct
7 Correct 12 ms 20736 KB Output is correct
8 Correct 12 ms 20736 KB Output is correct
9 Correct 11 ms 20736 KB Output is correct
10 Correct 16 ms 20776 KB Output is correct
11 Correct 11 ms 20736 KB Output is correct
12 Correct 10 ms 20736 KB Output is correct
13 Correct 15 ms 20736 KB Output is correct
14 Correct 15 ms 20736 KB Output is correct
15 Correct 11 ms 20736 KB Output is correct
16 Correct 14 ms 20736 KB Output is correct
17 Correct 10 ms 20736 KB Output is correct
18 Correct 12 ms 20736 KB Output is correct
19 Correct 12 ms 20736 KB Output is correct
20 Correct 11 ms 20736 KB Output is correct
21 Correct 16 ms 20736 KB Output is correct
22 Correct 12 ms 20736 KB Output is correct
23 Correct 10 ms 20736 KB Output is correct
24 Correct 12 ms 20736 KB Output is correct
25 Correct 10 ms 20736 KB Output is correct
26 Correct 11 ms 20736 KB Output is correct
27 Correct 12 ms 20736 KB Output is correct
28 Correct 11 ms 20736 KB Output is correct
29 Correct 11 ms 20736 KB Output is correct
30 Correct 11 ms 20736 KB Output is correct
31 Correct 11 ms 20736 KB Output is correct
32 Correct 11 ms 20736 KB Output is correct
33 Correct 11 ms 20736 KB Output is correct
34 Correct 11 ms 20736 KB Output is correct
35 Correct 10 ms 20736 KB Output is correct
36 Correct 12 ms 20736 KB Output is correct
37 Correct 10 ms 20736 KB Output is correct
38 Correct 11 ms 20736 KB Output is correct
39 Correct 11 ms 20736 KB Output is correct
40 Correct 10 ms 20736 KB Output is correct
41 Correct 12 ms 20736 KB Output is correct
42 Correct 10 ms 20736 KB Output is correct
43 Correct 11 ms 20736 KB Output is correct
44 Correct 11 ms 20736 KB Output is correct
45 Correct 10 ms 20736 KB Output is correct
46 Correct 12 ms 20736 KB Output is correct
47 Correct 11 ms 20736 KB Output is correct
48 Correct 11 ms 20736 KB Output is correct
49 Correct 12 ms 20736 KB Output is correct
50 Correct 11 ms 20736 KB Output is correct
51 Correct 10 ms 20736 KB Output is correct
52 Correct 11 ms 20736 KB Output is correct
53 Correct 10 ms 20736 KB Output is correct
54 Correct 11 ms 20736 KB Output is correct
55 Correct 10 ms 20736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 20736 KB Output is correct
2 Correct 11 ms 20736 KB Output is correct
3 Correct 10 ms 20736 KB Output is correct
4 Correct 10 ms 20736 KB Output is correct
5 Correct 11 ms 20736 KB Output is correct
6 Correct 11 ms 20736 KB Output is correct
7 Correct 12 ms 20736 KB Output is correct
8 Correct 12 ms 20736 KB Output is correct
9 Correct 11 ms 20736 KB Output is correct
10 Correct 16 ms 20776 KB Output is correct
11 Correct 11 ms 20736 KB Output is correct
12 Correct 10 ms 20736 KB Output is correct
13 Correct 15 ms 20736 KB Output is correct
14 Correct 15 ms 20736 KB Output is correct
15 Correct 11 ms 20736 KB Output is correct
16 Correct 14 ms 20736 KB Output is correct
17 Correct 10 ms 20736 KB Output is correct
18 Correct 12 ms 20736 KB Output is correct
19 Correct 12 ms 20736 KB Output is correct
20 Correct 11 ms 20736 KB Output is correct
21 Correct 11 ms 20736 KB Output is correct
22 Correct 11 ms 20736 KB Output is correct
23 Correct 12 ms 20736 KB Output is correct
24 Correct 12 ms 20736 KB Output is correct
25 Correct 13 ms 20736 KB Output is correct
26 Correct 13 ms 20736 KB Output is correct
27 Correct 12 ms 20736 KB Output is correct
28 Correct 16 ms 20736 KB Output is correct
29 Correct 10 ms 20736 KB Output is correct
30 Correct 12 ms 20736 KB Output is correct
31 Correct 11 ms 20736 KB Output is correct
32 Correct 11 ms 20736 KB Output is correct
33 Correct 16 ms 20736 KB Output is correct
34 Correct 10 ms 20736 KB Output is correct
35 Correct 11 ms 20736 KB Output is correct
36 Correct 11 ms 20736 KB Output is correct
37 Correct 11 ms 20736 KB Output is correct
38 Correct 11 ms 20736 KB Output is correct
39 Correct 12 ms 20736 KB Output is correct
40 Correct 10 ms 20736 KB Output is correct
41 Correct 11 ms 20736 KB Output is correct
42 Correct 10 ms 20736 KB Output is correct
43 Correct 12 ms 20736 KB Output is correct
44 Correct 10 ms 20736 KB Output is correct
45 Correct 12 ms 20736 KB Output is correct
46 Correct 10 ms 20736 KB Output is correct
47 Correct 11 ms 20736 KB Output is correct
48 Correct 12 ms 20736 KB Output is correct
49 Correct 11 ms 20736 KB Output is correct
50 Correct 11 ms 20736 KB Output is correct
51 Correct 11 ms 20736 KB Output is correct
52 Correct 11 ms 20736 KB Output is correct
53 Correct 11 ms 20736 KB Output is correct
54 Correct 11 ms 20736 KB Output is correct
55 Correct 11 ms 20736 KB Output is correct
56 Correct 10 ms 20736 KB Output is correct
57 Correct 12 ms 20736 KB Output is correct
58 Correct 10 ms 20736 KB Output is correct
59 Correct 11 ms 20736 KB Output is correct
60 Correct 15 ms 20864 KB Output is correct
61 Correct 11 ms 20736 KB Output is correct
62 Correct 171 ms 20992 KB Output is correct
63 Correct 16 ms 20736 KB Output is correct
64 Correct 109 ms 20736 KB Output is correct
65 Correct 110 ms 20736 KB Output is correct
66 Correct 19 ms 20736 KB Output is correct
67 Correct 16 ms 20736 KB Output is correct
68 Correct 14 ms 20736 KB Output is correct
69 Correct 106 ms 20736 KB Output is correct
70 Correct 11 ms 20736 KB Output is correct
71 Correct 10 ms 20736 KB Output is correct
72 Correct 12 ms 20736 KB Output is correct
73 Correct 10 ms 20736 KB Output is correct
74 Correct 11 ms 20736 KB Output is correct
75 Correct 11 ms 20736 KB Output is correct
76 Correct 10 ms 20736 KB Output is correct
77 Correct 12 ms 20736 KB Output is correct
78 Correct 11 ms 20736 KB Output is correct
79 Correct 11 ms 20736 KB Output is correct
80 Correct 12 ms 20736 KB Output is correct
81 Correct 11 ms 20736 KB Output is correct
82 Correct 10 ms 20736 KB Output is correct
83 Correct 11 ms 20736 KB Output is correct
84 Correct 10 ms 20736 KB Output is correct
85 Correct 11 ms 20736 KB Output is correct
86 Correct 10 ms 20736 KB Output is correct
87 Correct 23 ms 20736 KB Output is correct
88 Correct 14 ms 20736 KB Output is correct
89 Correct 10 ms 20736 KB Output is correct
90 Correct 10 ms 20736 KB Output is correct
91 Correct 10 ms 20736 KB Output is correct
92 Correct 55 ms 21624 KB Output is correct
93 Correct 50 ms 21120 KB Output is correct
94 Correct 12 ms 20736 KB Output is correct
95 Correct 15 ms 20736 KB Output is correct
96 Correct 12 ms 20736 KB Output is correct
97 Correct 19 ms 20864 KB Output is correct
98 Correct 12 ms 20736 KB Output is correct
99 Correct 26 ms 20864 KB Output is correct
100 Correct 17 ms 20736 KB Output is correct
101 Correct 20 ms 20736 KB Output is correct
102 Correct 134 ms 20984 KB Output is correct
103 Correct 13 ms 20736 KB Output is correct
104 Correct 28 ms 20736 KB Output is correct
105 Correct 11 ms 20736 KB Output is correct
106 Correct 47 ms 20864 KB Output is correct
107 Correct 48 ms 20736 KB Output is correct