Submission #689995

# Submission time Handle Problem Language Result Execution time Memory
689995 2023-01-30T01:17:18 Z NK_ Love Polygon (BOI18_polygon) C++17
100 / 100
253 ms 21756 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'

using str = string;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	map<str, int> IDX;

	int cur = 0;
	auto idx = [&](const str& S) {
		if (IDX.find(S) != IDX.end()) return IDX[S];
		return IDX[S] = cur++;
	};

	int N; cin >> N;

	vector<int> P(N, -1), in(N);
	vector<vector<int>> chd(N);
	for(int i = 0; i < N; i++) {
		str S, T; cin >> S >> T;
		int s = idx(S), t = idx(T);

		P[s] = t; in[t]++;
		chd[t].push_back(s);
	}

	int left = 0, pairs = 0;
	queue<int> q; for(int i = 0; i < N; i++) if (in[i] == 0) q.push(i);

	set<int> cyc; for(int i = 0; i < N; i++) cyc.insert(i);

	while(int(size(q)) > 0) {
		int u = q.front(); q.pop();

		cyc.erase(u); 

		int v = P[u];
		if (v == -1) { left++; continue; }

		// check if already in relationship
		int x = P[v];
		if (x != -1 && x != v && P[x] == v) { left++; continue; }

		cyc.erase(v);

		for(auto x : chd[v]) {
			P[x] = -1;
		}

		if (P[v] != -1) {
			in[P[v]]--; if (in[P[v]] == 0) q.push(P[v]);
		}

		pairs++;
	}


	vector<int> vis(N);
	for(auto i : cyc) if (!vis[i]) {
		int u = i, len = 0;
		while(!vis[u]) {
			vis[u] = 1;
			u = P[u]; len++;
		} 
		if (len == 2) continue;
		left += len & 1; pairs += len / 2;
	}

	if (left % 2) cout << -1 << nl;
	else cout << left + pairs << nl;
    return 0;
}

/*
3
rocky scarlet 
scarlet patrick 
patrick rocky

8
leonard emmy 
ada emmy
isaac leonard 
emmy pierre 
pierre bernhard 
bernhard emmy 
sofia karl
karl sofia

4
a c
b c
c d
d d

*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 195 ms 19420 KB Output is correct
5 Correct 215 ms 19444 KB Output is correct
6 Correct 183 ms 19388 KB Output is correct
7 Correct 192 ms 19444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 18768 KB Output is correct
2 Correct 225 ms 18700 KB Output is correct
3 Correct 157 ms 19436 KB Output is correct
4 Correct 233 ms 17704 KB Output is correct
5 Correct 240 ms 19140 KB Output is correct
6 Correct 212 ms 17856 KB Output is correct
7 Correct 197 ms 18080 KB Output is correct
8 Correct 207 ms 18516 KB Output is correct
9 Correct 203 ms 17516 KB Output is correct
10 Correct 125 ms 16708 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 195 ms 19420 KB Output is correct
20 Correct 215 ms 19444 KB Output is correct
21 Correct 183 ms 19388 KB Output is correct
22 Correct 192 ms 19444 KB Output is correct
23 Correct 237 ms 18768 KB Output is correct
24 Correct 225 ms 18700 KB Output is correct
25 Correct 157 ms 19436 KB Output is correct
26 Correct 233 ms 17704 KB Output is correct
27 Correct 240 ms 19140 KB Output is correct
28 Correct 212 ms 17856 KB Output is correct
29 Correct 197 ms 18080 KB Output is correct
30 Correct 207 ms 18516 KB Output is correct
31 Correct 203 ms 17516 KB Output is correct
32 Correct 125 ms 16708 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 229 ms 20884 KB Output is correct
38 Correct 206 ms 20008 KB Output is correct
39 Correct 253 ms 20456 KB Output is correct
40 Correct 210 ms 19920 KB Output is correct
41 Correct 207 ms 19896 KB Output is correct
42 Correct 231 ms 20280 KB Output is correct
43 Correct 214 ms 20304 KB Output is correct
44 Correct 220 ms 20272 KB Output is correct
45 Correct 232 ms 20152 KB Output is correct
46 Correct 217 ms 20252 KB Output is correct
47 Correct 196 ms 19528 KB Output is correct
48 Correct 248 ms 21004 KB Output is correct
49 Correct 217 ms 20784 KB Output is correct
50 Correct 164 ms 21536 KB Output is correct
51 Correct 217 ms 20048 KB Output is correct
52 Correct 223 ms 21212 KB Output is correct
53 Correct 208 ms 19864 KB Output is correct
54 Correct 203 ms 20404 KB Output is correct
55 Correct 190 ms 20644 KB Output is correct
56 Correct 185 ms 19548 KB Output is correct
57 Correct 149 ms 18944 KB Output is correct
58 Correct 0 ms 212 KB Output is correct
59 Correct 0 ms 324 KB Output is correct
60 Correct 0 ms 212 KB Output is correct
61 Correct 0 ms 212 KB Output is correct
62 Correct 0 ms 212 KB Output is correct
63 Correct 0 ms 212 KB Output is correct
64 Correct 1 ms 212 KB Output is correct
65 Correct 188 ms 21604 KB Output is correct
66 Correct 206 ms 21652 KB Output is correct
67 Correct 192 ms 21636 KB Output is correct
68 Correct 195 ms 21756 KB Output is correct
69 Correct 1 ms 216 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 1 ms 212 KB Output is correct
74 Correct 1 ms 212 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 0 ms 332 KB Output is correct
77 Correct 0 ms 324 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 0 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 0 ms 212 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 324 KB Output is correct