Submission #680539

#TimeUsernameProblemLanguageResultExecution timeMemory
680539Cross_RatioLove Polygon (BOI18_polygon)C++14
0 / 100
200 ms14824 KiB
#include <bits/stdc++.h>
using namespace std;
map<string, int> M;
vector<vector<int>> adj;
int type[100005];
bool vis[100005];
vector<int> V;
int no1 = -1, no2 = -1;
array<int, 2> dfs(int c, int p) {
    int d0 = 0, d1 = -1e9;
    int sum = 0;
    vis[c] = true;
    V.push_back(c);
    int cnt = 0;
    for(int n : adj[c]) {
        if(n==p || vis[n]) continue;
        if(c==no1&&n==no2) continue;
        if(c==no2&&n==no1) continue;
        array<int, 2> k = dfs(n, c);
        d0 += max(k[0], k[1]);
        sum += k[1];
        d1 = max(d1, k[0] - k[1]);
        cnt++;
    }
    d1 = sum + d1;
    if(cnt==0) return {0, 0};
    return {d0, 1 + d1};
}
bool vis2[100005];
array<int, 2> dfs2(int c, int p) {
    vis2[c] = true;
    for(int n :adj[c]) {
        if(n==p) continue;
        if(n==c) return {-2, -2};
        if(vis2[n]) return {c, n};
        array<int, 2> k = dfs2(n, c);
        if(k[0]==-2) return {-2, -2};
        if(k[0]>=0) return k;
    }
    return {-1, -1};
}
signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int N;
    cin >> N;
    int cnt = 0;
    int i, j;
    adj.resize(N);
    for(i=0;i<N;i++) {
        string s1, s2;
        cin >> s1 >> s2;
        int v1 = -1, v2 = -1;
        if(M.count(s1)) v1 = M[s1];
        else {
            M[s1] = v1 = cnt;
            cnt++;
        }
        if(M.count(s2)) v2 = M[s2];
        else {
            M[s2] = v2 = cnt;
            cnt++;
        }
        adj[v1].push_back(v2);
        adj[v2].push_back(v1);
    }
    if(N%2==1) {
        cout << "-1";
        return 0;
    }
    for(i=0;i<N;i++) type[i] = -1;
    int ans = 0;
    for(i=0;i<N;i++) {
        if(!vis2[i]) {
            array<int, 2> backedge = dfs2(i, -1);
            int x = backedge[0], y = backedge[1];
            if(x==-2) {
                array<int, 2> k = dfs(i, -1);
                ans += max(k[0], k[1]);
                continue;
            }
            assert(x!=-1);
            V.clear();
            vis[x] = vis[y] = true;
            int ma = 0;
            int val = 0;
            no1 = no2 = -1;
            for(int n : adj[y]) {
                if(!vis[n]) {
                    array<int, 2> k = dfs(n, -1);
                    val += max(k[0], k[1]);
                }
            }
            for(int n : adj[x]) {
                if(!vis[n]) {
                    array<int, 2> k = dfs(n, -1);
                    val += max(k[0], k[1]);
                }
            }
            val++;
            ma = max(ma, val);
            no1 = i, no2 = x;
            vis[x] = vis[y] = false;
            for(int k : V) vis[k] = false;
            V.clear();
            val = 0;
            if(!vis[x]) {
                array<int, 2> k = dfs(x, -1);
                val += max(k[0], k[1]);
            }
            if(!vis[y]) {
                array<int, 2> k = dfs(y, -1);
                val += max(k[0], k[1]);
            }
            V.clear();
            ma = max(ma, val);
            ans += ma;
        }
    }
    cout << N - ans;
}

Compilation message (stderr)

polygon.cpp: In function 'int main()':
polygon.cpp:49:12: warning: unused variable 'j' [-Wunused-variable]
   49 |     int i, j;
      |            ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...