Submission #657416

# Submission time Handle Problem Language Result Execution time Memory
657416 2022-11-09T19:54:02 Z inksamurai Love Polygon (BOI18_polygon) C++17
100 / 100
233 ms 43624 KB
#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3NRqilq ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

#define all(a) a.begin(),a.end()
#define nare {cout<<"-1\n";exit(0);}

const int inf=1e9;

signed main(){
_3NRqilq;
	int m;
	cin>>m;

	vec(pair<string,string>) _es;
	vec(string) tmp;
	rep(i,m){
		string s,t;
		cin>>s>>t;
		tmp.pb(s);
		tmp.pb(t);
		_es.pb({s,t});
	}
	
	sort(all(tmp));
	tmp.erase(unique(all(tmp)),tmp.end());
	int n=sz(tmp);

	if(n%2){
		nare;
	}
	
	vec(vi) adj(n),radj(n);
	rep(i,m){
		auto [s,t]=_es[i];
		int u=lower_bound(all(tmp),s)-tmp.begin();
		int v=lower_bound(all(tmp),t)-tmp.begin();
		adj[u].pb(v);
		radj[v].pb(u);
	}
	
	vi usd(n);
	vi way,cyc;
	vi incyc(n);
	vec(vi) dp(n,vi(2));
	vi edp(n);
	auto fout_cycle=[&]()->int{
		auto dfs=[&](auto self,int v)->void{
			usd[v]=1;
			dp[v][0]+=1;
			int now=0,mi=0;
			for(auto u:radj[v]){
				if(incyc[u]) continue;
				self(self,u);
				now+=dp[u][0];
				mi=min(mi,-dp[u][0]+dp[u][1]);
			}
			dp[v][0]+=now+mi;
			dp[v][1]=now;
		};
		for(auto v:cyc){
			incyc[v]=1;
		}
		for(auto v:cyc){
			dfs(dfs,v);
		}
		for(auto v:cyc){
			edp[v]=0; // dp(v,0) but is disconnected from child
			for(auto u:radj[v]){
				if(incyc[u]) continue;
				edp[v]+=dp[u][0];
			}
		}
		int res=0;
		if(sz(cyc)==1){
			res=dp[cyc.back()][0];
		}else{
			int s=cyc[0];
			int t=cyc.back();
			if(sz(cyc)==2){
				res=min(dp[s][1]+dp[t][1],dp[s][0]+dp[t][0]);
			}else{
				// (s,t) are connected
				{
					vi fdp(2,inf);
					rep(i,sz(cyc)-1){
						int v=cyc[i];
						vi gdp(2,inf);
						if(!i){
							gdp[0]=edp[v]+1;
							gdp[1]=inf;
						}else{
							// last was 1
							gdp[0]=min(gdp[0],fdp[1]+edp[v]+1);
							// last was 0
							gdp[0]=min(gdp[0],fdp[0]+dp[v][0]);
							gdp[1]=min(gdp[1],fdp[0]+dp[v][1]);
						}
						fdp=gdp;
					}
					res=dp[t][1]+fdp[0];
				}
				// (s,t) are disconnected
				{
					vi fdp(2,inf);
					rep(i,sz(cyc)){
						int v=cyc[i];
						vi gdp(2,inf);
						if(!i){
							gdp[0]=dp[v][0];
							gdp[1]=dp[v][1];
						}else{
							// last was 1
							gdp[0]=min(gdp[0],fdp[1]+edp[v]+1);
							// last was 0
							gdp[0]=min(gdp[0],fdp[0]+dp[v][0]);
							gdp[1]=min(gdp[1],fdp[0]+dp[v][1]);
						}
						fdp=gdp;
					}
					res=min(res,fdp[0]);
				}
			}
		}
		return res;
	};

	// just find one cycle 
	auto dfs=[&](auto self,int v)->void{
		usd[v]=1;
		way.pb(v);
		for(auto u:adj[v]){
			if(usd[u]){
				if(!sz(cyc)){
					per(i,sz(way)){
						cyc.pb(way[i]);
						if(way[i]==u) break;
					}
					reverse(all(cyc));
				}
				return;
			}
			if(!usd[u]){
				self(self,u);
			}
		}
		way.pop_back();
	};

	int ans=0;
	rep(rt,n){
		if(usd[rt]) continue;
		way.clear();
		cyc.clear();
		dfs(dfs,rt);
		ans+=fout_cycle();
	}
	print(ans);
}
# 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 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 0 ms 212 KB Output is correct
4 Correct 233 ms 42164 KB Output is correct
5 Correct 209 ms 31608 KB Output is correct
6 Correct 223 ms 43516 KB Output is correct
7 Correct 83 ms 13176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 199 ms 30920 KB Output is correct
2 Correct 196 ms 33232 KB Output is correct
3 Correct 179 ms 31628 KB Output is correct
4 Correct 79 ms 13092 KB Output is correct
5 Correct 225 ms 39696 KB Output is correct
6 Correct 184 ms 30256 KB Output is correct
7 Correct 202 ms 30376 KB Output is correct
8 Correct 188 ms 30584 KB Output is correct
9 Correct 174 ms 29960 KB Output is correct
10 Correct 159 ms 29200 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 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 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 233 ms 42164 KB Output is correct
20 Correct 209 ms 31608 KB Output is correct
21 Correct 223 ms 43516 KB Output is correct
22 Correct 83 ms 13176 KB Output is correct
23 Correct 199 ms 30920 KB Output is correct
24 Correct 196 ms 33232 KB Output is correct
25 Correct 179 ms 31628 KB Output is correct
26 Correct 79 ms 13092 KB Output is correct
27 Correct 225 ms 39696 KB Output is correct
28 Correct 184 ms 30256 KB Output is correct
29 Correct 202 ms 30376 KB Output is correct
30 Correct 188 ms 30584 KB Output is correct
31 Correct 174 ms 29960 KB Output is correct
32 Correct 159 ms 29200 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 199 ms 31372 KB Output is correct
38 Correct 203 ms 32512 KB Output is correct
39 Correct 200 ms 30584 KB Output is correct
40 Correct 189 ms 30348 KB Output is correct
41 Correct 188 ms 30372 KB Output is correct
42 Correct 190 ms 30580 KB Output is correct
43 Correct 195 ms 30728 KB Output is correct
44 Correct 189 ms 30596 KB Output is correct
45 Correct 191 ms 30600 KB Output is correct
46 Correct 193 ms 30728 KB Output is correct
47 Correct 175 ms 30080 KB Output is correct
48 Correct 208 ms 30924 KB Output is correct
49 Correct 209 ms 33156 KB Output is correct
50 Correct 183 ms 31704 KB Output is correct
51 Correct 78 ms 13096 KB Output is correct
52 Correct 213 ms 39692 KB Output is correct
53 Correct 188 ms 30348 KB Output is correct
54 Correct 187 ms 30352 KB Output is correct
55 Correct 191 ms 30472 KB Output is correct
56 Correct 181 ms 29968 KB Output is correct
57 Correct 146 ms 29192 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 0 ms 212 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 0 ms 212 KB Output is correct
65 Correct 224 ms 42120 KB Output is correct
66 Correct 211 ms 31640 KB Output is correct
67 Correct 223 ms 43624 KB Output is correct
68 Correct 76 ms 13072 KB Output is correct
69 Correct 0 ms 212 KB Output is correct
70 Correct 0 ms 212 KB Output is correct
71 Correct 0 ms 212 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 0 ms 212 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 0 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 0 ms 212 KB Output is correct
81 Correct 0 ms 212 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 0 ms 212 KB Output is correct