/*
Note :
Link :
Boka hoye gelum...
*/
#include "bits/stdc++.h"
#define sp <<" "
#define el <<"\n"
#define S second
#define F first
#define pb push_back
#define all(ar) ar.begin(),ar.end()
#define pii pair<ll,ll>
using namespace std;
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll oo = 3372036000000000;
const ll si = 100005;
int cru[si], phead = -1,tin[si];
bool done[si], pol[si],vis[si];
map<string,int> rec;
vector<int> tps;
void dfs(int v,int tim){
if(tin[v] == tim){
phead = v;
return;
}
tin[v] = tim;
if(cru[v] != v && (tin[cru[v]] == -1 || tin[cru[v]] == tim)) dfs(cru[v], tim);
if(phead != -1) pol[v] = 1;
if(phead == v) phead = -1;
return;
}
void top_sort(int v){
vis[v] = 1;
if(!vis[cru[v]]) top_sort(cru[v]);
tps.pb(v);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
/*
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
//*/
int n,cur = 0; cin>>n;
memset(done,false,sizeof(done));
for(int i = 0; i < n; i++){
string a,b; cin>>a>>b;
if(!rec.count(a)) rec[a] = cur++;
if(!rec.count(b)) rec[b] = cur++;
cru[rec[a]] = rec[b];
}
if(n % 2){
cout<<-1 el; return 0;
}
for(int i = 0; i < n; i++){
int des = cru[i];
if(i != des && cru[des] == i){
done[i] = done[des] = 1;
}
}
memset(vis,0,sizeof(vis));
for(int i = 0; i < n; i++) if(!vis[i] && !done[i]) top_sort(i);
reverse(all(tps));
memset(tin,-1,sizeof(tin));
memset(pol,0,sizeof(pol));
int ans = 0;
for(int i: tps) if(!done[i] && tin[i] == -1) dfs(i,i);
for(int i: tps) if(!done[i] && cru[i] != i && !pol[i] && !done[cru[i]]){
done[i] = done[cru[i]] = 1;
ans += 1;
}
for(int i: tps) if(!done[i] && cru[i] != i && !done[cru[i]]){
done[i] = done[cru[i]] = 1;
ans += 1;
}
for(int i: tps) if(!done[i]) ans += 1;
cout<<ans el;
return 0;
}
/*
check for:
*missed a break, continue ?
*corner cases, n = 1?
*overflow.
*unnecessary logics.
*impreciseness caused by double or long double.
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
984 KB |
Output is correct |
3 |
Correct |
1 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
976 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
976 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
179 ms |
14560 KB |
Output is correct |
5 |
Correct |
187 ms |
11880 KB |
Output is correct |
6 |
Correct |
228 ms |
14936 KB |
Output is correct |
7 |
Correct |
174 ms |
10740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
11796 KB |
Output is correct |
2 |
Correct |
197 ms |
12368 KB |
Output is correct |
3 |
Correct |
146 ms |
11944 KB |
Output is correct |
4 |
Correct |
182 ms |
10708 KB |
Output is correct |
5 |
Correct |
247 ms |
14748 KB |
Output is correct |
6 |
Correct |
187 ms |
11864 KB |
Output is correct |
7 |
Correct |
189 ms |
11840 KB |
Output is correct |
8 |
Correct |
219 ms |
11808 KB |
Output is correct |
9 |
Correct |
184 ms |
11844 KB |
Output is correct |
10 |
Correct |
143 ms |
11900 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
984 KB |
Output is correct |
3 |
Correct |
1 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
1 ms |
976 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
976 KB |
Output is correct |
16 |
Correct |
1 ms |
976 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
179 ms |
14560 KB |
Output is correct |
20 |
Correct |
187 ms |
11880 KB |
Output is correct |
21 |
Correct |
228 ms |
14936 KB |
Output is correct |
22 |
Correct |
174 ms |
10740 KB |
Output is correct |
23 |
Correct |
193 ms |
11796 KB |
Output is correct |
24 |
Correct |
197 ms |
12368 KB |
Output is correct |
25 |
Correct |
146 ms |
11944 KB |
Output is correct |
26 |
Correct |
182 ms |
10708 KB |
Output is correct |
27 |
Correct |
247 ms |
14748 KB |
Output is correct |
28 |
Correct |
187 ms |
11864 KB |
Output is correct |
29 |
Correct |
189 ms |
11840 KB |
Output is correct |
30 |
Correct |
219 ms |
11808 KB |
Output is correct |
31 |
Correct |
184 ms |
11844 KB |
Output is correct |
32 |
Correct |
143 ms |
11900 KB |
Output is correct |
33 |
Correct |
1 ms |
980 KB |
Output is correct |
34 |
Correct |
1 ms |
980 KB |
Output is correct |
35 |
Correct |
1 ms |
972 KB |
Output is correct |
36 |
Correct |
1 ms |
980 KB |
Output is correct |
37 |
Correct |
204 ms |
11892 KB |
Output is correct |
38 |
Incorrect |
209 ms |
12376 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |