Submission #475747

# Submission time Handle Problem Language Result Execution time Memory
475747 2021-09-24T00:09:18 Z KhaledFarhat Vlak (COCI20_vlak) C++17
70 / 70
19 ms 15548 KB
#include <bits/stdc++.h>
using namespace std;

struct TrieNode {
	TrieNode() {
		canWin[0] = canWin[1] = -1;
		hasColor[0] = hasColor[1] = false;
		memset(child, 0, sizeof child);
	}
	
	int canWin[2];
	bool hasColor[2];
	int child[26];
};

struct Trie {
	Trie() {
		fetchNode();
		root = fetchNode();
	}
	
	void insertWord(const string& s, int color) {
		int current = root;
		
		for (int i = 0; i < (int)s.size(); ++i) {
			int edge = s[i] - 'a';
						
			if (nodes[current].child[edge] == 0) {
				nodes[current].child[edge] = fetchNode();
			}
						
			current = nodes[current].child[edge];
			nodes[current].hasColor[color] = true;
		}
	}
	
	int canWin(int node, int turn) {
		int& ret = nodes[node].canWin[turn];
		
		if (ret != -1) {
			return ret;
		}
		
		for (int edge = 0; edge < 26; ++edge) {
			int child = nodes[node].child[edge];
			
			if (child != 0 && nodes[child].hasColor[turn] == true && canWin(child, turn ^ 1) == false) {
				return ret = 1;
			}
		}
		
		return ret = 0;
	}
	
	int fetchNode() {
		nodes.push_back(TrieNode());
		
		return (int)nodes.size() - 1;
	}
	
	int root;
	vector<TrieNode> nodes;
};

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	Trie trie;
	
	int n;
	cin >> n;
		
	while (n--) {
		string s;
		cin >> s;
		
		trie.insertWord(s, 0);
	}
	
	int m;
	cin >> m;
	
	while (m--) {
		string s;
		cin >> s;
		
		trie.insertWord(s, 1);
	}
	
	cout << (trie.canWin(1, 0) == 1 ? "Nina" : "Emilija");
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 588 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 652 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 588 KB Output is correct
3 Correct 1 ms 588 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15476 KB Output is correct
2 Correct 15 ms 15544 KB Output is correct
3 Correct 15 ms 15548 KB Output is correct
4 Correct 19 ms 15544 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 15420 KB Output is correct
2 Correct 16 ms 15548 KB Output is correct
3 Correct 14 ms 15548 KB Output is correct
4 Correct 17 ms 15548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 15484 KB Output is correct
2 Correct 18 ms 15456 KB Output is correct
3 Correct 15 ms 15520 KB Output is correct
4 Correct 18 ms 15480 KB Output is correct