#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 |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
235 ms |
9764 KB |
Output is correct |
5 |
Correct |
215 ms |
9668 KB |
Output is correct |
6 |
Correct |
217 ms |
9776 KB |
Output is correct |
7 |
Correct |
228 ms |
9796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
10080 KB |
Output is correct |
2 |
Correct |
225 ms |
10052 KB |
Output is correct |
3 |
Correct |
177 ms |
9752 KB |
Output is correct |
4 |
Correct |
207 ms |
9668 KB |
Output is correct |
5 |
Correct |
219 ms |
9764 KB |
Output is correct |
6 |
Correct |
222 ms |
10540 KB |
Output is correct |
7 |
Correct |
214 ms |
10380 KB |
Output is correct |
8 |
Correct |
216 ms |
10444 KB |
Output is correct |
9 |
Correct |
209 ms |
10868 KB |
Output is correct |
10 |
Correct |
169 ms |
10104 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
235 ms |
9764 KB |
Output is correct |
20 |
Correct |
215 ms |
9668 KB |
Output is correct |
21 |
Correct |
217 ms |
9776 KB |
Output is correct |
22 |
Correct |
228 ms |
9796 KB |
Output is correct |
23 |
Correct |
229 ms |
10080 KB |
Output is correct |
24 |
Correct |
225 ms |
10052 KB |
Output is correct |
25 |
Correct |
177 ms |
9752 KB |
Output is correct |
26 |
Correct |
207 ms |
9668 KB |
Output is correct |
27 |
Correct |
219 ms |
9764 KB |
Output is correct |
28 |
Correct |
222 ms |
10540 KB |
Output is correct |
29 |
Correct |
214 ms |
10380 KB |
Output is correct |
30 |
Correct |
216 ms |
10444 KB |
Output is correct |
31 |
Correct |
209 ms |
10868 KB |
Output is correct |
32 |
Correct |
169 ms |
10104 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
0 ms |
204 KB |
Output is correct |
35 |
Correct |
0 ms |
204 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
233 ms |
12072 KB |
Output is correct |
38 |
Correct |
221 ms |
12544 KB |
Output is correct |
39 |
Correct |
218 ms |
12488 KB |
Output is correct |
40 |
Correct |
224 ms |
12524 KB |
Output is correct |
41 |
Correct |
228 ms |
12616 KB |
Output is correct |
42 |
Correct |
214 ms |
12580 KB |
Output is correct |
43 |
Correct |
225 ms |
12584 KB |
Output is correct |
44 |
Correct |
223 ms |
12632 KB |
Output is correct |
45 |
Correct |
218 ms |
12520 KB |
Output is correct |
46 |
Correct |
216 ms |
12484 KB |
Output is correct |
47 |
Correct |
214 ms |
13120 KB |
Output is correct |
48 |
Correct |
231 ms |
12336 KB |
Output is correct |
49 |
Correct |
224 ms |
12100 KB |
Output is correct |
50 |
Correct |
176 ms |
11936 KB |
Output is correct |
51 |
Correct |
211 ms |
11780 KB |
Output is correct |
52 |
Correct |
221 ms |
11936 KB |
Output is correct |
53 |
Correct |
223 ms |
12484 KB |
Output is correct |
54 |
Correct |
233 ms |
12508 KB |
Output is correct |
55 |
Correct |
202 ms |
12580 KB |
Output is correct |
56 |
Correct |
206 ms |
13048 KB |
Output is correct |
57 |
Correct |
167 ms |
12356 KB |
Output is correct |
58 |
Correct |
0 ms |
332 KB |
Output is correct |
59 |
Correct |
1 ms |
204 KB |
Output is correct |
60 |
Correct |
0 ms |
204 KB |
Output is correct |
61 |
Correct |
1 ms |
332 KB |
Output is correct |
62 |
Correct |
1 ms |
332 KB |
Output is correct |
63 |
Correct |
0 ms |
204 KB |
Output is correct |
64 |
Correct |
1 ms |
204 KB |
Output is correct |
65 |
Correct |
229 ms |
11928 KB |
Output is correct |
66 |
Correct |
234 ms |
11864 KB |
Output is correct |
67 |
Correct |
223 ms |
11936 KB |
Output is correct |
68 |
Correct |
226 ms |
11736 KB |
Output is correct |
69 |
Correct |
1 ms |
204 KB |
Output is correct |
70 |
Correct |
0 ms |
332 KB |
Output is correct |
71 |
Correct |
0 ms |
204 KB |
Output is correct |
72 |
Correct |
1 ms |
204 KB |
Output is correct |
73 |
Correct |
1 ms |
204 KB |
Output is correct |
74 |
Correct |
0 ms |
204 KB |
Output is correct |
75 |
Correct |
0 ms |
204 KB |
Output is correct |
76 |
Correct |
0 ms |
332 KB |
Output is correct |
77 |
Correct |
0 ms |
204 KB |
Output is correct |
78 |
Correct |
0 ms |
204 KB |
Output is correct |
79 |
Correct |
1 ms |
204 KB |
Output is correct |
80 |
Correct |
0 ms |
204 KB |
Output is correct |
81 |
Correct |
1 ms |
204 KB |
Output is correct |
82 |
Correct |
1 ms |
332 KB |
Output is correct |
83 |
Correct |
1 ms |
316 KB |
Output is correct |