# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
904837 | simona1230 | Love Polygon (BOI18_polygon) | C++17 | 2082 ms | 11064 KiB |
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,names=1;
map<string,int> mp;
int nxt[100001],in[100001];
void read()
{
cin>>n;
for(int i=1; i<=n; i++)
{
string a,b;
cin>>a>>b;
if(!mp[a])mp[a]=names++;
if(!mp[b])mp[b]=names++;
//cout<<mp[a]<<" "<<mp[b]<<'\n';
nxt[mp[a]]=mp[b];
in[mp[b]]++;
if(a==b)nxt[mp[a]]=0;
}
if(n%2==1)
{
cout<<"-1"<<endl;
exit(0);
}
}
int used[100001];
queue<int> q;
void solve()
{
for(int i=1;i<=n;i++)
{
if(in[i]==0)
q.push(i);
}
int cnt=0,ans=0;
while(q.size())
{
int ver=q.front();
q.pop();
int a=nxt[ver];
int b=nxt[a];
if(nxt[b]==a)continue;
in[b]--;
if(in[b]==0)q.push(b);
nxt[a]=ver;
in[ver]++;
ver=b;
ans++;
}
for(int i=1; i<=n; i++)
{
if(in[i]==0)
{
ans++;
continue;
}
if(used[i]||nxt[nxt[i]]==i)continue;
int len=1,ver=i;
while(nxt[ver]!=i)
{
len++;
ver=nxt[ver];
used[ver]=1;
}
ans+=len/2+len%2;
}
cout<<ans<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
read();
solve();
return 0;
}
Compilation message (stderr)
# | 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... |