This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |