Submission #632260

# Submission time Handle Problem Language Result Execution time Memory
632260 2022-08-19T19:21:20 Z jasmin Love Polygon (BOI18_polygon) C++14
75 / 100
292 ms 48728 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long

struct unionfind{
    vector<int> chef;
    vector<int> cnt;

    unionfind(int n){
        chef.resize(n);
        iota(chef.begin(), chef.end(), 0);
        cnt.resize(n, 1);
    }

    int find(int a){
        if(chef[a]==a) return a;
        return chef[a]=find(chef[a]);
    }
    void unite(int a, int b){
        if(find(a)==find(b)) return;
        cnt[find(b)]+=cnt[find(a)];
        chef[find(a)]=find(b);
    }
};

void dfs(int v, vector<vector<int> >& adi, vector<bool>& vis, unionfind& ufind, vector<int>& postorder, bool first){
    assert(!vis[v]);
    vis[v]=true;

    for(auto u: adi[v]){
        if(!vis[u]){
            dfs(u, adi, vis, ufind, postorder, first);
            if(!first){
                ufind.unite(u, v);
            }
        }
    }
    if(first){
        postorder.push_back(v);
    }
}

void kosaraju(int n, vector<vector<int> >& adi, vector<vector<int> >& rev, unionfind& ufind){
    vector<int> postorder;
    vector<bool> vis(n);
    for(int v=0; v<n; v++){
        if(!vis[v]) {
            dfs(v, adi, vis, ufind, postorder, true);
        }
    }
    vis.assign(n, false);
    for(int v=n-1; v>=0; v--){
        if(!vis[postorder[v]]) {
            dfs(postorder[v], rev, vis, ufind, postorder, false);
        }
    }
}

void dfs_dp(int v, vector<vector<int> >& adi, vector<vector<int> >& dp, vector<bool>& vis, unionfind& ufind){
    if(vis[v]) return;
    vis[v]=true;

    int add=0;
    if(ufind.cnt[v]!=2){
        add=(ufind.cnt[v]+1)/2;
    }

    int sum0=0;
    for(auto u: adi[v]){
        dfs_dp(u, adi, dp, vis, ufind);
        sum0+=dp[u][0];
    }
    dp[v][1]=sum0;
    dp[v][0]=sum0+add;

    for(auto u: adi[v]){
        if(ufind.cnt[v]%2==1){
            dp[v][0]=min(dp[v][0], sum0-dp[u][0]+dp[u][1]+add);
        }
    }

    //cout << v << " " << ufind.find(v) << " " << ufind.cnt[ufind.find(v)] << "\n";
    //cout << dp[v][0] << " " << dp[v][1] << "\n";
}

int solve(int n, vector<vector<int> >& adi_alt, vector<vector<int> >& rev, vector<int>& par){
    unionfind ufind(n);
    kosaraju(n, adi_alt, rev, ufind);

    vector<vector<int> > adi(n);
    for(int u=0; u<n; u++){
        int v=par[u];
        if(v==-1) continue;
        if(ufind.find(u)!=ufind.find(v)){
            adi[ufind.find(v)].push_back(ufind.find(u));
        }
    }

    /*for(int i=0; i<n; i++){
        cout << i << ": " << ufind.find(i) << "\n";
    }
    for(int i=0; i<n; i++){
        if(ufind.find(i)==i){
            cout << i << ": ";
            for(auto e: adi[i]){
                cout << e << " ";
            }
            cout << "\n";
        }
    }*/

    vector<bool> vis(n);
    vector<vector<int> > dp(n, vector<int> (2));
    int ans=0;
    for(int v=0; v<n; v++){
        if(vis[ufind.find(v)]) continue;

        if(ufind.find(v)!=v || par[v]==-1){
            dfs_dp(ufind.find(v), adi, dp, vis, ufind);
            ans+=dp[ufind.find(v)][0];
        }
    }
    return ans;
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    int cnt=1;
    map<string, int> ind;
    vector<vector<int> > adi(n);
    vector<vector<int> > rev(n);
    vector<int> par(n, -1);
    for(int i=0; i<n; i++){
        string s, t;
        cin >> s >> t;
        if(ind[s]==0){
            ind[s]=cnt;
            cnt++;
        }
        if(ind[t]==0){
            ind[t]=cnt;
            cnt++;
        }

        int a=ind[s];
        int b=ind[t];
        if(a!=b){
            adi[b-1].push_back(a-1);
            rev[a-1].push_back(b-1);
            par[a-1]=b-1;
        }
    }

    if(n%2==1){
        cout << -1 << "\n";
        return 0;
    }

    cout << solve(n, adi, rev, par) << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 271 ms 46348 KB Output is correct
5 Correct 275 ms 31292 KB Output is correct
6 Correct 280 ms 48728 KB Output is correct
7 Correct 233 ms 21964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 284 ms 33032 KB Output is correct
2 Correct 291 ms 36056 KB Output is correct
3 Correct 161 ms 25280 KB Output is correct
4 Correct 220 ms 20704 KB Output is correct
5 Correct 292 ms 44604 KB Output is correct
6 Correct 267 ms 32096 KB Output is correct
7 Correct 248 ms 31996 KB Output is correct
8 Correct 207 ms 29104 KB Output is correct
9 Correct 243 ms 31356 KB Output is correct
10 Correct 149 ms 29816 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 320 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 271 ms 46348 KB Output is correct
20 Correct 275 ms 31292 KB Output is correct
21 Correct 280 ms 48728 KB Output is correct
22 Correct 233 ms 21964 KB Output is correct
23 Correct 284 ms 33032 KB Output is correct
24 Correct 291 ms 36056 KB Output is correct
25 Correct 161 ms 25280 KB Output is correct
26 Correct 220 ms 20704 KB Output is correct
27 Correct 292 ms 44604 KB Output is correct
28 Correct 267 ms 32096 KB Output is correct
29 Correct 248 ms 31996 KB Output is correct
30 Correct 207 ms 29104 KB Output is correct
31 Correct 243 ms 31356 KB Output is correct
32 Correct 149 ms 29816 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 0 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Incorrect 275 ms 34164 KB Output isn't correct
38 Halted 0 ms 0 KB -