Submission #398122

# Submission time Handle Problem Language Result Execution time Memory
398122 2021-05-03T17:57:15 Z AmineWeslati Love Polygon (BOI18_polygon) C++14
100 / 100
383 ms 17248 KB
#include "bits/stdc++.h"
using namespace std; 

typedef long long ll;
typedef vector<int>vi;
#define pb push_back
#define sz(v) (int)v.size()
typedef string str;
#define FOR(i,a,b) for(int i=a; i<b; i++)

void IO(){
#ifdef LOCAL
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
#endif
}
//------------------------------------------------------//

void ckmin(int &x, int y){x=min(x,y);}

const int MX=2e5;
int N;
vi out(MX),in(MX,0);
map<str,int>mp;

vi vis(MX,0);


int main(){
	IO();

	cin>>N;
	int cnt=0;
	FOR(i,0,N){
		str s,ss; cin>>s>>ss;
		if(!mp.count(s)) mp[s]=cnt++;
		if(!mp.count(ss)) mp[ss]=cnt++;

		out[mp[s]]=mp[ss];
		if(s!=ss) in[mp[ss]]++;
	}
	if(N&1){
		cout << -1 << endl;
		return 0;
	}

	FOR(i,0,N) if(out[i]!=i && out[out[i]]==i) vis[i]=1,vis[out[i]]=1;

	set<int>s;
	FOR(i,0,N) if(in[i]==0) s.insert(i);

	int ans=0;
	while(sz(s)){
		auto it=s.begin();
		int u=*it;
		s.erase(it);

		if(out[u]==u) continue;

		if(!vis[out[u]]){
			if(out[out[u]]!=out[u]){
				in[out[out[u]]]--;
				if(in[out[out[u]]]==0) s.insert(out[out[u]]);	
			}

			vis[u]=1; vis[out[u]]=1;
			out[out[u]]=u;
			ans++;
		}
	}


	FOR(i,0,N){
		int n=0,cur=i;
		while(!vis[cur]){
			vis[cur]=1; n++;
			cur=out[cur];
		}
		if(n!=2) ans+=(n+1)/2;
	}
	cout << ans << endl;
	
	
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2664 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2660 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2660 KB Output is correct
15 Correct 2 ms 2660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 298 ms 10544 KB Output is correct
5 Correct 312 ms 10572 KB Output is correct
6 Correct 327 ms 10512 KB Output is correct
7 Correct 312 ms 10556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 11696 KB Output is correct
2 Correct 310 ms 11260 KB Output is correct
3 Correct 256 ms 15176 KB Output is correct
4 Correct 296 ms 10524 KB Output is correct
5 Correct 296 ms 10428 KB Output is correct
6 Correct 327 ms 12980 KB Output is correct
7 Correct 299 ms 12852 KB Output is correct
8 Correct 289 ms 13436 KB Output is correct
9 Correct 301 ms 13872 KB Output is correct
10 Correct 260 ms 15172 KB Output is correct
11 Correct 2 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2636 KB Output is correct
2 Correct 2 ms 2636 KB Output is correct
3 Correct 2 ms 2636 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 2 ms 2636 KB Output is correct
7 Correct 2 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 2 ms 2664 KB Output is correct
10 Correct 2 ms 2636 KB Output is correct
11 Correct 2 ms 2660 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2668 KB Output is correct
14 Correct 2 ms 2660 KB Output is correct
15 Correct 2 ms 2660 KB Output is correct
16 Correct 2 ms 2636 KB Output is correct
17 Correct 2 ms 2636 KB Output is correct
18 Correct 2 ms 2636 KB Output is correct
19 Correct 298 ms 10544 KB Output is correct
20 Correct 312 ms 10572 KB Output is correct
21 Correct 327 ms 10512 KB Output is correct
22 Correct 312 ms 10556 KB Output is correct
23 Correct 313 ms 11696 KB Output is correct
24 Correct 310 ms 11260 KB Output is correct
25 Correct 256 ms 15176 KB Output is correct
26 Correct 296 ms 10524 KB Output is correct
27 Correct 296 ms 10428 KB Output is correct
28 Correct 327 ms 12980 KB Output is correct
29 Correct 299 ms 12852 KB Output is correct
30 Correct 289 ms 13436 KB Output is correct
31 Correct 301 ms 13872 KB Output is correct
32 Correct 260 ms 15172 KB Output is correct
33 Correct 2 ms 2636 KB Output is correct
34 Correct 2 ms 2636 KB Output is correct
35 Correct 2 ms 2636 KB Output is correct
36 Correct 2 ms 2636 KB Output is correct
37 Correct 316 ms 13044 KB Output is correct
38 Correct 311 ms 14744 KB Output is correct
39 Correct 338 ms 14764 KB Output is correct
40 Correct 354 ms 14940 KB Output is correct
41 Correct 314 ms 14968 KB Output is correct
42 Correct 365 ms 14288 KB Output is correct
43 Correct 312 ms 14284 KB Output is correct
44 Correct 312 ms 14336 KB Output is correct
45 Correct 315 ms 14332 KB Output is correct
46 Correct 323 ms 14312 KB Output is correct
47 Correct 312 ms 15924 KB Output is correct
48 Correct 322 ms 13752 KB Output is correct
49 Correct 315 ms 13252 KB Output is correct
50 Correct 267 ms 17248 KB Output is correct
51 Correct 304 ms 12604 KB Output is correct
52 Correct 350 ms 12612 KB Output is correct
53 Correct 317 ms 14904 KB Output is correct
54 Correct 383 ms 14960 KB Output is correct
55 Correct 356 ms 15532 KB Output is correct
56 Correct 314 ms 15960 KB Output is correct
57 Correct 255 ms 17220 KB Output is correct
58 Correct 2 ms 2636 KB Output is correct
59 Correct 2 ms 2644 KB Output is correct
60 Correct 2 ms 2636 KB Output is correct
61 Correct 2 ms 2660 KB Output is correct
62 Correct 2 ms 2636 KB Output is correct
63 Correct 2 ms 2636 KB Output is correct
64 Correct 2 ms 2664 KB Output is correct
65 Correct 322 ms 12664 KB Output is correct
66 Correct 347 ms 12660 KB Output is correct
67 Correct 316 ms 12520 KB Output is correct
68 Correct 306 ms 12628 KB Output is correct
69 Correct 2 ms 2636 KB Output is correct
70 Correct 2 ms 2636 KB Output is correct
71 Correct 3 ms 2636 KB Output is correct
72 Correct 2 ms 2636 KB Output is correct
73 Correct 3 ms 2636 KB Output is correct
74 Correct 2 ms 2636 KB Output is correct
75 Correct 2 ms 2664 KB Output is correct
76 Correct 3 ms 2636 KB Output is correct
77 Correct 2 ms 2636 KB Output is correct
78 Correct 2 ms 2636 KB Output is correct
79 Correct 2 ms 2636 KB Output is correct
80 Correct 2 ms 2636 KB Output is correct
81 Correct 2 ms 2636 KB Output is correct
82 Correct 2 ms 2636 KB Output is correct
83 Correct 2 ms 2636 KB Output is correct