#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 1e5+10;
vector<string> all;
pair<string,string> tmp[mxn];
int n;
int arr[mxn];
vector<int> jelly[mxn];
bitset<mxn> vis,psyco;
int dp[mxn][2];
int deg[mxn];
queue<int> q;
int ans;
void dfs(int now){
dp[now][0] = dp[now][1] = 0;
int sh = 1e9;
for(auto nxt:jelly[now]){
if(psyco[nxt])continue;
dfs(nxt);
dp[now][0] += max(dp[nxt][0],dp[nxt][1]);
sh = min(sh,max(dp[nxt][0],dp[nxt][1])-dp[nxt][0]);
}
dp[now][1] = max(dp[now][0],dp[now][0]-sh+1);
return;
}
void calc(vector<pii> &v){
if(v.size() == 1){
ans -= max(v[0].fs,v[0].sc);
return;
}
else if(v.size() == 2){
int tmp = max(v[0].fs+v[1].fs+2,max(v[0].fs,v[0].sc)+max(v[1].fs,v[1].sc));
ans -= tmp;
return;
}
int re = 0;
int dp[v.size()][2] = {};
dp[0][1] = max(v[0].fs,v[0].sc);
dp[0][0] = v[0].fs;
for(int i = 1;i<v.size();i++){
dp[i][0] = dp[i-1][1]+v[i].fs;
dp[i][1] = max({dp[i-1][0]+v[i].fs+1,dp[i-1][1]+v[i].sc,dp[i][0]});
}
re = max(re,dp[v.size()-1][1]);
v.push_back(v[0]);
v.erase(v.begin());
memset(dp,0,sizeof(dp));
dp[0][1] = max(v[0].fs,v[0].sc);
dp[0][0] = v[0].fs;
for(int i = 1;i<v.size();i++){
dp[i][0] = dp[i-1][1]+v[i].fs;
dp[i][1] = max({dp[i-1][0]+v[i].fs+1,dp[i-1][1]+v[i].sc,dp[i][0]});
}
re = max(re,dp[v.size()-1][1]);
ans -= re;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(int i =0;i<n;i++){
cin>>tmp[i].fs>>tmp[i].sc;
all.push_back(tmp[i].fs);
}
if(n&1){
cout<<-1;
return 0;
}
sort(all.begin(),all.end());
for(int i = 0;i<n;i++){
auto a = lower_bound(all.begin(),all.end(),tmp[i].fs)-all.begin()+1;
auto b = lower_bound(all.begin(),all.end(),tmp[i].sc)-all.begin()+1;
arr[a] = b;
deg[b]++;
jelly[b].push_back(a);
}
psyco.set();
for(int i = 1;i<=n;i++){
if(!deg[i])q.push(i);
}
while(!q.empty()){
auto now = q.front();
q.pop();
psyco[now] = false;
deg[arr[now]]--;
if(!deg[arr[now]]){
q.push(arr[now]);
}
}
for(int i = 1;i<=n;i++){
if(psyco[i])dfs(i);
}
vector<pii> v;
ans = n;
for(int i = 1;i<=n;i++){
if(vis[i]||!psyco[i])continue;
v.clear();
int now = i;
do{
v.push_back({dp[now][0],dp[now][1]});
vis[now] = true;
now = arr[now];
}while(now != i);
calc(v);
}
cout<<ans;
}
Compilation message
polygon.cpp: In function 'void calc(std::vector<std::pair<int, int> >&)':
polygon.cpp:50:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i = 1;i<v.size();i++){
| ~^~~~~~~~~
polygon.cpp:60:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i = 1;i<v.size();i++){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10492 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10328 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10496 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
3 ms |
10328 KB |
Output is correct |
4 |
Correct |
93 ms |
22212 KB |
Output is correct |
5 |
Correct |
87 ms |
19908 KB |
Output is correct |
6 |
Correct |
87 ms |
21568 KB |
Output is correct |
7 |
Correct |
15 ms |
17360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
18368 KB |
Output is correct |
2 |
Correct |
91 ms |
20412 KB |
Output is correct |
3 |
Correct |
77 ms |
18876 KB |
Output is correct |
4 |
Correct |
16 ms |
17364 KB |
Output is correct |
5 |
Correct |
107 ms |
24920 KB |
Output is correct |
6 |
Correct |
90 ms |
17804 KB |
Output is correct |
7 |
Correct |
88 ms |
18112 KB |
Output is correct |
8 |
Correct |
90 ms |
17908 KB |
Output is correct |
9 |
Correct |
83 ms |
17344 KB |
Output is correct |
10 |
Correct |
66 ms |
18384 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
2 ms |
10428 KB |
Output is correct |
13 |
Correct |
2 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10328 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
3 ms |
10492 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
3 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10332 KB |
Output is correct |
7 |
Correct |
2 ms |
10332 KB |
Output is correct |
8 |
Correct |
3 ms |
10332 KB |
Output is correct |
9 |
Correct |
3 ms |
10328 KB |
Output is correct |
10 |
Correct |
3 ms |
10332 KB |
Output is correct |
11 |
Correct |
2 ms |
10332 KB |
Output is correct |
12 |
Correct |
3 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10496 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
3 ms |
10332 KB |
Output is correct |
18 |
Correct |
3 ms |
10328 KB |
Output is correct |
19 |
Correct |
93 ms |
22212 KB |
Output is correct |
20 |
Correct |
87 ms |
19908 KB |
Output is correct |
21 |
Correct |
87 ms |
21568 KB |
Output is correct |
22 |
Correct |
15 ms |
17360 KB |
Output is correct |
23 |
Correct |
99 ms |
18368 KB |
Output is correct |
24 |
Correct |
91 ms |
20412 KB |
Output is correct |
25 |
Correct |
77 ms |
18876 KB |
Output is correct |
26 |
Correct |
16 ms |
17364 KB |
Output is correct |
27 |
Correct |
107 ms |
24920 KB |
Output is correct |
28 |
Correct |
90 ms |
17804 KB |
Output is correct |
29 |
Correct |
88 ms |
18112 KB |
Output is correct |
30 |
Correct |
90 ms |
17908 KB |
Output is correct |
31 |
Correct |
83 ms |
17344 KB |
Output is correct |
32 |
Correct |
66 ms |
18384 KB |
Output is correct |
33 |
Correct |
2 ms |
10332 KB |
Output is correct |
34 |
Correct |
2 ms |
10428 KB |
Output is correct |
35 |
Correct |
2 ms |
10332 KB |
Output is correct |
36 |
Correct |
2 ms |
10332 KB |
Output is correct |
37 |
Correct |
117 ms |
18928 KB |
Output is correct |
38 |
Correct |
90 ms |
19060 KB |
Output is correct |
39 |
Correct |
90 ms |
18452 KB |
Output is correct |
40 |
Correct |
92 ms |
18072 KB |
Output is correct |
41 |
Correct |
88 ms |
18060 KB |
Output is correct |
42 |
Correct |
89 ms |
18740 KB |
Output is correct |
43 |
Correct |
87 ms |
18364 KB |
Output is correct |
44 |
Correct |
92 ms |
18624 KB |
Output is correct |
45 |
Correct |
96 ms |
18188 KB |
Output is correct |
46 |
Correct |
88 ms |
17856 KB |
Output is correct |
47 |
Correct |
103 ms |
17792 KB |
Output is correct |
48 |
Correct |
90 ms |
18208 KB |
Output is correct |
49 |
Correct |
91 ms |
19904 KB |
Output is correct |
50 |
Correct |
79 ms |
18876 KB |
Output is correct |
51 |
Correct |
16 ms |
16596 KB |
Output is correct |
52 |
Correct |
91 ms |
24692 KB |
Output is correct |
53 |
Correct |
97 ms |
17600 KB |
Output is correct |
54 |
Correct |
87 ms |
17528 KB |
Output is correct |
55 |
Correct |
92 ms |
18880 KB |
Output is correct |
56 |
Correct |
83 ms |
17364 KB |
Output is correct |
57 |
Correct |
65 ms |
17124 KB |
Output is correct |
58 |
Correct |
2 ms |
10332 KB |
Output is correct |
59 |
Correct |
2 ms |
10332 KB |
Output is correct |
60 |
Correct |
2 ms |
10332 KB |
Output is correct |
61 |
Correct |
3 ms |
10332 KB |
Output is correct |
62 |
Correct |
2 ms |
10332 KB |
Output is correct |
63 |
Correct |
2 ms |
10332 KB |
Output is correct |
64 |
Correct |
2 ms |
10332 KB |
Output is correct |
65 |
Correct |
93 ms |
21244 KB |
Output is correct |
66 |
Correct |
87 ms |
18884 KB |
Output is correct |
67 |
Correct |
86 ms |
22080 KB |
Output is correct |
68 |
Correct |
16 ms |
16588 KB |
Output is correct |
69 |
Correct |
2 ms |
10332 KB |
Output is correct |
70 |
Correct |
2 ms |
10332 KB |
Output is correct |
71 |
Correct |
2 ms |
10332 KB |
Output is correct |
72 |
Correct |
2 ms |
10500 KB |
Output is correct |
73 |
Correct |
3 ms |
10500 KB |
Output is correct |
74 |
Correct |
3 ms |
10328 KB |
Output is correct |
75 |
Correct |
3 ms |
10328 KB |
Output is correct |
76 |
Correct |
2 ms |
10332 KB |
Output is correct |
77 |
Correct |
2 ms |
10332 KB |
Output is correct |
78 |
Correct |
3 ms |
10492 KB |
Output is correct |
79 |
Correct |
2 ms |
10332 KB |
Output is correct |
80 |
Correct |
2 ms |
10332 KB |
Output is correct |
81 |
Correct |
3 ms |
10432 KB |
Output is correct |
82 |
Correct |
2 ms |
10332 KB |
Output is correct |
83 |
Correct |
3 ms |
10332 KB |
Output is correct |