이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
map <string,pair<string,int> > love;
string s[100005];
int in[100005];
bool ok[100005];
int main(){
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i=1;i<=n;i++){
string t;
cin >> s[i] >> t;
love[s[i]]={t,i};
}
for (int i=1;i<=n;i++){
if (love[love[s[i]].first].first == s[i] && s[i] != love[s[i]].first) ok[i]=ok[love[love[s[i]].first].second]=true;
else in[love[love[s[i]].first].second]++;
}
int ans=0;
for (int i=1;i<=n;i++){
if (in[i] == 0 && !ok[i] && !ok[love[love[s[i]].first].second]){
ans++,ok[i]=ok[love[love[s[i]].first].second]=true;
string now=love[love[s[i]].first].first;
in[love[now].second]--;
while (in[love[now].second] == 0){
if (now != love[now].first && !ok[love[now].second] && !ok[love[love[now].first].second]){
ok[love[now].second]=ok[love[love[now].first].second]=true;
ans++;
now=love[love[now].first].first;
in[love[now].second]--;
}
else
break;
}
}
}
for (int i=1;i<=n;i++){
if (s[i] != love[s[i]].first && !ok[i] && !ok[love[love[s[i]].first].second]){
ans++,ok[i]=ok[love[love[s[i]].first].second]=true;
string now=love[love[s[i]].first].first;
in[love[now].second]--;
while (in[love[now].second] == 0){
if (now != love[now].first && !ok[love[now].second] && !ok[love[love[now].first].second]){
ok[love[now].second]=ok[love[love[now].first].second]=true;
ans++;
now=love[love[now].first].first;
in[love[now].second]--;
}
else
break;
}
}
}
for (int i=1;i<=n;i++){
if (!ok[i]) ans++;
}
if (n%2 == 1) cout << "-1\n";
else cout << ans << "\n";
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... |