Submission #916633

# Submission time Handle Problem Language Result Execution time Memory
916633 2024-01-26T07:57:35 Z penguin133 Love Polygon (BOI18_polygon) C++17
100 / 100
307 ms 38832 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

map <string, int> mp;
int n, cnt = 1;
vector <int> rev[200005];
int nxt[200005];
int vis[200005], cyc[200005], memo[200005][2];
vector <int> v;

void dfs(int x){
	//cerr << x << '\n';
	if(vis[x] == 2)return;
	if(vis[x] == 1){
		cyc[x] = 1;
		int cur = x;
		v.push_back(cur);
		cur = nxt[cur];
		while(cur != x){
			//cerr << cur << ' ';
			cyc[cur] = 1;
			v.push_back(cur), cur = nxt[cur];
		}
		return;
	}
	vis[x] = 1;
	dfs(nxt[x]);
	vis[x] = 2;
}

int dp(int x, int f){
	if(memo[x][f] != -1)return memo[x][f];
	vis[x] = 2;
	if(f){
		int ans = 1, mn = 1e18;
		for(auto i : rev[x]){
			if(cyc[i])continue;
			ans += dp(i, 1);
			mn = min(mn, dp(i, 0) - dp(i, 1));
		}
		if(mn != 1e18)ans += mn;
		return memo[x][f] = ans;
	}
	else{
		int ans = 0;
		for(auto i : rev[x])if(!cyc[i])ans += dp(i, 1);
		return memo[x][f] = ans;
	}
}

int memo2[200005][2][2][2];
int dp2(int pos, bool prv, bool prv2, bool first){
	if(pos == (int)v.size())return prv;
	if(memo2[pos][prv][prv2][first] != -1)return memo2[pos][prv][prv2][first];
	int ans = 1e18;
	if(prv){
		ans = min(ans, dp2(pos + 1, 0, 0, 0) + !prv2 + (pos < (int)v.size() - 1 || !first) + dp(v[pos], 0));
		ans = min(ans, dp2(pos + 1, 1, 0, first) + dp(v[pos], 1));
		return memo2[pos][prv][prv2][first] = ans;
	}
	else{
		ans = min(ans, dp2(pos + 1, 1, 1, (pos == 0)) + dp(v[pos], 0));
		ans = min(ans, dp2(pos + 1, 0, 0, 0) + dp(v[pos], 1));
		return memo2[pos][prv][prv2][first] = ans;
	}
}

void solve(){
	cin >> n;
	memset(memo, -1, sizeof(memo));
	for(int i=1;i<=n;i++){
		string a, b; cin >> a >> b;
		if(mp.find(a) == mp.end())mp[a] = cnt++;
		if(mp.find(b) == mp.end())mp[b] = cnt++;
		nxt[mp[a]] = mp[b];
		rev[mp[b]].push_back(mp[a]);
	}
	if(n%2){
		cout << -1 << '\n';
		return;
	}
	int ans = 0;
	for(int i=1;i<=n;i++){
		if(!vis[i]){
			v.clear();
			dfs(i);
			/*
			if((int)v.size() % 2 == 0){
				vector <int> tmp;
				ans += (int)v.size() == 2 ? 0 : (int)v.size() / 2;
				for(auto j : v)ans += dp(j, 0);
			}
			else{
				ans += (int)v.size() / 2;
				int mn = 1e18;
				vector <int> tmp;
				for(auto j : v){
					ans += dp(j, 0);
					mn = min(mn, (dp(j, 1) - dp(j, 0)));
				}
				ans += mn;
			}
			*/
			for(int i = 0; i < (int)v.size(); i++){
				for(int a=0;a<2;a++){
					for(int b=0;b<2;b++)for(int c=0;c<2;c++)memo2[i][a][b][c] = -1;
				}
			}
			int tmp = dp2(0, 0, 0, 0);
			v.push_back(v[0]);
			for(int i = 0; i < (int)v.size(); i++){
				for(int a=0;a<2;a++){
					for(int b=0;b<2;b++)for(int c=0;c<2;c++)memo2[i][a][b][c] = -1;
				}
			}
			v.erase(v.begin());
			int tmp2 = dp2(0, 0, 0, 0);
			ans += min(tmp, tmp2);
			//cout << ans << '\n';
		}
	}
	cout << ans << '\n';
	
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Compilation message

polygon.cpp:136:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  136 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10992 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 4 ms 10844 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 5 ms 10976 KB Output is correct
8 Correct 4 ms 10844 KB Output is correct
9 Correct 3 ms 10840 KB Output is correct
10 Correct 3 ms 10844 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 3 ms 10976 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 277 ms 36636 KB Output is correct
5 Correct 233 ms 23864 KB Output is correct
6 Correct 307 ms 38832 KB Output is correct
7 Correct 203 ms 23092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 268 ms 21884 KB Output is correct
2 Correct 293 ms 26472 KB Output is correct
3 Correct 182 ms 23536 KB Output is correct
4 Correct 250 ms 20924 KB Output is correct
5 Correct 300 ms 31512 KB Output is correct
6 Correct 271 ms 21584 KB Output is correct
7 Correct 251 ms 22388 KB Output is correct
8 Correct 256 ms 22692 KB Output is correct
9 Correct 182 ms 21152 KB Output is correct
10 Correct 158 ms 20388 KB Output is correct
11 Correct 4 ms 8284 KB Output is correct
12 Correct 4 ms 8284 KB Output is correct
13 Correct 4 ms 8284 KB Output is correct
14 Correct 5 ms 8312 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 10992 KB Output is correct
2 Correct 3 ms 10844 KB Output is correct
3 Correct 4 ms 10844 KB Output is correct
4 Correct 3 ms 10844 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 3 ms 10844 KB Output is correct
7 Correct 5 ms 10976 KB Output is correct
8 Correct 4 ms 10844 KB Output is correct
9 Correct 3 ms 10840 KB Output is correct
10 Correct 3 ms 10844 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 3 ms 10976 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10844 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 4 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 2 ms 10844 KB Output is correct
19 Correct 277 ms 36636 KB Output is correct
20 Correct 233 ms 23864 KB Output is correct
21 Correct 307 ms 38832 KB Output is correct
22 Correct 203 ms 23092 KB Output is correct
23 Correct 268 ms 21884 KB Output is correct
24 Correct 293 ms 26472 KB Output is correct
25 Correct 182 ms 23536 KB Output is correct
26 Correct 250 ms 20924 KB Output is correct
27 Correct 300 ms 31512 KB Output is correct
28 Correct 271 ms 21584 KB Output is correct
29 Correct 251 ms 22388 KB Output is correct
30 Correct 256 ms 22692 KB Output is correct
31 Correct 182 ms 21152 KB Output is correct
32 Correct 158 ms 20388 KB Output is correct
33 Correct 4 ms 8284 KB Output is correct
34 Correct 4 ms 8284 KB Output is correct
35 Correct 4 ms 8284 KB Output is correct
36 Correct 5 ms 8312 KB Output is correct
37 Correct 207 ms 23376 KB Output is correct
38 Correct 220 ms 26160 KB Output is correct
39 Correct 227 ms 22352 KB Output is correct
40 Correct 269 ms 22100 KB Output is correct
41 Correct 243 ms 21844 KB Output is correct
42 Correct 229 ms 22352 KB Output is correct
43 Correct 248 ms 22608 KB Output is correct
44 Correct 284 ms 22608 KB Output is correct
45 Correct 258 ms 22608 KB Output is correct
46 Correct 249 ms 22492 KB Output is correct
47 Correct 201 ms 21616 KB Output is correct
48 Correct 251 ms 22840 KB Output is correct
49 Correct 209 ms 24400 KB Output is correct
50 Correct 172 ms 24648 KB Output is correct
51 Correct 213 ms 21796 KB Output is correct
52 Correct 271 ms 32552 KB Output is correct
53 Correct 239 ms 22608 KB Output is correct
54 Correct 228 ms 23140 KB Output is correct
55 Correct 234 ms 23608 KB Output is correct
56 Correct 212 ms 22084 KB Output is correct
57 Correct 147 ms 21528 KB Output is correct
58 Correct 4 ms 9816 KB Output is correct
59 Correct 6 ms 9820 KB Output is correct
60 Correct 5 ms 9820 KB Output is correct
61 Correct 3 ms 10840 KB Output is correct
62 Correct 4 ms 10988 KB Output is correct
63 Correct 6 ms 8244 KB Output is correct
64 Correct 5 ms 8280 KB Output is correct
65 Correct 307 ms 36652 KB Output is correct
66 Correct 267 ms 23756 KB Output is correct
67 Correct 294 ms 38700 KB Output is correct
68 Correct 213 ms 22976 KB Output is correct
69 Correct 3 ms 9820 KB Output is correct
70 Correct 4 ms 10844 KB Output is correct
71 Correct 3 ms 10844 KB Output is correct
72 Correct 3 ms 10844 KB Output is correct
73 Correct 4 ms 10844 KB Output is correct
74 Correct 3 ms 10840 KB Output is correct
75 Correct 3 ms 10844 KB Output is correct
76 Correct 3 ms 10840 KB Output is correct
77 Correct 3 ms 10844 KB Output is correct
78 Correct 3 ms 10844 KB Output is correct
79 Correct 2 ms 10884 KB Output is correct
80 Correct 2 ms 10840 KB Output is correct
81 Correct 4 ms 10844 KB Output is correct
82 Correct 3 ms 10840 KB Output is correct
83 Correct 3 ms 10844 KB Output is correct