#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;
}
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 '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 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6624 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6616 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
168 ms |
24144 KB |
Output is correct |
5 |
Correct |
139 ms |
15824 KB |
Output is correct |
6 |
Correct |
155 ms |
25200 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
16208 KB |
Output is correct |
2 |
Correct |
137 ms |
16724 KB |
Output is correct |
3 |
Correct |
134 ms |
15880 KB |
Output is correct |
4 |
Correct |
2 ms |
6488 KB |
Output is correct |
5 |
Correct |
135 ms |
18260 KB |
Output is correct |
6 |
Correct |
129 ms |
15952 KB |
Output is correct |
7 |
Correct |
174 ms |
16564 KB |
Output is correct |
8 |
Correct |
127 ms |
16344 KB |
Output is correct |
9 |
Correct |
125 ms |
15952 KB |
Output is correct |
10 |
Correct |
107 ms |
16016 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6624 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6616 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
168 ms |
24144 KB |
Output is correct |
20 |
Correct |
139 ms |
15824 KB |
Output is correct |
21 |
Correct |
155 ms |
25200 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
141 ms |
16208 KB |
Output is correct |
24 |
Correct |
137 ms |
16724 KB |
Output is correct |
25 |
Correct |
134 ms |
15880 KB |
Output is correct |
26 |
Correct |
2 ms |
6488 KB |
Output is correct |
27 |
Correct |
135 ms |
18260 KB |
Output is correct |
28 |
Correct |
129 ms |
15952 KB |
Output is correct |
29 |
Correct |
174 ms |
16564 KB |
Output is correct |
30 |
Correct |
127 ms |
16344 KB |
Output is correct |
31 |
Correct |
125 ms |
15952 KB |
Output is correct |
32 |
Correct |
107 ms |
16016 KB |
Output is correct |
33 |
Correct |
1 ms |
6488 KB |
Output is correct |
34 |
Correct |
1 ms |
6488 KB |
Output is correct |
35 |
Correct |
1 ms |
6488 KB |
Output is correct |
36 |
Correct |
1 ms |
6488 KB |
Output is correct |
37 |
Correct |
146 ms |
16192 KB |
Output is correct |
38 |
Correct |
147 ms |
19796 KB |
Output is correct |
39 |
Correct |
145 ms |
17492 KB |
Output is correct |
40 |
Correct |
148 ms |
17104 KB |
Output is correct |
41 |
Correct |
141 ms |
16976 KB |
Output is correct |
42 |
Correct |
153 ms |
16980 KB |
Output is correct |
43 |
Correct |
140 ms |
17152 KB |
Output is correct |
44 |
Correct |
129 ms |
17076 KB |
Output is correct |
45 |
Correct |
131 ms |
17120 KB |
Output is correct |
46 |
Correct |
133 ms |
17156 KB |
Output is correct |
47 |
Correct |
126 ms |
17128 KB |
Output is correct |
48 |
Correct |
140 ms |
17200 KB |
Output is correct |
49 |
Correct |
153 ms |
17728 KB |
Output is correct |
50 |
Correct |
123 ms |
16724 KB |
Output is correct |
51 |
Correct |
1 ms |
6488 KB |
Output is correct |
52 |
Correct |
140 ms |
19024 KB |
Output is correct |
53 |
Correct |
132 ms |
16680 KB |
Output is correct |
54 |
Correct |
136 ms |
17212 KB |
Output is correct |
55 |
Correct |
129 ms |
17328 KB |
Output is correct |
56 |
Correct |
144 ms |
16976 KB |
Output is correct |
57 |
Correct |
102 ms |
16980 KB |
Output is correct |
58 |
Correct |
1 ms |
6492 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6492 KB |
Output is correct |
61 |
Correct |
1 ms |
6492 KB |
Output is correct |
62 |
Correct |
1 ms |
6488 KB |
Output is correct |
63 |
Correct |
1 ms |
6488 KB |
Output is correct |
64 |
Correct |
1 ms |
6492 KB |
Output is correct |
65 |
Correct |
143 ms |
25048 KB |
Output is correct |
66 |
Correct |
133 ms |
16616 KB |
Output is correct |
67 |
Correct |
165 ms |
25936 KB |
Output is correct |
68 |
Correct |
1 ms |
6492 KB |
Output is correct |
69 |
Correct |
1 ms |
6492 KB |
Output is correct |
70 |
Correct |
1 ms |
6492 KB |
Output is correct |
71 |
Correct |
1 ms |
6488 KB |
Output is correct |
72 |
Correct |
1 ms |
6492 KB |
Output is correct |
73 |
Correct |
2 ms |
6492 KB |
Output is correct |
74 |
Correct |
1 ms |
6544 KB |
Output is correct |
75 |
Correct |
1 ms |
6492 KB |
Output is correct |
76 |
Correct |
1 ms |
6616 KB |
Output is correct |
77 |
Correct |
1 ms |
6492 KB |
Output is correct |
78 |
Correct |
1 ms |
6492 KB |
Output is correct |
79 |
Correct |
1 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
6492 KB |
Output is correct |
81 |
Correct |
1 ms |
6492 KB |
Output is correct |
82 |
Correct |
1 ms |
6616 KB |
Output is correct |
83 |
Correct |
1 ms |
6492 KB |
Output is correct |