#pragma GCC optimize("Ofast,unroll-loops")
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef int ll;
typedef long double ld;
typedef pair <ll, ll> pll;
#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1); cout.flush();
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< (x) << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()
cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;
template <class A,class B> ostream& operator << (ostream& out,const pair<A,B>&a){return out<<'('<<a.first<<", "<<a.second<<')';}
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
out<< '['; for (int i = -1; ++i < int(a.size());) out<< a[i] << (i + 1 < int(a.size()) ? ", " : ""); return out<<']'; }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;
cl N = 1e5 + 7;
ll P [N], n, ans, cnt, lst;
bool mark [N];
unordered_map <string, ll> mp;
string s, t;
priority_queue <pll, vector <pll>, greater <pll>> pq;
ll in [N];
//!
void f () {
if (cnt == 2) return;
ans += (cnt >> 1);
ans += (cnt & 1);
}
void dfs (ll v) {
mark[v] = 1;
cnt++;
if (!mark[P[v]]) dfs(P[v]);
}
void sol1 () {
for (ll i = 1; i <= n; i++) if (!mark[i]) cnt = 0, dfs(i), f();
}
//*
int main ()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n; if (n & 1) kill(-1);
for (ll i = 1; i <= n; i++) {
cin>> s >> t;
if (!mp.count(s)) mp[s] = ++lst;
if (!mp.count(t)) mp[t] = ++lst;
P[mp[s]] = mp[t];
in[mp[t]]++;
}
for (ll i = 1; i <= lst; i++) pq.push({in[i], i});
while (!pq.empty()) {
pll x = pq.top();
pq.pop();
// dbg(x.fr, x.sc);
if (x.fr > 0) {
break;
}
if (mark[x.sc]) dbg(x.sc);
if (x.fr != in[x.sc] || mark[x.sc]) continue;
// dbg(x, P[x.sc]);
mark[x.sc] = 1;
if (P[x.sc] == -1 || mark[P[x.sc]]) {
ans++;
continue;
}
mark[P[x.sc]] = 1;
if (P[P[x.sc]] == x.sc) {
continue;
}
ans++;
in[P[P[x.sc]]]--;
pq.push({in[P[P[x.sc]]], P[P[x.sc]]});
}
// dbg(ans);
for (ll i = 1; i <= n; i++) {
if(!mark[i]) {
// dbg(i);
cnt = 1;
mark[i] = 1;
// dbg(i, P[i]);
for(int j = P[i]; j != i; j = P[j]) {
// dbg(j, P[j]);
cnt++;
mark[j] = true;
}
// dbg(cnt);
if (cnt == 2) continue;
ans += (cnt + 1) / 2;
}
}
kill(ans);
return 0;
}
/*
8
leonard emmy
ada emmy
isaac leonard
emmy pierre
pierre bernhard
bernhard emmy
sofia karl
karl sofia
4
a c
b c
c d
d d
*/
Compilation message
polygon.cpp: In function 'int main()':
polygon.cpp:23:18: warning: statement has no effect [-Wunused-value]
23 | #define dbg(...) 0
| ^
polygon.cpp:95:19: note: in expansion of macro 'dbg'
95 | if (mark[x.sc]) dbg(x.sc);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
99 ms |
10000 KB |
Output is correct |
5 |
Correct |
106 ms |
10128 KB |
Output is correct |
6 |
Correct |
116 ms |
10064 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
9872 KB |
Output is correct |
2 |
Correct |
119 ms |
9968 KB |
Output is correct |
3 |
Correct |
82 ms |
9872 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
118 ms |
9952 KB |
Output is correct |
6 |
Correct |
104 ms |
10020 KB |
Output is correct |
7 |
Correct |
105 ms |
9872 KB |
Output is correct |
8 |
Correct |
91 ms |
9872 KB |
Output is correct |
9 |
Correct |
119 ms |
10000 KB |
Output is correct |
10 |
Correct |
78 ms |
9488 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |