Submission #862868

# Submission time Handle Problem Language Result Execution time Memory
862868 2023-10-19T07:12:01 Z maks007 Love Polygon (BOI18_polygon) C++14
75 / 100
338 ms 25440 KB
//Bismi Allah
#include "bits/stdc++.h"

using namespace std;

signed main () {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	map <string,int> mp;
	vector <int> love(n, 0);
	vector <int> used(n, 0), dp(n, 0), g[n], unite(n, 0), loved(n, 0), rk(n, 0);
	function <void(int,int)> dfs=[&](int v, int p) {
		used[v] = 1;
		rk[v] = 1;
		for(auto u : g[v]) {
			if(used[u]) continue;
			dfs(u, v);
			rk[v] += rk[u];
		}
		if(unite[v]) return;
		if(p != -1 && unite[p] == 0) {
			unite[p] = 1;
			unite[v] = 1;
		}
	};
	for(int i = 0; i < n; i ++) {
		string u, v;
		cin >> u >> v;
		if(mp.count(u) == 0) mp[u] = mp.size();
		if(mp.count(v) == 0) mp[v] = mp.size();
		loved[mp[v]] ++;
		love[mp[u]] = mp[v];
		if(mp[u] == mp[v]) continue;
		g[mp[u]].push_back(mp[v]);
		g[mp[v]].push_back(mp[u]);
	}
	if(n <= 20) {
		vector <int> forMask;
		for(int mask = 0; mask < (1 << n); mask ++) {
			if(__builtin_popcount(mask) % 2 != 0) continue;
			forMask.push_back(mask);
		}
		sort(forMask.begin(), forMask.end(), [](int a, int b) {
			return __builtin_popcount(a) < __builtin_popcount(b);
		});
		vector <int> Dp(1 << n, 1e9);
		Dp[0] = 0;
		for(auto mask : forMask) {
			if(mask == 0) continue;
			vector <int> v;
			for(int i = 0; i < n; i ++) {
				if(mask & (1 << i)) {
					v.push_back(i);
				}
			}
			for(int i = 0; i < v.size(); i ++) {
				for(int j = i + 1; j < v.size(); j ++) {
					int cost = 0, newmask = mask;
					newmask ^= (1 << v[i]);
					newmask ^= (1 << v[j]);
					if(love[v[i]] == v[j] && love[v[j]] == v[i]) cost = 0;
					else if(love[v[i]] == v[j] || love[v[j]] == v[i]) cost = 1;
					else cost = 2;
					Dp[mask] = min(Dp[mask], Dp[newmask] + cost);
				}
			}
		}
		if(Dp[(1 << n) - 1] == 1e9) cout << -1;
		else
		cout << Dp[(1 << n) - 1];
		return 0;
	}else
	if(*min_element(loved.begin(), loved.end()) >= 1) {
		if(n % 2 == 1) {
		//	assert(false);
			cout << -1;
			return 0;
		}
		int ans = 0, mod = 0;
		for(int i = 0; i < n; i ++) {
			if(used[i]) continue;
			dfs(i, -1);
			if(rk[i] == 2) continue;
			ans += rk[i] / 2;
			mod += (rk[i] % 2);
		}
		if(mod % 2 == 1) cout << -1;
		else cout << ans + mod;
		return 0;
	}
	if(n % 2 == 1) {
	//	assert(false);
		cout << -1;
		return 0;
	}
	int ans = 0, mod = 0;
	for(int i = 0; i < n; i ++) {
		if(used[i]) continue;
		dfs(i, -1);
	}
	for(auto i : unite) {
		ans += (i == 1);
		mod += (i == 0);
	}
	cout << ans/2+mod;
	
	return 0;
}

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:58:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |    for(int i = 0; i < v.size(); i ++) {
      |                   ~~^~~~~~~~~~
polygon.cpp:59:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for(int j = i + 1; j < v.size(); j ++) {
      |                        ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 198 ms 6484 KB Output is correct
2 Correct 182 ms 6484 KB Output is correct
3 Correct 189 ms 6600 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 90 ms 3680 KB Output is correct
7 Correct 194 ms 6592 KB Output is correct
8 Correct 199 ms 6736 KB Output is correct
9 Correct 187 ms 6848 KB Output is correct
10 Correct 192 ms 6644 KB Output is correct
11 Correct 186 ms 6848 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 309 ms 24232 KB Output is correct
5 Correct 334 ms 15788 KB Output is correct
6 Correct 320 ms 25440 KB Output is correct
7 Correct 282 ms 15956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 331 ms 15924 KB Output is correct
2 Correct 338 ms 17496 KB Output is correct
3 Correct 179 ms 12736 KB Output is correct
4 Correct 304 ms 16124 KB Output is correct
5 Correct 318 ms 21356 KB Output is correct
6 Correct 300 ms 16208 KB Output is correct
7 Correct 283 ms 16100 KB Output is correct
8 Correct 310 ms 15288 KB Output is correct
9 Correct 271 ms 16276 KB Output is correct
10 Correct 197 ms 16324 KB Output is correct
11 Correct 9 ms 856 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 10 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 198 ms 6484 KB Output is correct
2 Correct 182 ms 6484 KB Output is correct
3 Correct 189 ms 6600 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 90 ms 3680 KB Output is correct
7 Correct 194 ms 6592 KB Output is correct
8 Correct 199 ms 6736 KB Output is correct
9 Correct 187 ms 6848 KB Output is correct
10 Correct 192 ms 6644 KB Output is correct
11 Correct 186 ms 6848 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 309 ms 24232 KB Output is correct
20 Correct 334 ms 15788 KB Output is correct
21 Correct 320 ms 25440 KB Output is correct
22 Correct 282 ms 15956 KB Output is correct
23 Correct 331 ms 15924 KB Output is correct
24 Correct 338 ms 17496 KB Output is correct
25 Correct 179 ms 12736 KB Output is correct
26 Correct 304 ms 16124 KB Output is correct
27 Correct 318 ms 21356 KB Output is correct
28 Correct 300 ms 16208 KB Output is correct
29 Correct 283 ms 16100 KB Output is correct
30 Correct 310 ms 15288 KB Output is correct
31 Correct 271 ms 16276 KB Output is correct
32 Correct 197 ms 16324 KB Output is correct
33 Correct 9 ms 856 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 10 ms 860 KB Output is correct
37 Correct 324 ms 18260 KB Output is correct
38 Correct 300 ms 19764 KB Output is correct
39 Incorrect 305 ms 18256 KB Output isn't correct
40 Halted 0 ms 0 KB -