#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = 2147483647;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 100010;
int n;
map<string, int> mp;
int edge[MAX];
int indeg[MAX];
bool exist[MAX];
vi qu;
int res;
int query(string str){
if(mp.find(str) == mp.end()){
mp[str] = szof(mp) + 1;
}
return mp[str];
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
FOR(i, 1, n, 1){
string su, sv;
cin>>su>>sv;
int u = query(su);
int v = query(sv);
edge[u] = v;
indeg[v]++;
}
if(n%2 == 1){
cout<<-1<<'\n';
return 0;
}
FOR(i, 1, n, 1){
exist[i] = 1;
if(indeg[i] == 0){
qu.pb(i);
}
}
res = n;
FOR(i, 1, n, 1){
if(exist[i]==0) continue;
if(edge[edge[i]] == i and edge[i] != i){
res -= 2;
exist[i] = 0;
exist[edge[i]] = 0;
indeg[i]--;
indeg[edge[i]]--;
}
}
while(!qu.empty()){
int u = qu.back();
qu.pop_back();
if(exist[u]==0) continue;
int v = edge[u];
if(exist[v]==0) continue;
int w = edge[v];
exist[u] = 0;
exist[v] = 0;
indeg[v]--;
indeg[w]--;
res--;
if(indeg[w] == 0 and exist[w] == 1) qu.pb(w);
}
FOR(i, 1, n, 1){
if(exist[i]==0) continue;
int ptr = i;
int cnt = 0;
while(exist[ptr] == 1){
exist[ptr] = 0;
ptr = edge[ptr];
cnt++;
}
if(ptr == i){
if(cnt == 2){
res -= 2;
}
else{
res -= cnt / 2;
}
}
}
cout<<res<<'\n';
/*
FOR(i, 1, n, 1){
cerr<<exist[i]<<" ";
}
cerr<<endl;
*/
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
212 ms |
11192 KB |
Output is correct |
5 |
Correct |
212 ms |
11200 KB |
Output is correct |
6 |
Correct |
207 ms |
11216 KB |
Output is correct |
7 |
Correct |
214 ms |
11304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
11640 KB |
Output is correct |
2 |
Correct |
214 ms |
11456 KB |
Output is correct |
3 |
Correct |
176 ms |
11332 KB |
Output is correct |
4 |
Correct |
199 ms |
11204 KB |
Output is correct |
5 |
Correct |
208 ms |
11320 KB |
Output is correct |
6 |
Correct |
211 ms |
11840 KB |
Output is correct |
7 |
Correct |
202 ms |
11920 KB |
Output is correct |
8 |
Correct |
201 ms |
11952 KB |
Output is correct |
9 |
Correct |
200 ms |
12392 KB |
Output is correct |
10 |
Correct |
160 ms |
11564 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
212 ms |
11192 KB |
Output is correct |
20 |
Correct |
212 ms |
11200 KB |
Output is correct |
21 |
Correct |
207 ms |
11216 KB |
Output is correct |
22 |
Correct |
214 ms |
11304 KB |
Output is correct |
23 |
Correct |
209 ms |
11640 KB |
Output is correct |
24 |
Correct |
214 ms |
11456 KB |
Output is correct |
25 |
Correct |
176 ms |
11332 KB |
Output is correct |
26 |
Correct |
199 ms |
11204 KB |
Output is correct |
27 |
Correct |
208 ms |
11320 KB |
Output is correct |
28 |
Correct |
211 ms |
11840 KB |
Output is correct |
29 |
Correct |
202 ms |
11920 KB |
Output is correct |
30 |
Correct |
201 ms |
11952 KB |
Output is correct |
31 |
Correct |
200 ms |
12392 KB |
Output is correct |
32 |
Correct |
160 ms |
11564 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
256 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
209 ms |
11444 KB |
Output is correct |
38 |
Correct |
215 ms |
11992 KB |
Output is correct |
39 |
Correct |
199 ms |
11944 KB |
Output is correct |
40 |
Correct |
212 ms |
11888 KB |
Output is correct |
41 |
Correct |
209 ms |
11844 KB |
Output is correct |
42 |
Correct |
203 ms |
11980 KB |
Output is correct |
43 |
Correct |
215 ms |
11976 KB |
Output is correct |
44 |
Correct |
208 ms |
11952 KB |
Output is correct |
45 |
Correct |
203 ms |
11940 KB |
Output is correct |
46 |
Correct |
202 ms |
11932 KB |
Output is correct |
47 |
Correct |
194 ms |
12460 KB |
Output is correct |
48 |
Correct |
212 ms |
11708 KB |
Output is correct |
49 |
Correct |
205 ms |
11460 KB |
Output is correct |
50 |
Correct |
176 ms |
11336 KB |
Output is correct |
51 |
Correct |
217 ms |
11168 KB |
Output is correct |
52 |
Correct |
206 ms |
11232 KB |
Output is correct |
53 |
Correct |
205 ms |
11900 KB |
Output is correct |
54 |
Correct |
210 ms |
11904 KB |
Output is correct |
55 |
Correct |
195 ms |
11976 KB |
Output is correct |
56 |
Correct |
189 ms |
12392 KB |
Output is correct |
57 |
Correct |
157 ms |
11588 KB |
Output is correct |
58 |
Correct |
0 ms |
324 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
1 ms |
320 KB |
Output is correct |
62 |
Correct |
1 ms |
204 KB |
Output is correct |
63 |
Correct |
1 ms |
332 KB |
Output is correct |
64 |
Correct |
0 ms |
204 KB |
Output is correct |
65 |
Correct |
202 ms |
11248 KB |
Output is correct |
66 |
Correct |
202 ms |
11236 KB |
Output is correct |
67 |
Correct |
213 ms |
11284 KB |
Output is correct |
68 |
Correct |
203 ms |
11224 KB |
Output is correct |
69 |
Correct |
1 ms |
204 KB |
Output is correct |
70 |
Correct |
1 ms |
332 KB |
Output is correct |
71 |
Correct |
1 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
332 KB |
Output is correct |
73 |
Correct |
1 ms |
204 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
204 KB |
Output is correct |
76 |
Correct |
1 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
1 ms |
204 KB |
Output is correct |
81 |
Correct |
1 ms |
316 KB |
Output is correct |
82 |
Correct |
1 ms |
204 KB |
Output is correct |
83 |
Correct |
1 ms |
204 KB |
Output is correct |