#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <functional>
#include <cassert>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <sstream>
#include <chrono>
#include <random>
#define ff first
#define ss second
#define PB push_back
#define sz(x) int(x.size())
#define rsz resize
#define fch(xxx, yyy) for (auto xxx : yyy) // abusive notation
#define all(x) (x).begin(),(x).end()
#define eps 1e-9
// more abusive notation (use at your own risk):
//#define int ll
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
// debugging
void __print(int x) {std::cerr << x;}
void __print(ll x) {std::cerr << x;} /* remember to uncomment this when not using THE MACRO */
void __print(unsigned x) {std::cerr << x;}
void __print(ull x) {std::cerr << x;}
void __print(float x) {std::cerr << x;}
void __print(double x) {std::cerr << x;}
void __print(ld x) {std::cerr << x;}
void __print(char x) {std::cerr << '\'' << x << '\'';}
void __print(const char *x) {std::cerr << '\"' << x << '\"';}
void __print(const string& x) {std::cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void __print(const pair<T, V> &x) {std::cerr << '{'; __print(x.ff); std::cerr << ", "; __print(x.ss); std::cerr << '}';}
template<typename T> void __print(const T& x) {int f = 0; std::cerr << '{'; for (auto &i: x) std::cerr << (f++ ? ", " : ""), __print(i); std::cerr << "}";}
void _print() {std::cerr << "]\n";}
template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) std::cerr << ", "; _print(v...);}
void println() {std::cerr << ">--------------------<" << endl;}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// templates
template <class T> bool ckmin(T &a, const T &b) {return b<a ? a = b, 1 : 0;}
template <class T> bool ckmax(T &a, const T &b) {return b>a ? a = b, 1 : 0;}
template <class T> using gr = greater<T>;
template <class T> using vc = vector<T>;
template <class T> using p_q = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vc<T>, gr<T>>;
template <class T1, class T2> using pr = pair<T1, T2>;
mt19937_64 rng_ll(chrono::steady_clock::now().time_since_epoch().count());
int rng(int M) {return (int)(rng_ll()%M);} /*returns any random number in [0, M) */
// const variables
constexpr int INF = (int)2e9;
constexpr int MOD = 998244353;
constexpr ll LL_INF = (ll)3e18;
constexpr int mod = (int)1e9 + 7;
constexpr ll inverse = 500000004LL; // inverse of 2 modulo 1e9 + 7
void setIO(const string& str) {// fast input/output
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (str.empty()) return;
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
int N, cnt = 1;
map<string, int> mp;
vc<vi> adj;
vc<vi> fuck, comps;
vi dp[2], vst, f, cc;
void dfs(int v) {
dp[0][v] = 0, dp[1][v] = 1;
vst[v] = 1;
vi children;
fch(u, adj[v]) if (!vst[u]) {
dfs(u);
children.PB(u);
dp[1][v] += dp[0][u];
}
//debug(v);
//debug(children);
if (sz(children) == 0) return;
int C = sz(children);
vi psum(C), ssum(C);
for (int i = 0; i < C; ++i) {
psum[i] = (i > 0 ? psum[i - 1] : 0) + dp[0][children[i]];
}
for (int i = C-1; i >= 0; --i) {
ssum[i] = (i < C - 1 ? ssum[i + 1] : 0) + dp[0][children[i]];
}
//debug(psum);
//debug(ssum);
for (int i = 0; i < C; ++i) {
ckmax(dp[0][v], dp[1][children[i]] + (i > 0 ? psum[i - 1] : 0) + (i < C - 1 ? ssum[i + 1] : 0));
}
//debug(dp[0][v], dp[1][v]);
}
void undo(int v) {
for (int i = 0; i < sz(comps[cc[v]]); ++i) {
vst[comps[cc[v]][i]] = 0;
}
}
void why_not(int v) {
for (int i = 0; i < sz(comps[cc[v]]); ++i) {
vst[comps[cc[v]][i]] = 1;
}
}
void dfs1(int v) {
fch(u, fuck[v]) if (cc[u] != cc[v]) {
cc[u] = cc[v];
dfs1(u);
}
}
signed main() { // TIME YOURSELF !!!
setIO("");
cin >> N;
if (N & 1) {
cout << "-1\n";
return 0;
}
adj.rsz(N+1), dp[0].rsz(N+1, 0), dp[1].rsz(N+1, 1), vst.rsz(N+1), f.rsz(N+1), cc.rsz(N+1, -1), comps.rsz(N+1);
fuck.rsz(N+1);
for (int i = 0; i < N; ++i) {
string a, b;
cin >> a >> b;
if (mp[a] == 0) mp[a] = cnt++;
if (mp[b] == 0) mp[b] = cnt++;
adj[mp[b]].PB(mp[a]);
fuck[mp[b]].PB(mp[a]);
fuck[mp[a]].PB(mp[b]);
f[mp[a]] = mp[b];
}
{ // finding the connected components
for (int i = 1; i <= N; ++i) {
if (cc[i] == -1) {
cc[i] = i;
dfs1(i);
}
}
for (int i = 1; i <= N; ++i) {
comps[cc[i]].PB(i);
}
}
//debug(mp);
int ans = 0;
for (int i = 1; i <= N; ++i) {
if (!vst[i]) {
//debug(i);
int t = f[i], h = f[f[i]];
while (t != h) {
t = f[t];
h = f[f[h]];
}
int L = 1;
t = f[t];
while (t != h) {
L++;
t = f[t];
}
//debug(L, t);
if (L == 1) {
vst[t] = 1;
dfs(t);
ans += dp[0][t];
//debug(dp[0][t]);
} else {
int res = 0;
int s = f[t];
// 2: (t, f[t]) are not connected
dfs(t);
ckmax(res, dp[0][t]);
undo(t);
// 1: (t, f[t]) are connected
if (L > 2) {
vst[t] = vst[s] = 1;
int temp = t;
t = f[s];
while (f[t] != temp) {
t = f[t];
}
dfs(t);
int sum = dp[0][t];
t = temp;
fch(u, adj[t]) if (!vst[u]) {
dfs(u);
sum += dp[0][u];
}
fch(u, adj[s]) if (!vst[u]) {
dfs(u);
sum += dp[0][u];
}
ckmax(res, sum);
} else if (L == 2) {
vst[t] = vst[s] = 1;
int sum = 2;
fch(u, adj[t]) if (!vst[u]) {
dfs(u);
sum += dp[0][u];
}
fch(u, adj[s]) if (!vst[u]) {
dfs(u);
sum += dp[0][u];
}
ckmax(res, sum);
}
ans += res;
}
why_not(i);
}
}
assert(ans <= N);
cout << N - ans << '\n';
return 0;
}
// TLE -> TRY NOT USING DEFINE INT LONG LONG
// CE -> CHECK LINE 45
// 5000 * 5000 size matrices are kinda big (potential mle)
// Do something, start simpler
Compilation message
polygon.cpp: In function 'void setIO(const string&)':
polygon.cpp:88:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
508 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
333 ms |
33504 KB |
Output is correct |
5 |
Correct |
290 ms |
24400 KB |
Output is correct |
6 |
Correct |
309 ms |
35016 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
282 ms |
23240 KB |
Output is correct |
2 |
Correct |
283 ms |
28116 KB |
Output is correct |
3 |
Correct |
236 ms |
28868 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
295 ms |
36300 KB |
Output is correct |
6 |
Correct |
271 ms |
24788 KB |
Output is correct |
7 |
Correct |
283 ms |
24916 KB |
Output is correct |
8 |
Correct |
273 ms |
26112 KB |
Output is correct |
9 |
Correct |
238 ms |
24528 KB |
Output is correct |
10 |
Correct |
175 ms |
24944 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
508 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
360 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
333 ms |
33504 KB |
Output is correct |
20 |
Correct |
290 ms |
24400 KB |
Output is correct |
21 |
Correct |
309 ms |
35016 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
282 ms |
23240 KB |
Output is correct |
24 |
Correct |
283 ms |
28116 KB |
Output is correct |
25 |
Correct |
236 ms |
28868 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
295 ms |
36300 KB |
Output is correct |
28 |
Correct |
271 ms |
24788 KB |
Output is correct |
29 |
Correct |
283 ms |
24916 KB |
Output is correct |
30 |
Correct |
273 ms |
26112 KB |
Output is correct |
31 |
Correct |
238 ms |
24528 KB |
Output is correct |
32 |
Correct |
175 ms |
24944 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
309 ms |
25908 KB |
Output is correct |
38 |
Incorrect |
289 ms |
26564 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |