답안 #718179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
718179 2023-04-03T14:39:09 Z amirhoseinfar1385 Love Polygon (BOI18_polygon) C++17
100 / 100
297 ms 28380 KB
    #include<bits/stdc++.h>
    using namespace std;
     
    int main(){
    	ios::sync_with_stdio(0);
    	cin.tie(0);
    	cout.tie(0);
    	int n;
    	cin>>n;
    	map<string,int>mp;
    	vector<int>cnt(n+1);
    	vector<vector<int>>revadj(n+1);
    	vector<pair<string,string>>all(n);
    	for(int i=0;i<n;i++){
    		cin>>all[i].first>>all[i].second;
    		mp[all[i].first]=i;
    	}
    	vector<int>adj(n+1);
    	for(int i=0;i<n;i++){
    		int x=mp[all[i].second];
    		revadj[x].push_back(i);
    		adj[i]=x;
    		cnt[x]++;
    	}
    	if(n&1){
    		cout<<-1<<"\n";
    		return 0;
    	}
    	int res=n;
    	set<pair<int,int>>st;
    	vector<int>visa(n+1);
    	for(int i=0;i<n;i++){
    		if(adj[i]!=i&&adj[adj[i]]==i){
    			res--;
    			visa[i]=1;
    			continue;
    		}
    		st.insert(make_pair(cnt[i],i));
    	}
    	while((int)st.size()>0){
    		auto x=*st.begin();
    		if(x.first>=1){
    			vector<int>vis(n+2);
    			for(int i=0;i<n;i++){
    				if(st.count(make_pair(cnt[i],i))==0){
    					vis[i]=1;
    				}
    			}
    			for(int i=0;i<n;i++){
    				if(vis[i]==0){
    					int cnta=0;
    					int fi=i;
    					while(vis[fi]==0){
    						vis[fi]=1;
    						cnta++;
    					//	cout<<fi<<" "<<adj[fi]<<'\n';
    						fi=adj[fi];
    					}
    					if(cnta==2){
    						res-=cnta;
    						continue;
    					}
    					//cout<<i<<" "<<cnt<<"\n";
    					res-=(cnta)/2;
    				}
    			}
    			break;
    		}
    		st.erase(x);
    		//cout<<x.second<<" "<<adj[x.second]<<'\n';
    		if(adj[x.second]==x.second||visa[adj[x.second]]){
    			continue;
    		}
    		res--;
    		for(auto y:revadj[x.second]){
    			st.erase(make_pair(cnt[y],y));
    		}
    	//	cout<<x.second<<'\n';
    		st.erase(make_pair(cnt[adj[x.second]],adj[x.second]));
    		if(st.count(make_pair(cnt[adj[adj[x.second]]],adj[adj[x.second]]))==1){
    			st.erase(make_pair(cnt[adj[adj[x.second]]],adj[adj[x.second]]));
    			cnt[adj[adj[x.second]]]--;
    			st.insert(make_pair(cnt[adj[adj[x.second]]],adj[adj[x.second]]));
    		}		
    		for(auto y:revadj[adj[x.second]]){
    			st.erase(make_pair(cnt[y],y));
    		}
    	}
    	cout<<res<<'\n';
    }
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 316 KB Output is correct
3 Correct 1 ms 320 KB Output is correct
4 Correct 175 ms 26540 KB Output is correct
5 Correct 175 ms 26096 KB Output is correct
6 Correct 174 ms 26300 KB Output is correct
7 Correct 138 ms 20880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 254 ms 26124 KB Output is correct
2 Correct 238 ms 25728 KB Output is correct
3 Correct 149 ms 26564 KB Output is correct
4 Correct 161 ms 19636 KB Output is correct
5 Correct 229 ms 26244 KB Output is correct
6 Correct 226 ms 24776 KB Output is correct
7 Correct 217 ms 25076 KB Output is correct
8 Correct 184 ms 25484 KB Output is correct
9 Correct 193 ms 24160 KB Output is correct
10 Correct 125 ms 23536 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 292 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 320 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 320 KB Output is correct
19 Correct 175 ms 26540 KB Output is correct
20 Correct 175 ms 26096 KB Output is correct
21 Correct 174 ms 26300 KB Output is correct
22 Correct 138 ms 20880 KB Output is correct
23 Correct 254 ms 26124 KB Output is correct
24 Correct 238 ms 25728 KB Output is correct
25 Correct 149 ms 26564 KB Output is correct
26 Correct 161 ms 19636 KB Output is correct
27 Correct 229 ms 26244 KB Output is correct
28 Correct 226 ms 24776 KB Output is correct
29 Correct 217 ms 25076 KB Output is correct
30 Correct 184 ms 25484 KB Output is correct
31 Correct 193 ms 24160 KB Output is correct
32 Correct 125 ms 23536 KB Output is correct
33 Correct 1 ms 320 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 316 KB Output is correct
37 Correct 216 ms 25912 KB Output is correct
38 Correct 257 ms 24956 KB Output is correct
39 Correct 233 ms 25116 KB Output is correct
40 Correct 227 ms 26344 KB Output is correct
41 Correct 219 ms 26544 KB Output is correct
42 Correct 234 ms 26700 KB Output is correct
43 Correct 233 ms 26780 KB Output is correct
44 Correct 231 ms 26792 KB Output is correct
45 Correct 275 ms 26708 KB Output is correct
46 Correct 297 ms 26832 KB Output is correct
47 Correct 195 ms 25928 KB Output is correct
48 Correct 289 ms 27468 KB Output is correct
49 Correct 238 ms 27440 KB Output is correct
50 Correct 165 ms 28380 KB Output is correct
51 Correct 143 ms 21284 KB Output is correct
52 Correct 249 ms 27912 KB Output is correct
53 Correct 209 ms 26400 KB Output is correct
54 Correct 261 ms 26776 KB Output is correct
55 Correct 280 ms 27012 KB Output is correct
56 Correct 193 ms 25800 KB Output is correct
57 Correct 135 ms 25096 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 316 KB Output is correct
60 Correct 1 ms 212 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 1 ms 320 KB Output is correct
64 Correct 0 ms 316 KB Output is correct
65 Correct 170 ms 28276 KB Output is correct
66 Correct 184 ms 28080 KB Output is correct
67 Correct 201 ms 28280 KB Output is correct
68 Correct 148 ms 22812 KB Output is correct
69 Correct 1 ms 212 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 1 ms 316 KB Output is correct
74 Correct 1 ms 212 KB Output is correct
75 Correct 1 ms 212 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 1 ms 212 KB Output is correct
78 Correct 1 ms 320 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 320 KB Output is correct
81 Correct 0 ms 320 KB Output is correct
82 Correct 0 ms 212 KB Output is correct
83 Correct 1 ms 212 KB Output is correct