#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
int pp[100005];
map<string,int>mp;
int ptr=0;
int sz=0;
bool visited[100005];
int in[100005];
void dfs(int index){
sz++;
visited[index]=true;
int nx=pp[index];
if(visited[nx]) return;
dfs(nx);
}
void solve(){
int n;
cin >> n;
string s,s2;
for(int x=0;x<n;x++){
cin >> s >> s2;
if(mp.find(s)==mp.end()){
mp[s]=ptr++;
}
if(mp.find(s2)==mp.end()){
mp[s2]=ptr++;
}
pp[mp[s]]=mp[s2];
//show2(a,mp[s],b,mp[s2]);
in[mp[s2]]++;
}
for(int x=0;x<n;x++){
if(pp[pp[x]]==x&&pp[x]!=x){
//relationship
visited[x]=true;
visited[pp[x]]=true;
}
}
int counter=0;
queue<int>q;
for(int x=0;x<n;x++){
if(in[x]==0) q.push(x);
}
while(!q.empty()){
int cur=q.front();
q.pop();
if(visited[cur]) continue;
int nx=pp[cur];
//show2(cur,cur,nx,nx);
if(visited[nx]){
pp[cur]=cur;
continue;
}
counter++;
in[pp[nx]]--;
if(in[pp[nx]]==0)q.push(pp[nx]);
pp[nx]=cur;
visited[nx]=true;
visited[cur]=true;
}
for(int x=0;x<n;x++){
if(visited[x]) continue;
//show(visit,x);
sz=0;
dfs(x);
if(sz==2) continue;
counter+=(sz+1)/2;
}
if(n%2) cout << -1;
else cout << counter;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
159 ms |
11228 KB |
Output is correct |
5 |
Correct |
163 ms |
11244 KB |
Output is correct |
6 |
Correct |
170 ms |
11344 KB |
Output is correct |
7 |
Correct |
150 ms |
11404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
11600 KB |
Output is correct |
2 |
Correct |
174 ms |
11284 KB |
Output is correct |
3 |
Correct |
140 ms |
11120 KB |
Output is correct |
4 |
Correct |
156 ms |
11448 KB |
Output is correct |
5 |
Correct |
163 ms |
11088 KB |
Output is correct |
6 |
Correct |
147 ms |
11600 KB |
Output is correct |
7 |
Correct |
184 ms |
11460 KB |
Output is correct |
8 |
Correct |
151 ms |
11456 KB |
Output is correct |
9 |
Correct |
157 ms |
11660 KB |
Output is correct |
10 |
Correct |
117 ms |
11092 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
464 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
159 ms |
11228 KB |
Output is correct |
20 |
Correct |
163 ms |
11244 KB |
Output is correct |
21 |
Correct |
170 ms |
11344 KB |
Output is correct |
22 |
Correct |
150 ms |
11404 KB |
Output is correct |
23 |
Correct |
163 ms |
11600 KB |
Output is correct |
24 |
Correct |
174 ms |
11284 KB |
Output is correct |
25 |
Correct |
140 ms |
11120 KB |
Output is correct |
26 |
Correct |
156 ms |
11448 KB |
Output is correct |
27 |
Correct |
163 ms |
11088 KB |
Output is correct |
28 |
Correct |
147 ms |
11600 KB |
Output is correct |
29 |
Correct |
184 ms |
11460 KB |
Output is correct |
30 |
Correct |
151 ms |
11456 KB |
Output is correct |
31 |
Correct |
157 ms |
11660 KB |
Output is correct |
32 |
Correct |
117 ms |
11092 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
176 ms |
11260 KB |
Output is correct |
38 |
Correct |
173 ms |
11344 KB |
Output is correct |
39 |
Correct |
160 ms |
11780 KB |
Output is correct |
40 |
Correct |
154 ms |
11396 KB |
Output is correct |
41 |
Correct |
206 ms |
11596 KB |
Output is correct |
42 |
Correct |
157 ms |
11348 KB |
Output is correct |
43 |
Correct |
163 ms |
11340 KB |
Output is correct |
44 |
Correct |
189 ms |
11536 KB |
Output is correct |
45 |
Correct |
150 ms |
11516 KB |
Output is correct |
46 |
Correct |
213 ms |
11296 KB |
Output is correct |
47 |
Correct |
152 ms |
11952 KB |
Output is correct |
48 |
Correct |
163 ms |
11396 KB |
Output is correct |
49 |
Correct |
163 ms |
11088 KB |
Output is correct |
50 |
Correct |
140 ms |
11176 KB |
Output is correct |
51 |
Correct |
150 ms |
11600 KB |
Output is correct |
52 |
Correct |
160 ms |
11092 KB |
Output is correct |
53 |
Correct |
165 ms |
11380 KB |
Output is correct |
54 |
Correct |
147 ms |
11592 KB |
Output is correct |
55 |
Correct |
161 ms |
11276 KB |
Output is correct |
56 |
Correct |
139 ms |
11740 KB |
Output is correct |
57 |
Correct |
118 ms |
11272 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
1 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1 ms |
344 KB |
Output is correct |
62 |
Correct |
1 ms |
348 KB |
Output is correct |
63 |
Correct |
0 ms |
348 KB |
Output is correct |
64 |
Correct |
1 ms |
348 KB |
Output is correct |
65 |
Correct |
174 ms |
11156 KB |
Output is correct |
66 |
Correct |
170 ms |
11088 KB |
Output is correct |
67 |
Correct |
149 ms |
10072 KB |
Output is correct |
68 |
Correct |
147 ms |
9812 KB |
Output is correct |
69 |
Correct |
1 ms |
348 KB |
Output is correct |
70 |
Correct |
1 ms |
348 KB |
Output is correct |
71 |
Correct |
0 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
344 KB |
Output is correct |
73 |
Correct |
0 ms |
344 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
412 KB |
Output is correct |
81 |
Correct |
0 ms |
348 KB |
Output is correct |
82 |
Correct |
0 ms |
348 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |