#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b or a == -1) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
int n, g[N], deg[N];
vector<int> adj[N];
stack<int> st;
bool on[N], on_cycle[N], vis[N];
void dfs(int u) {
st.push(u); vis[u] = 1; on[u] = 1;
int v = g[u];
if (!vis[v]) {
dfs(v);
}
if (on[v]) {
while (st.size()) {
int k = st.top(); on_cycle[k] = 1;
st.pop(); on[k] = 0; if (k == v) break ;
}
}
if (sz(st) and st.top() == u) st.pop();
on[u] = 0;
}
int cnt[N], dp[N][2][2];
void main_code() {
cin >> n; map<string, int> mp;
if (n % 2 == 1) evoid(-1);
for(int i = 1, cnt = 0; i <= n; ++i) {
// string x, y; cin >> x >> y;
// if (mp.find(x) == mp.end()) {
// mp[x] = ++cnt;
// }
// if (mp.find(y) == mp.end()) {
// mp[y] = ++cnt;
// }
// int a = mp[x], b = mp[y];
// g[a] = b;
int a, b; cin >> a >> b;
g[a] = b;
}
FOR(i, 1, n) if (!vis[i]) dfs(i);
FOR(i, 1, n) {
++deg[g[i]];
}
queue<int> q;
int res = 0;
FOR(i, 1, n) if (deg[i] == 0) {
q.push(i);
}
vec<bool> c(n + 1, 0);
while (!q.empty()) {
int u = q.front(); q.pop();
--deg[g[u]]; if (deg[g[u]] == 0) q.push(g[u]);
if (!c[u]) {
if (on_cycle[g[u]]) {
++cnt[g[u]];
continue ;
}
++res; if (!c[g[u]] and on_cycle[g[u]] == 0) c[g[u]] = 1;
}
}
// FOR(i, 1, n) cout << c[i] << ' '; cout << nl << res << nl;
// FOR(i, 1, n) cout << cnt[i] << ' '; cout << nl;
vec<bool> mark(n + 1, 0);
FOR(i, 1, n) if (on_cycle[i] and !mark[i]) {
vec<int> cy;
cy.push_back(i);
for(int j = g[i]; j != i; j = g[j]) {
cy.push_back(j);
}
for(int k : cy) {
if (cnt[k] > 1) {
res += cnt[k] - 1;
cnt[k] = 1;
}
mark[k] = 1;
}
if (sz(cy) == 1) {
++res; continue ;
}
if (sz(cy) == 2) {
for(int k : cy) res += cnt[k];
continue ;
}
// for(int k : cy) cout << k << ' '; cout << nl;
int m = sz(cy);
FOR(i, 0, m) FOR(j, 0, 1) FOR(k, 0, 1) dp[i][j][k] = n;
dp[0][0][0] = 1;
dp[0][0][1] = cnt[ cy[0] ];
dp[0][1][0] = cnt[ cy[0] ] + 1;
FOR(i, 0, m - 3) FOR(j, 0, 1) FOR(k, 0, 1) {
if (k == 0) {
// no match (i, i + 1)
mini(dp[i + 1][j][0], dp[i][j][k] + 1);
mini(dp[i + 1][j][1], dp[i][j][k] + cnt[ cy[i + 1] ]);
} else {
mini(dp[i + 1][j][0], dp[i][j][k] + cnt[ cy[i + 1] ] + 1);
}
}
// match (0, m - 1)
mini(dp[m - 1][1][1], dp[m - 2][1][0] + cnt[ cy[m - 1] ]);
// match (m - 1, m - 2)
mini(dp[m - 1][0][0], dp[m - 2][0][1] + cnt[ cy[m - 1] ] + 1);
// match (m - 1, it'child)
mini(dp[m - 1][0][0], dp[m - 2][0][0] + 1);
int l = n; FOR(j, 0, 1) FOR(k, 0, 1) l = min(l, dp[m - 1][j][k]);
res += l;
}
cout << res;
}
/* Let the river flows naturally */
Compilation message
polygon.cpp: In function 'void main_code()':
polygon.cpp:75:20: warning: unused variable 'cnt' [-Wunused-variable]
75 | for(int i = 1, cnt = 0; i <= n; ++i) {
| ^~~
polygon.cpp: In function 'int main()':
polygon.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |