Submission #485581

# Submission time Handle Problem Language Result Execution time Memory
485581 2021-11-08T11:27:55 Z sam571128 Love Polygon (BOI18_polygon) C++17
54 / 100
267 ms 32196 KB
#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 1e5+5;
int visited[N], arr[N], sz[N], dp[N][2], col[N], ok[N], dp2[N][2], vis[N];
vector<int> adj[N];


void dfs(int u, int p, int no = -1){
    visited[u] = 1;
    sz[u] = 1;
    int sum = 0, sum2 = 0;
    for(auto v : adj[u]){
        if(v==p || visited[v] || (arr[u]==no&&v==no)) continue;
        dfs(v,u,no);
        sum += dp[v][0];
        sum2 += dp2[v][0];
        sz[u] += sz[v];
    }

    int mx = 0, mx2 = 0;
    for(auto v : adj[u]){
        if(v==p||(arr[u]==no&&v==no)) continue;
        mx = max(dp[v][1]-dp[v][0],mx);
        mx2 = max(dp2[v][1]-dp2[v][0],mx2);
    }


    dp[u][1] = sum+1;

    if(u!=no) dp2[u][1] = sum2+1;
    else dp2[u][1] = -1e18;

    dp[u][0] = mx+dp[u][1]-1;
    dp2[u][0] = max((int)0,mx2+dp2[u][1]-1);
}

int idx;
map<string,int> m;

int assign(string x){
    if(m.find(x)==m.end()){
        m[x] = idx++;
    }
    return m[x];
}

signed main(){
    fastio
    int n;
    cin >> n;

    for(int i = 0;i < n;i++){
        string a,b;
        cin >> a >> b;
        int x = assign(a);
        int y = assign(b);
        arr[x] = y;
        if(x==y) continue;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

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

    for(int i = 0; i < n; i++){
        if(arr[arr[i]]==i){
            if(adj[arr[i]].size()==2){
                ok[i] = 1;
                visited[arr[i]] = 1;
            }else{
                ok[arr[i]] = 1;
                visited[i] = 1;
            }  
        }
    }

    int ans = 0;
    for(int i = 0; i < n; i++){
        if(arr[i]==i){
            dfs(i,i);
            ans += sz[i]-dp[i][0];
        }
    }

    for(int i = 0; i < n; i++){
        if(!visited[i]){
            if(ok[i]){
                dfs(i,i);
                ans += sz[i]-dp[i][1];
            }else{
                int tmp = i;
                while(!vis[arr[tmp]]) tmp = arr[tmp], vis[arr[tmp]] = 1;
                dfs(tmp,tmp,arr[tmp]);
                ans += sz[tmp]-max(dp[tmp][0],dp2[tmp][1]);
            }
        }
    }


    cout << ans << "\n";
}   
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 1 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Incorrect 1 ms 2636 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 254 ms 28612 KB Output is correct
5 Correct 267 ms 22668 KB Output is correct
6 Correct 229 ms 32196 KB Output is correct
7 Correct 213 ms 16752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 218 ms 20200 KB Output is correct
2 Correct 221 ms 21740 KB Output is correct
3 Correct 149 ms 16688 KB Output is correct
4 Correct 193 ms 14892 KB Output is correct
5 Correct 226 ms 29180 KB Output is correct
6 Correct 219 ms 19768 KB Output is correct
7 Correct 210 ms 20292 KB Output is correct
8 Correct 179 ms 19064 KB Output is correct
9 Correct 206 ms 19896 KB Output is correct
10 Correct 151 ms 20012 KB Output is correct
11 Correct 1 ms 2636 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 1 ms 2636 KB Output is correct
14 Correct 1 ms 2636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 1 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 1 ms 2636 KB Output is correct
8 Correct 2 ms 2636 KB Output is correct
9 Correct 1 ms 2636 KB Output is correct
10 Incorrect 1 ms 2636 KB Output isn't correct
11 Halted 0 ms 0 KB -