#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N=100005;
map<string,int> m;
vector<int> vis(N);
vector<int> v(N,-1),in(N),inq(N);
inline int id(string s){
if(m.count(s)){
return m[s];
}
int p=m.size();
//cout<<s<<' '<<p<<endl;
m[s]=p;
return p;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
if(n&1){
cout<<-1<<endl;
return 0;
}
for(int i=0;i<n;i++){
string a,b;cin>>a>>b;
v[id(a)]=id(b);
in[id(b)]++;
if(v[id(b)]==id(a)&&a!=b){
vis[id(a)]=vis[id(b)]=1;
}
}
int ans=0;
queue<int> q;
for(int i=0;i<n;i++){
if(in[i]==0){
q.push(i);
}
}
while(q.size()){
int p=q.front();
q.pop();
inq[p]++;
//cout<<p<<endl;
in[v[p]]--;
if(in[v[p]]==0){
q.push(v[p]);
}
if(vis[p]){
continue;
}
ans++;
vis[p]=vis[v[p]]=1;
}
for(int i=0;i<n;i++){
if(vis[i])continue;
int sz=0,f=v[i];
vector<int> a(1,vis[i]);
vis[i]=1;
while(f!=i){
a.push_back(vis[f]);
vis[f]=1;
f=v[f];
}
int idx=0;
for(int i=0;i<a.size();i++){
if(a[i]==1)idx=(i+1)%(int)a.size();
}
for(int i=0;i<a.size();i++){
//cout<<a[idx]<<endl;
if(a[idx]==0)sz++;
else ans+=(sz+1)/2,sz=0;
idx++;
idx%=(int)a.size();
}
//assert(sz==1);
ans+=(sz+1)/2;
}
cout<<ans<<endl;
return 0;
}
Compilation message
polygon.cpp: In function 'int32_t main()':
polygon.cpp:66:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
polygon.cpp:69:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for(int i=0;i<a.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
203 ms |
10348 KB |
Output is correct |
5 |
Correct |
196 ms |
9808 KB |
Output is correct |
6 |
Correct |
194 ms |
10452 KB |
Output is correct |
7 |
Correct |
1 ms |
1880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
202 ms |
9952 KB |
Output is correct |
2 |
Correct |
198 ms |
9896 KB |
Output is correct |
3 |
Correct |
171 ms |
9792 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
209 ms |
9976 KB |
Output is correct |
6 |
Correct |
190 ms |
9840 KB |
Output is correct |
7 |
Correct |
190 ms |
9812 KB |
Output is correct |
8 |
Correct |
194 ms |
9816 KB |
Output is correct |
9 |
Correct |
185 ms |
10064 KB |
Output is correct |
10 |
Correct |
140 ms |
10064 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
2 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1880 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1880 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
1884 KB |
Output is correct |
12 |
Correct |
1 ms |
1884 KB |
Output is correct |
13 |
Correct |
1 ms |
1884 KB |
Output is correct |
14 |
Correct |
1 ms |
1884 KB |
Output is correct |
15 |
Correct |
1 ms |
1880 KB |
Output is correct |
16 |
Correct |
1 ms |
1884 KB |
Output is correct |
17 |
Correct |
1 ms |
1884 KB |
Output is correct |
18 |
Correct |
1 ms |
1884 KB |
Output is correct |
19 |
Correct |
203 ms |
10348 KB |
Output is correct |
20 |
Correct |
196 ms |
9808 KB |
Output is correct |
21 |
Correct |
194 ms |
10452 KB |
Output is correct |
22 |
Correct |
1 ms |
1880 KB |
Output is correct |
23 |
Correct |
202 ms |
9952 KB |
Output is correct |
24 |
Correct |
198 ms |
9896 KB |
Output is correct |
25 |
Correct |
171 ms |
9792 KB |
Output is correct |
26 |
Correct |
1 ms |
1884 KB |
Output is correct |
27 |
Correct |
209 ms |
9976 KB |
Output is correct |
28 |
Correct |
190 ms |
9840 KB |
Output is correct |
29 |
Correct |
190 ms |
9812 KB |
Output is correct |
30 |
Correct |
194 ms |
9816 KB |
Output is correct |
31 |
Correct |
185 ms |
10064 KB |
Output is correct |
32 |
Correct |
140 ms |
10064 KB |
Output is correct |
33 |
Correct |
1 ms |
1884 KB |
Output is correct |
34 |
Correct |
1 ms |
1884 KB |
Output is correct |
35 |
Correct |
1 ms |
1884 KB |
Output is correct |
36 |
Correct |
2 ms |
1884 KB |
Output is correct |
37 |
Correct |
213 ms |
11860 KB |
Output is correct |
38 |
Correct |
197 ms |
12116 KB |
Output is correct |
39 |
Correct |
201 ms |
12168 KB |
Output is correct |
40 |
Correct |
205 ms |
12052 KB |
Output is correct |
41 |
Correct |
212 ms |
12016 KB |
Output is correct |
42 |
Correct |
199 ms |
12396 KB |
Output is correct |
43 |
Correct |
192 ms |
12000 KB |
Output is correct |
44 |
Correct |
215 ms |
11960 KB |
Output is correct |
45 |
Correct |
207 ms |
12356 KB |
Output is correct |
46 |
Correct |
219 ms |
11960 KB |
Output is correct |
47 |
Correct |
198 ms |
12116 KB |
Output is correct |
48 |
Correct |
222 ms |
11904 KB |
Output is correct |
49 |
Correct |
219 ms |
11972 KB |
Output is correct |
50 |
Correct |
176 ms |
11816 KB |
Output is correct |
51 |
Correct |
1 ms |
1884 KB |
Output is correct |
52 |
Correct |
205 ms |
11944 KB |
Output is correct |
53 |
Correct |
200 ms |
12112 KB |
Output is correct |
54 |
Correct |
221 ms |
12124 KB |
Output is correct |
55 |
Correct |
217 ms |
12072 KB |
Output is correct |
56 |
Correct |
197 ms |
12244 KB |
Output is correct |
57 |
Correct |
154 ms |
12316 KB |
Output is correct |
58 |
Correct |
1 ms |
1884 KB |
Output is correct |
59 |
Correct |
1 ms |
1884 KB |
Output is correct |
60 |
Correct |
1 ms |
1884 KB |
Output is correct |
61 |
Correct |
1 ms |
1884 KB |
Output is correct |
62 |
Correct |
1 ms |
1884 KB |
Output is correct |
63 |
Correct |
2 ms |
2040 KB |
Output is correct |
64 |
Correct |
1 ms |
1884 KB |
Output is correct |
65 |
Correct |
213 ms |
12640 KB |
Output is correct |
66 |
Correct |
210 ms |
11924 KB |
Output is correct |
67 |
Correct |
236 ms |
12568 KB |
Output is correct |
68 |
Correct |
1 ms |
1884 KB |
Output is correct |
69 |
Correct |
1 ms |
1884 KB |
Output is correct |
70 |
Correct |
1 ms |
1884 KB |
Output is correct |
71 |
Correct |
1 ms |
1884 KB |
Output is correct |
72 |
Correct |
1 ms |
1924 KB |
Output is correct |
73 |
Correct |
1 ms |
1884 KB |
Output is correct |
74 |
Correct |
1 ms |
1884 KB |
Output is correct |
75 |
Correct |
1 ms |
1884 KB |
Output is correct |
76 |
Correct |
1 ms |
1884 KB |
Output is correct |
77 |
Correct |
1 ms |
1884 KB |
Output is correct |
78 |
Correct |
1 ms |
1884 KB |
Output is correct |
79 |
Correct |
1 ms |
1884 KB |
Output is correct |
80 |
Correct |
1 ms |
1884 KB |
Output is correct |
81 |
Correct |
1 ms |
1884 KB |
Output is correct |
82 |
Correct |
2 ms |
1884 KB |
Output is correct |
83 |
Correct |
1 ms |
1884 KB |
Output is correct |