#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
#define mk make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 1e5 + 50;
int n, dp[N][2], dp2[N][2], vis[N], mark[N], t;
pll e[N];
vector<int> pth;
vector<int> vc[N];
vector<int> adj[N];
void dfs1(int u, int p) {
vis[u] = 1;
pth.push_back(u);
bool flg = 0;
bool flg2 = 0;
for (int v:adj[u]) {
if (!flg && v == p) {
flg = 1;
continue;
}
if (!vis[v]) {
dfs1(v, u);
}
else if (!flg2 && vis[v] == 1) {
flg2 = 1;
int pt = (int)pth.size()-1;
while (pth[pt] != v) {
vc[t].push_back(pth[pt]);
mark[pth[pt]] = 1;
pt--;
}
vc[t].push_back(pth[pt]);
mark[pth[pt]] = 1;
}
}
pth.pop_back();
vis[u] = 2;
}
void dfs2(int u, int p) {
for (int v:adj[u]) {
if (v == p || mark[v]) continue;
dfs2(v, u);
dp[u][1] += dp[v][0];
}
dp[u][0] = dp[u][1];
for (int v:adj[u]) {
if (v == p || mark[v]) continue;
dp[u][0] = max(dp[u][0], dp[u][1] - dp[v][0] + dp[v][1] + 1);
}
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
string s, t;
cin >> s >> t;
ll u = 0, v = 0;
for (char c:s) {
u = u*27 + (c-'a'+1);
}
for (char c:t) {
v = v*27 + (c-'a'+1);
}
e[i] = {u, v};
}
if (n&1) {
cout << -1 << endl;
return;
}
sort(e+1, e+n+1);
for (int u = 1; u <= n; u++) {
int v = lower_bound(e+1, e+n+1, mk(e[u].S, 0ll))-e;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int u = 1; u <= n; u++) {
if (!vis[u]) {
t++;
dfs1(u, 0);
}
}
int ans = 0;
for (int x = 1; x <= t; x++) {
for (int u:vc[x]) {
dfs2(u, 0);
}
if (vc[x].size() == 1) {
ans += dp[vc[x][0]][0];
continue;
}
if (vc[x].size() == 2) {
int u = vc[x][0], v = vc[x][1];
ans += max(dp[u][1] + dp[v][1] + 2, dp[u][0] + dp[v][0]);
continue;
}
vc[x].push_back(vc[x][0]);
int s = vc[x].size();
int mx = 0;
dp2[vc[x][0]][0] = 0;
dp2[vc[x][0]][1] = -1e9;
for (int i = 1; i < s; i++) {
int u = vc[x][i];
int v = vc[x][i-1];
dp2[u][0] = max(dp2[v][0] + dp[v][0], dp2[v][1] + dp[v][1]);
dp2[u][1] = dp2[v][0] + dp[v][1] + 1;
}
mx = max(mx, dp2[vc[x].back()][0]);
dp2[vc[x][0]][0] = -1e9;
dp2[vc[x][0]][1] = 0;
for (int i = 1; i < s; i++) {
int u = vc[x][i];
int v = vc[x][i-1];
dp2[u][0] = max(dp2[v][0] + dp[v][0], dp2[v][1] + dp[v][1]);
dp2[u][1] = dp2[v][0] + dp[v][1] + 1;
}
mx = max(mx, dp2[vc[x].back()][1]);
ans += mx;
}
cout << n-ans << endl;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5032 KB |
Output is correct |
7 |
Correct |
4 ms |
5036 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5032 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
83 ms |
21820 KB |
Output is correct |
5 |
Correct |
75 ms |
15140 KB |
Output is correct |
6 |
Correct |
99 ms |
22908 KB |
Output is correct |
7 |
Correct |
19 ms |
8704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
13408 KB |
Output is correct |
2 |
Correct |
80 ms |
14924 KB |
Output is correct |
3 |
Correct |
50 ms |
16532 KB |
Output is correct |
4 |
Correct |
20 ms |
8740 KB |
Output is correct |
5 |
Correct |
116 ms |
20716 KB |
Output is correct |
6 |
Correct |
89 ms |
13064 KB |
Output is correct |
7 |
Correct |
68 ms |
13528 KB |
Output is correct |
8 |
Correct |
61 ms |
14916 KB |
Output is correct |
9 |
Correct |
62 ms |
13340 KB |
Output is correct |
10 |
Correct |
38 ms |
13340 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5036 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5024 KB |
Output is correct |
6 |
Correct |
3 ms |
5032 KB |
Output is correct |
7 |
Correct |
4 ms |
5036 KB |
Output is correct |
8 |
Correct |
3 ms |
5024 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5024 KB |
Output is correct |
16 |
Correct |
4 ms |
5032 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
83 ms |
21820 KB |
Output is correct |
20 |
Correct |
75 ms |
15140 KB |
Output is correct |
21 |
Correct |
99 ms |
22908 KB |
Output is correct |
22 |
Correct |
19 ms |
8704 KB |
Output is correct |
23 |
Correct |
85 ms |
13408 KB |
Output is correct |
24 |
Correct |
80 ms |
14924 KB |
Output is correct |
25 |
Correct |
50 ms |
16532 KB |
Output is correct |
26 |
Correct |
20 ms |
8740 KB |
Output is correct |
27 |
Correct |
116 ms |
20716 KB |
Output is correct |
28 |
Correct |
89 ms |
13064 KB |
Output is correct |
29 |
Correct |
68 ms |
13528 KB |
Output is correct |
30 |
Correct |
61 ms |
14916 KB |
Output is correct |
31 |
Correct |
62 ms |
13340 KB |
Output is correct |
32 |
Correct |
38 ms |
13340 KB |
Output is correct |
33 |
Correct |
3 ms |
4948 KB |
Output is correct |
34 |
Correct |
3 ms |
4948 KB |
Output is correct |
35 |
Correct |
3 ms |
5036 KB |
Output is correct |
36 |
Correct |
3 ms |
4948 KB |
Output is correct |
37 |
Correct |
90 ms |
14272 KB |
Output is correct |
38 |
Correct |
77 ms |
15776 KB |
Output is correct |
39 |
Correct |
72 ms |
14496 KB |
Output is correct |
40 |
Correct |
66 ms |
13672 KB |
Output is correct |
41 |
Correct |
74 ms |
13576 KB |
Output is correct |
42 |
Correct |
75 ms |
13992 KB |
Output is correct |
43 |
Correct |
88 ms |
14192 KB |
Output is correct |
44 |
Correct |
75 ms |
14260 KB |
Output is correct |
45 |
Correct |
71 ms |
14204 KB |
Output is correct |
46 |
Correct |
71 ms |
14152 KB |
Output is correct |
47 |
Correct |
68 ms |
14508 KB |
Output is correct |
48 |
Correct |
78 ms |
13424 KB |
Output is correct |
49 |
Correct |
77 ms |
14888 KB |
Output is correct |
50 |
Correct |
45 ms |
16528 KB |
Output is correct |
51 |
Correct |
20 ms |
8672 KB |
Output is correct |
52 |
Correct |
91 ms |
20616 KB |
Output is correct |
53 |
Correct |
87 ms |
13164 KB |
Output is correct |
54 |
Correct |
72 ms |
13456 KB |
Output is correct |
55 |
Correct |
63 ms |
14908 KB |
Output is correct |
56 |
Correct |
61 ms |
13344 KB |
Output is correct |
57 |
Correct |
34 ms |
13388 KB |
Output is correct |
58 |
Correct |
3 ms |
5032 KB |
Output is correct |
59 |
Correct |
4 ms |
5004 KB |
Output is correct |
60 |
Correct |
3 ms |
4948 KB |
Output is correct |
61 |
Correct |
3 ms |
4948 KB |
Output is correct |
62 |
Correct |
3 ms |
4948 KB |
Output is correct |
63 |
Correct |
3 ms |
4948 KB |
Output is correct |
64 |
Correct |
3 ms |
4948 KB |
Output is correct |
65 |
Correct |
104 ms |
21796 KB |
Output is correct |
66 |
Correct |
86 ms |
15184 KB |
Output is correct |
67 |
Correct |
106 ms |
22936 KB |
Output is correct |
68 |
Correct |
18 ms |
8652 KB |
Output is correct |
69 |
Correct |
3 ms |
4948 KB |
Output is correct |
70 |
Correct |
3 ms |
5036 KB |
Output is correct |
71 |
Correct |
3 ms |
5048 KB |
Output is correct |
72 |
Correct |
4 ms |
4948 KB |
Output is correct |
73 |
Correct |
4 ms |
5028 KB |
Output is correct |
74 |
Correct |
4 ms |
5028 KB |
Output is correct |
75 |
Correct |
4 ms |
4948 KB |
Output is correct |
76 |
Correct |
3 ms |
4948 KB |
Output is correct |
77 |
Correct |
3 ms |
4948 KB |
Output is correct |
78 |
Correct |
3 ms |
4948 KB |
Output is correct |
79 |
Correct |
3 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
4948 KB |
Output is correct |
81 |
Correct |
3 ms |
4948 KB |
Output is correct |
82 |
Correct |
3 ms |
5032 KB |
Output is correct |
83 |
Correct |
3 ms |
4948 KB |
Output is correct |