답안 #415550

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
415550 2021-06-01T08:33:58 Z Blagojce Love Polygon (BOI18_polygon) C++11
100 / 100
223 ms 32964 KB
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
  
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pii;
const ll inf = 1e18;
const int i_inf = 1e9;
const ll mod = 1e9+7;

mt19937 _rand(time(NULL));
const int mxn = 2e5;

bool vis[mxn];

int n;
vector<int> g[mxn];
vector<int> tree[mxn];


int link[mxn];

unordered_map<string, int> mapa;


vector<vector<int> > R;

vector<int> r;

void mark(int u){
	vis[u] = true;
	for(auto e : g[u]){
		if(vis[e]) continue;
		tree[u].pb(e);
		mark(e);
	}
}

void find_roots(){
	fr(i, 0, n){
		if(vis[i]) continue;
		int x = i;
		
		vector<int> undo;
		
		while(!vis[x]){
			vis[x] = true;
			undo.pb(x);
			x = link[x];
		}
		int a = x;
		vector<int> v;
		
		do{
			v.pb(a);
			a = link[a];
		}while(a != x);
		
		R.pb(v);
		
		for(auto u : undo) vis[u] = false;
		for(auto u : v) vis[u] = true;
		
		for(auto u : v){
			r.pb(u);
			mark(u);
		}
	}
}
int dp[mxn][2];

void dfs(int u){
	for(auto e : tree[u]){
		dfs(e);
	}
	
	int minsum = 0;
	for(auto e : tree[u]){
		minsum += min(dp[e][0], dp[e][1]);
	}
	dp[u][0] = dp[u][1] = minsum + 1;
	for(auto e : tree[u]){
		int tempsum = minsum - min(dp[e][0], dp[e][1]) + dp[e][0];
		dp[u][1] = min(dp[u][1], tempsum);
	}
}
bool used[mxn];
int ans;

void solve(){
	cin >> n;
	
	int c = 0;
	
	fr(i, 0, n){
		link[i] = i;
	}
	
	fr(i, 0, n){
		string s;
		cin >> s;
		if(mapa[s] == 0){
			mapa[s] = ++c;
		}
		int u = mapa[s]-1;
		cin >> s;
		if(mapa[s] == 0){
			mapa[s] = ++c;
		}
		int v = mapa[s]-1;
		link[u] = v;
		
		g[v].pb(u);
	}
	
	
	
	if(n % 2){
		cout<<-1<<endl;
		return;
	}
	
	find_roots();
	
	for(auto u : r){
		dfs(u);	
	}
	
	for(auto vec : R){
		int len = (int)vec.size();
		
		
		if(len == 2){
			for(auto u : vec){
				ans += dp[u][0];
			}	
			ans -= 2;
			continue;
		}
		
		
		fr(i, 0, len){
			int u = vec[i];
			used[i] = dp[u][1] >= dp[u][0];
			ans += min(dp[u][1], dp[u][0]);
		}
		int l = 0;
		fr(i, 0, len){
			if(used[i]) ++l;
			else break;
		}
		
		
		if(l == len){
			if(l == 2){
				ans -= 2;
			}
			else ans -= len/2;
		}
		else{
			int r = 0;
			for(int i = len - 1; i >= 0; i --){
				if(used[i]) ++r;
				else break;
			}
			int con = 0;
			vector<int> tr;
			tr.pb(l+r);
			fr(i, l, len-r){
				if(used[i]) con ++;
				else{
					if(con > 0) tr.pb(con);
					con = 0;
				}
			}		
			if(con > 0){
				tr.pb(con);
			}
			for(auto u : tr){
				
				ans -= u/2;
			}
		}	
		
	}
	cout<<ans<<endl;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	solve();
	
}


/*
. 



.-.
|
.*/





# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9676 KB Output is correct
2 Correct 7 ms 9724 KB Output is correct
3 Correct 7 ms 9784 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 10 ms 9724 KB Output is correct
6 Correct 8 ms 9728 KB Output is correct
7 Correct 9 ms 9676 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 8 ms 9676 KB Output is correct
10 Correct 6 ms 9724 KB Output is correct
11 Correct 8 ms 9652 KB Output is correct
12 Correct 7 ms 9676 KB Output is correct
13 Correct 8 ms 9676 KB Output is correct
14 Correct 6 ms 9728 KB Output is correct
15 Correct 6 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9688 KB Output is correct
4 Correct 198 ms 25888 KB Output is correct
5 Correct 138 ms 25988 KB Output is correct
6 Correct 197 ms 26296 KB Output is correct
7 Correct 157 ms 22904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 223 ms 25564 KB Output is correct
2 Correct 187 ms 27444 KB Output is correct
3 Correct 160 ms 29728 KB Output is correct
4 Correct 168 ms 21428 KB Output is correct
5 Correct 213 ms 32784 KB Output is correct
6 Correct 135 ms 23984 KB Output is correct
7 Correct 176 ms 23988 KB Output is correct
8 Correct 175 ms 26692 KB Output is correct
9 Correct 144 ms 23132 KB Output is correct
10 Correct 103 ms 21964 KB Output is correct
11 Correct 7 ms 9728 KB Output is correct
12 Correct 9 ms 9676 KB Output is correct
13 Correct 7 ms 9632 KB Output is correct
14 Correct 8 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9676 KB Output is correct
2 Correct 7 ms 9724 KB Output is correct
3 Correct 7 ms 9784 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 10 ms 9724 KB Output is correct
6 Correct 8 ms 9728 KB Output is correct
7 Correct 9 ms 9676 KB Output is correct
8 Correct 9 ms 9728 KB Output is correct
9 Correct 8 ms 9676 KB Output is correct
10 Correct 6 ms 9724 KB Output is correct
11 Correct 8 ms 9652 KB Output is correct
12 Correct 7 ms 9676 KB Output is correct
13 Correct 8 ms 9676 KB Output is correct
14 Correct 6 ms 9728 KB Output is correct
15 Correct 6 ms 9676 KB Output is correct
16 Correct 6 ms 9676 KB Output is correct
17 Correct 6 ms 9676 KB Output is correct
18 Correct 6 ms 9688 KB Output is correct
19 Correct 198 ms 25888 KB Output is correct
20 Correct 138 ms 25988 KB Output is correct
21 Correct 197 ms 26296 KB Output is correct
22 Correct 157 ms 22904 KB Output is correct
23 Correct 223 ms 25564 KB Output is correct
24 Correct 187 ms 27444 KB Output is correct
25 Correct 160 ms 29728 KB Output is correct
26 Correct 168 ms 21428 KB Output is correct
27 Correct 213 ms 32784 KB Output is correct
28 Correct 135 ms 23984 KB Output is correct
29 Correct 176 ms 23988 KB Output is correct
30 Correct 175 ms 26692 KB Output is correct
31 Correct 144 ms 23132 KB Output is correct
32 Correct 103 ms 21964 KB Output is correct
33 Correct 7 ms 9728 KB Output is correct
34 Correct 9 ms 9676 KB Output is correct
35 Correct 7 ms 9632 KB Output is correct
36 Correct 8 ms 9676 KB Output is correct
37 Correct 206 ms 26420 KB Output is correct
38 Correct 187 ms 24280 KB Output is correct
39 Correct 148 ms 24324 KB Output is correct
40 Correct 174 ms 24000 KB Output is correct
41 Correct 136 ms 24024 KB Output is correct
42 Correct 160 ms 24732 KB Output is correct
43 Correct 184 ms 24756 KB Output is correct
44 Correct 152 ms 24752 KB Output is correct
45 Correct 145 ms 24760 KB Output is correct
46 Correct 147 ms 24664 KB Output is correct
47 Correct 121 ms 23040 KB Output is correct
48 Correct 174 ms 25544 KB Output is correct
49 Correct 195 ms 27488 KB Output is correct
50 Correct 112 ms 29736 KB Output is correct
51 Correct 112 ms 21424 KB Output is correct
52 Correct 169 ms 32964 KB Output is correct
53 Correct 140 ms 23956 KB Output is correct
54 Correct 161 ms 23952 KB Output is correct
55 Correct 163 ms 26732 KB Output is correct
56 Correct 115 ms 22968 KB Output is correct
57 Correct 110 ms 22056 KB Output is correct
58 Correct 7 ms 9648 KB Output is correct
59 Correct 7 ms 9676 KB Output is correct
60 Correct 10 ms 9676 KB Output is correct
61 Correct 7 ms 9676 KB Output is correct
62 Correct 8 ms 9736 KB Output is correct
63 Correct 8 ms 9616 KB Output is correct
64 Correct 7 ms 9728 KB Output is correct
65 Correct 139 ms 25940 KB Output is correct
66 Correct 177 ms 26068 KB Output is correct
67 Correct 166 ms 26144 KB Output is correct
68 Correct 118 ms 22968 KB Output is correct
69 Correct 8 ms 9676 KB Output is correct
70 Correct 7 ms 9676 KB Output is correct
71 Correct 8 ms 9724 KB Output is correct
72 Correct 9 ms 9676 KB Output is correct
73 Correct 7 ms 9724 KB Output is correct
74 Correct 7 ms 9644 KB Output is correct
75 Correct 7 ms 9724 KB Output is correct
76 Correct 7 ms 9676 KB Output is correct
77 Correct 7 ms 9676 KB Output is correct
78 Correct 7 ms 9676 KB Output is correct
79 Correct 9 ms 9676 KB Output is correct
80 Correct 7 ms 9676 KB Output is correct
81 Correct 9 ms 9708 KB Output is correct
82 Correct 9 ms 9728 KB Output is correct
83 Correct 8 ms 9728 KB Output is correct