#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i,a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define pii pair<int, int>
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define f first
#define s second
#define pb push_back
#define endl '\n'
const ll maxn = 1e5+5;
const ll inf = (1ll<<60);
vector<int> graph(maxn);
vector<int> rgraph[maxn];
vector<bool> occ(maxn);
int dpt[maxn][2];
int dpc[maxn][3];
vector<bool> incyc(maxn);
vector<int> siz(maxn);
vector<int> cycs;
void dfs(int x){
if (incyc[graph[x]]) {
incyc[x]=1;
return;
}
if (occ[graph[x]]==0) {
occ[graph[x]]=1;
dfs(graph[x]);
incyc[x]=1;
}
else {
cycs.pb(x);
incyc[x]=1;
}
}
void dfs2(int x){
int mn=0;
REP(i, SZ(rgraph[x])){
if (incyc[rgraph[x][i]]) continue;
dfs2(rgraph[x][i]);
dpt[x][0]+=dpt[rgraph[x][i]][1];
dpt[x][1]+=dpt[rgraph[x][i]][1];
mn=min(mn, dpt[rgraph[x][i]][0]-dpt[rgraph[x][i]][1]);
}
dpt[x][1]+=mn+1;
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n; cin>>n;
vector<string> pos(n);
vector<pair<string, string> > Graph(n);
REP(i, n){
cin>>Graph[i].f>>Graph[i].s;
pos[i]=Graph[i].f;
}
sort(ALL(pos));
REP(i, n){
int a=upper_bound(ALL(pos), Graph[i].f)-pos.begin();
int b=upper_bound(ALL(pos), Graph[i].s)-pos.begin();
graph[a]=b;
rgraph[b].pb(a);
}
REP1(i, n){
if (!occ[i]){
occ[i]=1;
dfs(i);
}
}
int ans=0;
fill(ALL(incyc), false);
REP(i, SZ(cycs)){
int cur = cycs[i];
//cout<<cur<<endl;
vector<pii> tmp;
incyc[cur]=1;
cur=graph[cur];
while(cur!=cycs[i]){
incyc[cur]=1;
cur=graph[cur];
}
dfs2(cur);
tmp.pb({dpt[cur][0], dpt[cur][1]});
cur=graph[cur];
while(cur!=cycs[i]){
dfs2(cur);
tmp.pb({dpt[cur][0], dpt[cur][1]});
cur=graph[cur];
}
if (SZ(tmp)==1){
ans+=tmp[0].s;
}
else if (SZ(tmp)==2){
ans+=min(tmp[0].f+tmp[1].f, tmp[0].s+tmp[1].s);
}
else {
int cans = inf;
// first is 0
dpc[0][0]=tmp[0].f;
dpc[0][1]=dpc[0][2]=inf;
REP1(j, SZ(tmp)-1){
dpc[j][0]=min(inf, min(dpc[j-1][1], dpc[j-1][2])+tmp[j].f);
dpc[j][1]=min(inf, min(dpc[j-1][1], dpc[j-1][2])+tmp[j].s);
dpc[j][2]=min(inf, dpc[j-1][0]+tmp[j].f+1);
}
cans=min({cans, dpc[SZ(tmp)-1][1], dpc[SZ(tmp)-1][2]});
// second is 1
dpc[0][1]=tmp[0].s;
dpc[0][0]=dpc[0][2]=inf;
REP1(j, SZ(tmp)-1){
dpc[j][0]=min(inf, min(dpc[j-1][1], dpc[j-1][2])+tmp[j].f);
dpc[j][1]=min(inf, min(dpc[j-1][1], dpc[j-1][2])+tmp[j].s);
dpc[j][2]=min(inf, dpc[j-1][0]+tmp[j].f+1);
}
cans=min({cans, dpc[SZ(tmp)-1][1], dpc[SZ(tmp)-1][2]});
// third is 2
dpc[0][2]=tmp[0].f+1;
dpc[0][0]=dpc[0][1]=inf;
REP1(j, SZ(tmp)-1){
dpc[j][0]=min(inf, min(dpc[j-1][1], dpc[j-1][2])+tmp[j].f);
dpc[j][1]=min(inf, min(dpc[j-1][1], dpc[j-1][2])+tmp[j].s);
dpc[j][2]=min(inf, dpc[j-1][0]+tmp[j].f+1);
}
cans=min(cans, dpc[SZ(tmp)-1][0]);
ans+=cans;
}
}
if (n%2==1) ans=-1;
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4196 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4284 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
3 ms |
4280 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4284 KB |
Output is correct |
15 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
122 ms |
26696 KB |
Output is correct |
5 |
Correct |
130 ms |
20760 KB |
Output is correct |
6 |
Correct |
144 ms |
27592 KB |
Output is correct |
7 |
Correct |
126 ms |
24648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
19812 KB |
Output is correct |
2 |
Correct |
127 ms |
21700 KB |
Output is correct |
3 |
Correct |
142 ms |
21308 KB |
Output is correct |
4 |
Correct |
128 ms |
19292 KB |
Output is correct |
5 |
Correct |
136 ms |
27740 KB |
Output is correct |
6 |
Correct |
120 ms |
19156 KB |
Output is correct |
7 |
Correct |
120 ms |
19284 KB |
Output is correct |
8 |
Correct |
125 ms |
19920 KB |
Output is correct |
9 |
Correct |
110 ms |
18900 KB |
Output is correct |
10 |
Correct |
100 ms |
18404 KB |
Output is correct |
11 |
Correct |
3 ms |
4180 KB |
Output is correct |
12 |
Correct |
3 ms |
4280 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
3 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
2 ms |
4196 KB |
Output is correct |
3 |
Correct |
3 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
2 ms |
4284 KB |
Output is correct |
6 |
Correct |
3 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
3 ms |
4280 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4284 KB |
Output is correct |
15 |
Correct |
2 ms |
4180 KB |
Output is correct |
16 |
Correct |
3 ms |
4180 KB |
Output is correct |
17 |
Correct |
2 ms |
4180 KB |
Output is correct |
18 |
Correct |
2 ms |
4180 KB |
Output is correct |
19 |
Correct |
122 ms |
26696 KB |
Output is correct |
20 |
Correct |
130 ms |
20760 KB |
Output is correct |
21 |
Correct |
144 ms |
27592 KB |
Output is correct |
22 |
Correct |
126 ms |
24648 KB |
Output is correct |
23 |
Correct |
146 ms |
19812 KB |
Output is correct |
24 |
Correct |
127 ms |
21700 KB |
Output is correct |
25 |
Correct |
142 ms |
21308 KB |
Output is correct |
26 |
Correct |
128 ms |
19292 KB |
Output is correct |
27 |
Correct |
136 ms |
27740 KB |
Output is correct |
28 |
Correct |
120 ms |
19156 KB |
Output is correct |
29 |
Correct |
120 ms |
19284 KB |
Output is correct |
30 |
Correct |
125 ms |
19920 KB |
Output is correct |
31 |
Correct |
110 ms |
18900 KB |
Output is correct |
32 |
Correct |
100 ms |
18404 KB |
Output is correct |
33 |
Correct |
3 ms |
4180 KB |
Output is correct |
34 |
Correct |
3 ms |
4280 KB |
Output is correct |
35 |
Correct |
2 ms |
4180 KB |
Output is correct |
36 |
Correct |
3 ms |
4180 KB |
Output is correct |
37 |
Correct |
132 ms |
20316 KB |
Output is correct |
38 |
Correct |
132 ms |
20756 KB |
Output is correct |
39 |
Correct |
125 ms |
19412 KB |
Output is correct |
40 |
Correct |
112 ms |
19168 KB |
Output is correct |
41 |
Correct |
132 ms |
19144 KB |
Output is correct |
42 |
Correct |
129 ms |
19408 KB |
Output is correct |
43 |
Correct |
116 ms |
19552 KB |
Output is correct |
44 |
Correct |
115 ms |
19532 KB |
Output is correct |
45 |
Correct |
119 ms |
19508 KB |
Output is correct |
46 |
Correct |
130 ms |
19540 KB |
Output is correct |
47 |
Correct |
106 ms |
18912 KB |
Output is correct |
48 |
Correct |
139 ms |
19728 KB |
Output is correct |
49 |
Correct |
127 ms |
21644 KB |
Output is correct |
50 |
Correct |
112 ms |
21320 KB |
Output is correct |
51 |
Correct |
117 ms |
19208 KB |
Output is correct |
52 |
Correct |
131 ms |
27724 KB |
Output is correct |
53 |
Correct |
121 ms |
19156 KB |
Output is correct |
54 |
Correct |
124 ms |
19284 KB |
Output is correct |
55 |
Correct |
115 ms |
19928 KB |
Output is correct |
56 |
Correct |
103 ms |
19004 KB |
Output is correct |
57 |
Correct |
98 ms |
18248 KB |
Output is correct |
58 |
Correct |
2 ms |
4180 KB |
Output is correct |
59 |
Correct |
2 ms |
4280 KB |
Output is correct |
60 |
Correct |
2 ms |
4284 KB |
Output is correct |
61 |
Correct |
2 ms |
4180 KB |
Output is correct |
62 |
Correct |
2 ms |
4284 KB |
Output is correct |
63 |
Correct |
2 ms |
4180 KB |
Output is correct |
64 |
Correct |
2 ms |
4180 KB |
Output is correct |
65 |
Correct |
120 ms |
26700 KB |
Output is correct |
66 |
Correct |
134 ms |
20800 KB |
Output is correct |
67 |
Correct |
118 ms |
27588 KB |
Output is correct |
68 |
Correct |
119 ms |
24600 KB |
Output is correct |
69 |
Correct |
2 ms |
4180 KB |
Output is correct |
70 |
Correct |
2 ms |
4180 KB |
Output is correct |
71 |
Correct |
2 ms |
4180 KB |
Output is correct |
72 |
Correct |
2 ms |
4260 KB |
Output is correct |
73 |
Correct |
2 ms |
4284 KB |
Output is correct |
74 |
Correct |
4 ms |
4180 KB |
Output is correct |
75 |
Correct |
3 ms |
4284 KB |
Output is correct |
76 |
Correct |
3 ms |
4284 KB |
Output is correct |
77 |
Correct |
3 ms |
4180 KB |
Output is correct |
78 |
Correct |
2 ms |
4280 KB |
Output is correct |
79 |
Correct |
3 ms |
4180 KB |
Output is correct |
80 |
Correct |
2 ms |
4284 KB |
Output is correct |
81 |
Correct |
3 ms |
4180 KB |
Output is correct |
82 |
Correct |
2 ms |
4188 KB |
Output is correct |
83 |
Correct |
2 ms |
4280 KB |
Output is correct |