Submission #373707

# Submission time Handle Problem Language Result Execution time Memory
373707 2021-03-05T13:32:55 Z luciocf Love Polygon (BOI18_polygon) C++14
75 / 100
343 ms 15348 KB
#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;

int f[maxn];

int nivel[maxn], cc;
int ini[maxn], fim[maxn];
int mark[maxn];

bool done[maxn], incycle[maxn];

void dfs(int u)
{
	mark[u] = 1;

	if (mark[f[u]] == 1)
	{
		++cc;
		mark[u] = 2;
		ini[cc] = f[u], fim[cc] = u;
		return;
	}

	if (!mark[f[u]]) dfs(f[u]);

	nivel[u] = nivel[f[u]]+1;
	mark[u] = 2;
}

bool comp(int a, int b)
{
	if (incycle[a] && !incycle[b]) return 0;
	if (!incycle[a] && incycle[b]) return 1;
	return nivel[a] > nivel[b];
}

int main(void)
{
	int n;
	cin >> n;

	map<string, int> mp;

	int ind = 0;

	for (int i = 1; i <= n; i++)
	{
		string s1, s2;
		cin >> s1 >> s2;

		if (mp.find(s1) == mp.end()) mp[s1] = ++ind;
		if (mp.find(s2) == mp.end()) mp[s2] = ++ind;

		f[mp[s1]] = mp[s2];
	}

	if (n&1)
	{
		cout << "-1\n";
		return 0;
	}

	for (int i = 1; i <= n; i++)
		if (!mark[i])
			dfs(i);

	int ans = n;

	vector<int> v;
	for (int i = 1; i <= n; i++)
		v.push_back(i);

	for (int i = 1; i <= cc; i++)
	{
		for (int u = ini[i]; ; u = f[u])
		{
			incycle[u] = 1;
			if (u == fim[i]) break;
		}
	}

	sort(v.begin(), v.end(), comp);

	for (int i = 1; i <= n; i++)
	{
		if (done[i]) continue;
		
		if (f[i] != i && f[f[i]] == i)
		{
			done[i] = done[f[i]] = 1;
			ans -= 2;
		}
	}

	for (auto u: v)
	{
		if (done[u]) continue;

		if (f[u] != u && !done[f[u]])
		{
			ans--;
			done[u] = done[f[u]] = 1;
		}
	}

	cout << ans << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 343 ms 14948 KB Output is correct
5 Correct 325 ms 12516 KB Output is correct
6 Correct 331 ms 15348 KB Output is correct
7 Correct 312 ms 10732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 323 ms 12264 KB Output is correct
2 Correct 334 ms 12672 KB Output is correct
3 Correct 269 ms 12648 KB Output is correct
4 Correct 310 ms 10732 KB Output is correct
5 Correct 331 ms 13544 KB Output is correct
6 Correct 316 ms 12136 KB Output is correct
7 Correct 323 ms 12136 KB Output is correct
8 Correct 290 ms 12644 KB Output is correct
9 Correct 304 ms 12136 KB Output is correct
10 Correct 253 ms 12136 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 364 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 364 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 1 ms 364 KB Output is correct
12 Correct 1 ms 364 KB Output is correct
13 Correct 1 ms 364 KB Output is correct
14 Correct 1 ms 364 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 1 ms 492 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 364 KB Output is correct
19 Correct 343 ms 14948 KB Output is correct
20 Correct 325 ms 12516 KB Output is correct
21 Correct 331 ms 15348 KB Output is correct
22 Correct 312 ms 10732 KB Output is correct
23 Correct 323 ms 12264 KB Output is correct
24 Correct 334 ms 12672 KB Output is correct
25 Correct 269 ms 12648 KB Output is correct
26 Correct 310 ms 10732 KB Output is correct
27 Correct 331 ms 13544 KB Output is correct
28 Correct 316 ms 12136 KB Output is correct
29 Correct 323 ms 12136 KB Output is correct
30 Correct 290 ms 12644 KB Output is correct
31 Correct 304 ms 12136 KB Output is correct
32 Correct 253 ms 12136 KB Output is correct
33 Correct 1 ms 364 KB Output is correct
34 Correct 1 ms 364 KB Output is correct
35 Correct 1 ms 364 KB Output is correct
36 Correct 1 ms 364 KB Output is correct
37 Correct 321 ms 12388 KB Output is correct
38 Incorrect 324 ms 12648 KB Output isn't correct
39 Halted 0 ms 0 KB -