Submission #949322

# Submission time Handle Problem Language Result Execution time Memory
949322 2024-03-19T06:12:07 Z koukirocks Love Polygon (BOI18_polygon) C++17
75 / 100
154 ms 32432 KB
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
 
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
const ll MAX=1e5+10,P=998244353;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
 
int n;
vector<pair<string,string> > eg;
int G[MAX];
vector<int> cptG[MAX];
int dp[MAX][3];
 
void dfs2(int v,int p) {
	if (cptG[v].size()==1) {
		dp[v][0]=0;
		dp[v][1]=-INF;
		return;
	}
	dp[v][0]=0;
	for (int i:cptG[v]) {
		if (i==p) continue;
		dfs2(i,v);
		dp[v][0]+=max(dp[i][0],dp[i][1]);
	}
	dp[v][1]=0;
	for (int i:cptG[v]) {
		if (i==p) continue;
		dp[v][1]=max(dp[v][1],dp[v][0]-max(dp[i][0],dp[i][1])+dp[i][0]+1);
	}
//	cout<<v<<" "<<dp[v][0]<<" "<<dp[v][1]<<"\n";
}
 
int main() {
	speed;
	cin>>n;
	vector<string> dct;
	for (int i=0;i<n;i++) {
		string a,b;
		cin>>a>>b;
		eg.emplace_back(a,b);
		dct.push_back(a);
		dct.push_back(b);
	}
	sort(all(dct));
	dct.resize(unique(all(dct))-dct.begin());
	vector<int> lvd(n,0);
	for (int i=0;i<n;i++) {
		auto [a,b]=eg[i];
		int va=lower_bound(all(dct),a)-dct.begin();
		int vb=lower_bound(all(dct),b)-dct.begin();
		G[va]=vb;
		cptG[va].push_back(vb);
		cptG[vb].push_back(va);
		lvd[vb]++;
	}
	bool alllvd=true;
	for (int i=0;i<n;i++) {
		if (lvd[i]!=1) {
			alllvd=false;
			break;
		}
	}
	if (n&1) {
		cout<<"-1\n";
		return 0;
	}
	if (n<=20) {
		int ans=n;
		for (int st=0;st<(1<<n);st++) {
			bool flag=true;
			vector<int> cnt(n,0);
			for (int i=0;i<n;i++) {
				if (st&(1<<i)) continue;
				if (G[i]==i) {
					flag=false;
					break;
				}
				int tgt=G[i];
				if (G[tgt]==i) continue;
				if (st&(1<<tgt)) {
					if (cnt[tgt]==0) {
						cnt[tgt]++;
						continue;
					}
				}
				flag=false;
				break;
			}
	//		if (flag) cout<<st<<"\n";
			if (flag) ans=min(ans,__builtin_popcount(st));
		}
		cout<<ans<<"\n";
	} else if (alllvd) {
		vector<bool> vis(n,0);
		int ans=0;
		for (int i=0;i<n;i++) {
			if (vis[i]) continue;
			int now=i;
			int st=i;
			int sz=1;
			vis[i]=true;
			while (G[now]!=st) {
//				cout<<now<<" ";
				sz++;
				now=G[now];
				vis[now]=true;
			}
			if (sz==2) continue;
			ans+=(sz+1)>>1;
		}
		cout<<ans<<"\n";
	} else {
		int ans=0;
		for (int i=0;i<n;i++) {
			if (G[i]==i) {
				dfs2(i,i);
				ans+=max(dp[i][1],dp[i][0]);
			}
		}
		cout<<n-ans<<"\n";
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 28 ms 3672 KB Output is correct
2 Correct 23 ms 3672 KB Output is correct
3 Correct 27 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3672 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 27 ms 3820 KB Output is correct
8 Correct 29 ms 3676 KB Output is correct
9 Correct 26 ms 3676 KB Output is correct
10 Correct 30 ms 3824 KB Output is correct
11 Correct 31 ms 3820 KB Output is correct
12 Correct 1 ms 3672 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3676 KB Output is correct
2 Correct 2 ms 3676 KB Output is correct
3 Correct 1 ms 3676 KB Output is correct
4 Correct 131 ms 21012 KB Output is correct
5 Correct 122 ms 21908 KB Output is correct
6 Correct 118 ms 21448 KB Output is correct
7 Correct 119 ms 22928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 24204 KB Output is correct
2 Correct 126 ms 25980 KB Output is correct
3 Correct 117 ms 22944 KB Output is correct
4 Correct 121 ms 25224 KB Output is correct
5 Correct 130 ms 32432 KB Output is correct
6 Correct 132 ms 24468 KB Output is correct
7 Correct 119 ms 24976 KB Output is correct
8 Correct 133 ms 23060 KB Output is correct
9 Correct 132 ms 26000 KB Output is correct
10 Correct 90 ms 26256 KB Output is correct
11 Correct 3 ms 3820 KB Output is correct
12 Correct 1 ms 3676 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 3 ms 3820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 3672 KB Output is correct
2 Correct 23 ms 3672 KB Output is correct
3 Correct 27 ms 3676 KB Output is correct
4 Correct 1 ms 3676 KB Output is correct
5 Correct 1 ms 3672 KB Output is correct
6 Correct 1 ms 3676 KB Output is correct
7 Correct 27 ms 3820 KB Output is correct
8 Correct 29 ms 3676 KB Output is correct
9 Correct 26 ms 3676 KB Output is correct
10 Correct 30 ms 3824 KB Output is correct
11 Correct 31 ms 3820 KB Output is correct
12 Correct 1 ms 3672 KB Output is correct
13 Correct 1 ms 3676 KB Output is correct
14 Correct 1 ms 3676 KB Output is correct
15 Correct 1 ms 3676 KB Output is correct
16 Correct 1 ms 3676 KB Output is correct
17 Correct 2 ms 3676 KB Output is correct
18 Correct 1 ms 3676 KB Output is correct
19 Correct 131 ms 21012 KB Output is correct
20 Correct 122 ms 21908 KB Output is correct
21 Correct 118 ms 21448 KB Output is correct
22 Correct 119 ms 22928 KB Output is correct
23 Correct 128 ms 24204 KB Output is correct
24 Correct 126 ms 25980 KB Output is correct
25 Correct 117 ms 22944 KB Output is correct
26 Correct 121 ms 25224 KB Output is correct
27 Correct 130 ms 32432 KB Output is correct
28 Correct 132 ms 24468 KB Output is correct
29 Correct 119 ms 24976 KB Output is correct
30 Correct 133 ms 23060 KB Output is correct
31 Correct 132 ms 26000 KB Output is correct
32 Correct 90 ms 26256 KB Output is correct
33 Correct 3 ms 3820 KB Output is correct
34 Correct 1 ms 3676 KB Output is correct
35 Correct 1 ms 3676 KB Output is correct
36 Correct 3 ms 3820 KB Output is correct
37 Incorrect 154 ms 24724 KB Output isn't correct
38 Halted 0 ms 0 KB -