이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int ans = 0;
int cnt = 0;
vector<int> loves(n);
map<string, int> mp;
for(int i = 0; i < n; i++) {
string f, s;
cin >> f >> s;
if(mp.find(f) == mp.end()) {
mp[f] = cnt;
cnt++;
}
if(mp.find(s) == mp.end()) {
mp[s] = cnt;
cnt++;
}
loves[mp[f]] = mp[s];
}
/*for(auto c : loves) {
cout << c << ' ';
}
cout << '\n';*/
if(n % 2 == 0) {
vector<int> ok(n);
for(int i = 0; i < n; i++) {
if(loves[loves[i]] == i && loves[i] != i) {
ok[i] = true;
ok[loves[i]] = true;
}
}
/*for(auto c : ok) {
cout << c << ' ';
}
cout << '\n';*/
for(int i = 0; i < n; i++) {
//cout << i << " is not ok at the moment!\n";
if(!ok[i]) {
if(!ok[loves[i]] && loves[i] != i) {
cout << i << '\n';
loves[loves[i]] = i;
ans++;
ok[i] = true;
ok[loves[i]] = true;
continue;
}
int loves_curr = -1;
for(int j = 0; j < n; j++) {
if(loves[j] == i && loves[j] != i && j != i) {
loves_curr = j;
break;
}
}
if(loves_curr == -1) {
int first_not_ok = -1;
for(int j = 0; j < n; j++) {
if(!ok[j] && j != i) {
first_not_ok = j;
break;
}
}
loves[i] = first_not_ok;
loves[first_not_ok] = i;
ans += 2;
ok[i] = true;
ok[first_not_ok] = true;
}
else {
loves[i] = loves_curr;
ans++;
ok[i] = true;
ok[loves_curr] = true;
}
}
}
/*for(auto c : ok) {
cout << c << ' ';
}
cout << '\n';*/
}
else {
ans = -1;
}
/*for(auto c : loves) {
cout << c << ' ';
}
cout << '\n';*/
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... |