#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5;
int n, t, vs[nx], l[nx], res, cnt;
string su[nx], sv[nx];
map<string, int> mp;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>su[i]>>sv[i], mp[su[i]]=mp[sv[i]]=0;
if (n%2) return cout<<-1, 0;
for (auto &[x, y]:mp) y=++t;
for (int i=1; i<=n; i++) l[mp[su[i]]]=mp[sv[i]];
for (int i=1; i<=n; i++)
{
if (vs[i]) continue;
int sz=1, tmp=i;
vs[i]=1;
while (l[tmp]!=i) sz++, tmp=l[tmp], vs[tmp]=1;
if (sz==2) continue;
res+=sz/2;
if (sz%2) cnt++;
}
res+=cnt;
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2047 ms |
7000 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
139 ms |
17464 KB |
Output is correct |
5 |
Correct |
137 ms |
17236 KB |
Output is correct |
6 |
Correct |
141 ms |
17236 KB |
Output is correct |
7 |
Correct |
81 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2009 ms |
14932 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2047 ms |
7000 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |