Submission #676523

# Submission time Handle Problem Language Result Execution time Memory
676523 2022-12-31T06:17:32 Z vjudge1 Love Polygon (BOI18_polygon) C++17
100 / 100
126 ms 27744 KB
#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 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 2 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 2 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 2 ms 4180 KB Output is correct
10 Correct 3 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 4180 KB Output is correct
15 Correct 3 ms 4264 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 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 117 ms 24664 KB Output is correct
5 Correct 121 ms 18772 KB Output is correct
6 Correct 123 ms 25532 KB Output is correct
7 Correct 117 ms 22612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 17740 KB Output is correct
2 Correct 121 ms 21712 KB Output is correct
3 Correct 112 ms 21340 KB Output is correct
4 Correct 113 ms 19140 KB Output is correct
5 Correct 123 ms 27724 KB Output is correct
6 Correct 113 ms 19140 KB Output is correct
7 Correct 117 ms 19160 KB Output is correct
8 Correct 117 ms 19876 KB Output is correct
9 Correct 105 ms 18884 KB Output is correct
10 Correct 80 ms 18244 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 4284 KB Output is correct
14 Correct 3 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 2 ms 4180 KB Output is correct
5 Correct 2 ms 4180 KB Output is correct
6 Correct 2 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 2 ms 4180 KB Output is correct
10 Correct 3 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 4180 KB Output is correct
15 Correct 3 ms 4264 KB Output is correct
16 Correct 2 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 117 ms 24664 KB Output is correct
20 Correct 121 ms 18772 KB Output is correct
21 Correct 123 ms 25532 KB Output is correct
22 Correct 117 ms 22612 KB Output is correct
23 Correct 120 ms 17740 KB Output is correct
24 Correct 121 ms 21712 KB Output is correct
25 Correct 112 ms 21340 KB Output is correct
26 Correct 113 ms 19140 KB Output is correct
27 Correct 123 ms 27724 KB Output is correct
28 Correct 113 ms 19140 KB Output is correct
29 Correct 117 ms 19160 KB Output is correct
30 Correct 117 ms 19876 KB Output is correct
31 Correct 105 ms 18884 KB Output is correct
32 Correct 80 ms 18244 KB Output is correct
33 Correct 2 ms 4180 KB Output is correct
34 Correct 2 ms 4180 KB Output is correct
35 Correct 2 ms 4284 KB Output is correct
36 Correct 3 ms 4180 KB Output is correct
37 Correct 123 ms 20172 KB Output is correct
38 Correct 118 ms 20544 KB Output is correct
39 Correct 117 ms 19396 KB Output is correct
40 Correct 115 ms 19168 KB Output is correct
41 Correct 121 ms 19144 KB Output is correct
42 Correct 122 ms 19460 KB Output is correct
43 Correct 120 ms 19532 KB Output is correct
44 Correct 121 ms 19484 KB Output is correct
45 Correct 122 ms 19652 KB Output is correct
46 Correct 118 ms 19532 KB Output is correct
47 Correct 115 ms 18984 KB Output is correct
48 Correct 117 ms 19736 KB Output is correct
49 Correct 124 ms 21708 KB Output is correct
50 Correct 111 ms 21336 KB Output is correct
51 Correct 117 ms 19144 KB Output is correct
52 Correct 121 ms 27608 KB Output is correct
53 Correct 118 ms 19252 KB Output is correct
54 Correct 116 ms 19152 KB Output is correct
55 Correct 117 ms 19912 KB Output is correct
56 Correct 126 ms 18832 KB Output is correct
57 Correct 82 ms 18296 KB Output is correct
58 Correct 2 ms 4180 KB Output is correct
59 Correct 2 ms 4180 KB Output is correct
60 Correct 2 ms 4180 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 4280 KB Output is correct
64 Correct 3 ms 4180 KB Output is correct
65 Correct 121 ms 26656 KB Output is correct
66 Correct 123 ms 20688 KB Output is correct
67 Correct 123 ms 27744 KB Output is correct
68 Correct 118 ms 24668 KB Output is correct
69 Correct 2 ms 4180 KB Output is correct
70 Correct 2 ms 4288 KB Output is correct
71 Correct 2 ms 4180 KB Output is correct
72 Correct 2 ms 4180 KB Output is correct
73 Correct 2 ms 4180 KB Output is correct
74 Correct 2 ms 4180 KB Output is correct
75 Correct 2 ms 4284 KB Output is correct
76 Correct 2 ms 4284 KB Output is correct
77 Correct 2 ms 4180 KB Output is correct
78 Correct 2 ms 4180 KB Output is correct
79 Correct 2 ms 4180 KB Output is correct
80 Correct 3 ms 4180 KB Output is correct
81 Correct 2 ms 4180 KB Output is correct
82 Correct 3 ms 4180 KB Output is correct
83 Correct 2 ms 4180 KB Output is correct