/*
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], psi = 0;
bool done[si], pol[si],vis[si];
map<string,int> rec;
vector<int> tps;
int ans = 0;
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;
psi++;
}
if(phead == v){
phead = -1;
if(psi % 2 == 0){
ans += psi/2;
bool xe = 1;
for(int r = v; r != v || xe ; r = cru[r]){
done[r] = 1;
xe = 0;
}
}
psi = 0;
}
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));
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 |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
944 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
964 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
187 ms |
14648 KB |
Output is correct |
5 |
Correct |
232 ms |
11756 KB |
Output is correct |
6 |
Correct |
171 ms |
15016 KB |
Output is correct |
7 |
Correct |
191 ms |
10824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
11776 KB |
Output is correct |
2 |
Correct |
170 ms |
12420 KB |
Output is correct |
3 |
Correct |
165 ms |
11892 KB |
Output is correct |
4 |
Correct |
171 ms |
10660 KB |
Output is correct |
5 |
Correct |
182 ms |
14676 KB |
Output is correct |
6 |
Correct |
185 ms |
11812 KB |
Output is correct |
7 |
Correct |
176 ms |
12060 KB |
Output is correct |
8 |
Correct |
177 ms |
11840 KB |
Output is correct |
9 |
Correct |
164 ms |
11832 KB |
Output is correct |
10 |
Correct |
129 ms |
11788 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 |
980 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 |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
976 KB |
Output is correct |
4 |
Correct |
1 ms |
944 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |