#include<bits/stdc++.h>
using namespace std;
int n,a,b,il;
char wcz;
int pol[100001],lvl[100001],odp;
bool spar[100001];
queue<int>kol;
string s1,s2;
unordered_map<string,int>skal;
int main()
{
cin>>n;
ios_base::sync_with_stdio(0);
if(n%2==1)
{
printf("-1");
return 0;
}
for(int i=n;i;--i)
{
cin>>s1>>s2;
if(skal[s1])
a=skal[s1];
else
{
++il;
skal[s1]=il;
a=il;
}
if(skal[s2])
b=skal[s2];
else
{
++il;
skal[s2]=il;
b=il;
}
pol[a]=b;
++lvl[b];
}
for(int i=1;i<=il;++i)
{
if(lvl[i]==0)
kol.push(i);
}
while(!kol.empty())
{
a=kol.front();
kol.pop();
if(pol[a]==a||spar[a])
continue;
if(spar[pol[a]]==0)
{
spar[pol[a]]=1;
spar[a]=1;
++odp;
lvl[pol[a]]=0;
--lvl[pol[pol[a]]];
if(lvl[pol[pol[a]]]==0)
kol.push(pol[pol[a]]);
}
else
{
++odp;
spar[a]=1;
}
--lvl[pol[a]];
if(lvl[pol[a]]==0)
kol.push(pol[a]);
}
for(int i=1;i<=il;++i)
{
if(spar[i]==0)
{
int ter=pol[i];
int wielk=1;
spar[i]=1;
while(ter!=i)
{
spar[ter]=1;
ter=pol[ter];
++wielk;
}
if(wielk!=2)
odp+=wielk/2+(wielk%2);
}
}
cout<<odp;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
88 ms |
10952 KB |
Output is correct |
5 |
Correct |
60 ms |
10888 KB |
Output is correct |
6 |
Correct |
61 ms |
10880 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
11008 KB |
Output is correct |
2 |
Correct |
88 ms |
11020 KB |
Output is correct |
3 |
Correct |
46 ms |
10944 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
67 ms |
10940 KB |
Output is correct |
6 |
Correct |
62 ms |
11088 KB |
Output is correct |
7 |
Correct |
67 ms |
11056 KB |
Output is correct |
8 |
Correct |
77 ms |
11104 KB |
Output is correct |
9 |
Correct |
69 ms |
11212 KB |
Output is correct |
10 |
Correct |
55 ms |
10988 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |