이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define per(i, a, b) for (int i = b - 1; i >= a; --i)
#define trav(a, x) for (auto& a : x)
#define sz(a) a.size()
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
unordered_map<string, string> people;
unordered_set<string> vis;
int si;
void dfs(string person){
if(vis.count(person))return;
vis.insert(person);
++si;
dfs(people[person]);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
if (n & 1) {
cout << -1 << endl;
return 0;
}
int ans=0;
string n1, n2;
rep(i, 0, n) {
cin >> n1 >> n2;
people.emplace(n1, n2);
if (n1!=n2&&people.count(n2) && people[n2] == n1) {
people.erase(n1);
people.erase(n2);
}
}
trav(person,people){
si=0;
dfs(person.first);
ans+=(si+1)>>1;
}
cout<<ans<<endl;
}
# | 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... |