// In the name of God
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
typedef long long ll;
const int N = 1e5 + 5, MOD = 1e9 + 7;
int n;
vector<int> adj[N];
map<string, int> M;
bool vis[N];
int cnt, par[N], h[N], C[3][N];
vector<int> V;
void dfs(int v) {
vis[v] = true;
cnt++;
V.pb(v);
for (auto u : adj[v]) {
if (!vis[u]) {
par[u] = v;
h[u] = h[v] + 1;
dfs(u);
}
}
}
void dfs2(int v) {
vis[v] = true;
C[2][v] = 1;
int mx = 0;
for (auto u : adj[v]) {
if (vis[u])
continue;
dfs2(u);
C[2][v] += C[1][u];
C[0][v] += C[1][u];
mx = max(mx, C[2][u] - C[1][u]);
}
C[1][v] = C[0][v] + mx;
C[1][v] = max(C[1][v], C[0][v]);
}
int dp[N][3];
/*int DP(vector<int> vec) {
assert(vec.empty());
if (vec.empty()) {
return 0;
}
for (int i = 0; i < vec.size(); i++) {
int v = vec[i];
dp[i] = (i ? dp[i - 1] : 0) + C[1][v];
dp[i] = max(dp[i], (i - 1 ? dp[i - 2] : 0) + C[0][v] + (i ? C[1][vec[i - 1]] : 0));
}
return dp[(int)vec.size() - 1];
}*/
int SOLVE(int x) {
V.clear();
cnt = 0;
dfs(x);
int up = x, dn = x;
bool ok = true;
for (auto v : V) {
int c = 0;
for (auto u : adj[v]) {
c += u == par[v];
if (h[u] < h[v] - 1) {
up = u;
dn = v;
}
if (u == v) {
ok = false;
dn = up = v;
}
}
if (c > 1) {
dn = v;
up = par[v];
}
}
int xx = dn;
vector<int> cle;
while (dn != up) {
cle.pb(dn);
dn = par[dn];
}
cle.pb(up);
for (auto v : V)
vis[v] = false;
for (auto v : cle)
vis[v] = true;
for (auto v : cle) {
dfs2(v);
}
reverse(cle.begin(), cle.end());
if (cle.size() == 1) {
return max(C[0][up], C[1][up]);
}
if (cle.size() == 2) {
return max(C[2][up] + C[2][xx], max(C[0][up], C[1][up]) + max(C[0][xx], C[1][xx]));
}
int v = cle.front();
// 0:
dp[0][0] = 0, dp[0][1] = 0;
for (int i = 1; i < cle.size(); i++) {
int u = cle[i];
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + C[1][u];
dp[i][1] = C[2][u] + (i > 1 ? C[0][cle[i - 1]] : 0) + (i > 2 ? max(dp[i - 2][1], dp[i - 2][0]) : 0);
}
int ans = max(dp[(int)cle.size() - 1][0], dp[(int)cle.size() - 1][1]) + C[0][v];
// 1:
dp[1][0] = max(C[0][cle[1]], C[1][cle[1]]);
dp[1][1] = 0;
for (int i = 2; i < cle.size(); i++) {
int u = cle[i];
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + C[1][u];
dp[i][1] = C[2][u] + (i > 1 ? C[0][cle[i - 1]] : 0) + (i > 2 ? max(dp[i - 2][1], dp[i - 2][0]) : 0);
}
ans = max(ans, max(dp[(int)cle.size() - 1][1], dp[(int)cle.size() - 1][0]) + C[1][v]);
// 2:
dp[1][0] = C[1][cle[1]];
dp[1][1] = 0;
for (int i = 2; i < cle.size(); i++) {
int u = cle[i];
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + C[1][u];
dp[i][1] = C[2][u] + (i > 1 ? C[0][cle[i - 1]] : 0) + (i > 2 ? max(dp[i - 2][1], dp[i - 2][0]) : 0);
}
ans = max(ans, C[0][cle.back()] + C[2][v] + max(dp[(int)cle.size() - 2][1], dp[(int)cle.size() - 2][0]));
return ans;
}
void solve() {
cin >> n;
set<string> S;
vector<pair<string, string> > tmp;
for (int i = 0; i < n; i++) {
string a, b; cin >> a >> b;
tmp.pb(make_pair(a, b));
S.insert(a), S.insert(b);
}
int inx = 0;
for (auto s : S) {
M[s] = ++inx;
}
for (auto [a, b] : tmp) {
adj[M[a]].pb(M[b]);
adj[M[b]].pb(M[a]);
}
if (n % 2) {
cout << -1 << endl;
return;
}
int ans = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
ans += SOLVE(i);
}
}
cout << n - ans << endl;
}
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
solve();
return 0;
}
Compilation message
polygon.cpp: In function 'int SOLVE(int)':
polygon.cpp:107:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int i = 1; i < cle.size(); i++) {
| ~~^~~~~~~~~~~~
polygon.cpp:116:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for (int i = 2; i < cle.size(); i++) {
| ~~^~~~~~~~~~~~
polygon.cpp:125:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for (int i = 2; i < cle.size(); i++) {
| ~~^~~~~~~~~~~~
polygon.cpp:63:10: warning: variable 'ok' set but not used [-Wunused-but-set-variable]
63 | bool ok = true;
| ^~
polygon.cpp: In function 'void solve()':
polygon.cpp:147:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
147 | for (auto [a, b] : tmp) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2692 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2676 KB |
Output is correct |
10 |
Correct |
2 ms |
2680 KB |
Output is correct |
11 |
Correct |
2 ms |
2684 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2684 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2688 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
440 ms |
39120 KB |
Output is correct |
5 |
Correct |
363 ms |
31488 KB |
Output is correct |
6 |
Correct |
394 ms |
40192 KB |
Output is correct |
7 |
Correct |
400 ms |
29884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
403 ms |
31944 KB |
Output is correct |
2 |
Correct |
454 ms |
34560 KB |
Output is correct |
3 |
Correct |
313 ms |
30724 KB |
Output is correct |
4 |
Correct |
337 ms |
29936 KB |
Output is correct |
5 |
Correct |
418 ms |
39984 KB |
Output is correct |
6 |
Correct |
376 ms |
32864 KB |
Output is correct |
7 |
Correct |
345 ms |
32036 KB |
Output is correct |
8 |
Correct |
352 ms |
31856 KB |
Output is correct |
9 |
Correct |
368 ms |
32720 KB |
Output is correct |
10 |
Correct |
278 ms |
32780 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2692 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2676 KB |
Output is correct |
10 |
Correct |
2 ms |
2680 KB |
Output is correct |
11 |
Correct |
2 ms |
2684 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
1 ms |
2684 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2688 KB |
Output is correct |
17 |
Correct |
1 ms |
2644 KB |
Output is correct |
18 |
Correct |
1 ms |
2644 KB |
Output is correct |
19 |
Correct |
440 ms |
39120 KB |
Output is correct |
20 |
Correct |
363 ms |
31488 KB |
Output is correct |
21 |
Correct |
394 ms |
40192 KB |
Output is correct |
22 |
Correct |
400 ms |
29884 KB |
Output is correct |
23 |
Correct |
403 ms |
31944 KB |
Output is correct |
24 |
Correct |
454 ms |
34560 KB |
Output is correct |
25 |
Correct |
313 ms |
30724 KB |
Output is correct |
26 |
Correct |
337 ms |
29936 KB |
Output is correct |
27 |
Correct |
418 ms |
39984 KB |
Output is correct |
28 |
Correct |
376 ms |
32864 KB |
Output is correct |
29 |
Correct |
345 ms |
32036 KB |
Output is correct |
30 |
Correct |
352 ms |
31856 KB |
Output is correct |
31 |
Correct |
368 ms |
32720 KB |
Output is correct |
32 |
Correct |
278 ms |
32780 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2644 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
2644 KB |
Output is correct |
37 |
Correct |
446 ms |
32384 KB |
Output is correct |
38 |
Correct |
471 ms |
33620 KB |
Output is correct |
39 |
Correct |
445 ms |
31936 KB |
Output is correct |
40 |
Correct |
414 ms |
32884 KB |
Output is correct |
41 |
Correct |
458 ms |
32056 KB |
Output is correct |
42 |
Correct |
421 ms |
32796 KB |
Output is correct |
43 |
Correct |
416 ms |
32448 KB |
Output is correct |
44 |
Correct |
445 ms |
32756 KB |
Output is correct |
45 |
Correct |
408 ms |
32780 KB |
Output is correct |
46 |
Correct |
415 ms |
32928 KB |
Output is correct |
47 |
Correct |
383 ms |
33036 KB |
Output is correct |
48 |
Correct |
380 ms |
31856 KB |
Output is correct |
49 |
Correct |
426 ms |
34440 KB |
Output is correct |
50 |
Correct |
338 ms |
30696 KB |
Output is correct |
51 |
Correct |
378 ms |
29972 KB |
Output is correct |
52 |
Correct |
452 ms |
39916 KB |
Output is correct |
53 |
Correct |
463 ms |
32920 KB |
Output is correct |
54 |
Correct |
420 ms |
31992 KB |
Output is correct |
55 |
Correct |
369 ms |
31876 KB |
Output is correct |
56 |
Correct |
443 ms |
32676 KB |
Output is correct |
57 |
Correct |
300 ms |
32776 KB |
Output is correct |
58 |
Correct |
2 ms |
2644 KB |
Output is correct |
59 |
Correct |
2 ms |
2644 KB |
Output is correct |
60 |
Correct |
2 ms |
2644 KB |
Output is correct |
61 |
Correct |
2 ms |
2688 KB |
Output is correct |
62 |
Correct |
2 ms |
2644 KB |
Output is correct |
63 |
Correct |
2 ms |
2644 KB |
Output is correct |
64 |
Correct |
2 ms |
2680 KB |
Output is correct |
65 |
Correct |
435 ms |
39084 KB |
Output is correct |
66 |
Correct |
373 ms |
31544 KB |
Output is correct |
67 |
Correct |
370 ms |
40204 KB |
Output is correct |
68 |
Correct |
367 ms |
29880 KB |
Output is correct |
69 |
Correct |
2 ms |
2680 KB |
Output is correct |
70 |
Correct |
2 ms |
2688 KB |
Output is correct |
71 |
Correct |
2 ms |
2688 KB |
Output is correct |
72 |
Correct |
2 ms |
2644 KB |
Output is correct |
73 |
Correct |
2 ms |
2644 KB |
Output is correct |
74 |
Correct |
3 ms |
2680 KB |
Output is correct |
75 |
Correct |
2 ms |
2644 KB |
Output is correct |
76 |
Correct |
2 ms |
2644 KB |
Output is correct |
77 |
Correct |
2 ms |
2692 KB |
Output is correct |
78 |
Correct |
2 ms |
2692 KB |
Output is correct |
79 |
Correct |
3 ms |
2656 KB |
Output is correct |
80 |
Correct |
2 ms |
2692 KB |
Output is correct |
81 |
Correct |
2 ms |
2696 KB |
Output is correct |
82 |
Correct |
2 ms |
2684 KB |
Output is correct |
83 |
Correct |
2 ms |
2644 KB |
Output is correct |