Submission #535914

# Submission time Handle Problem Language Result Execution time Memory
535914 2022-03-11T19:44:40 Z perchuts Love Polygon (BOI18_polygon) C++17
29 / 100
253 ms 33640 KB
#include <bits/stdc++.h>
#define maxn (int)(1e5+51)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define ll long long
#define pb push_back
#define ull unsigned long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define inf 2000000001
#define mod 1000000007 //998244353
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
 
using namespace std;
 
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
 
map<string,int>mp;
int par[maxn];
vector<int>g[maxn];
int vis[maxn], mark[maxn][2], dp[maxn][2];
 
int countnodes(int u){
    vis[u] = 1;
    int resp = 1;
    for(auto v:g[u]){
        resp += countnodes(v);
    }
    return resp;
}

int dfs(int u,bool mode){
    if(mark[u][mode])return dp[u][mode];
    mark[u][mode] = 1;
    if(mode)dp[u][mode] = 1;
    else{
        dp[u][mode] = dfs(u,1) - 1;
    }
    int best = 0;
    for(auto v:g[u]){
        if(mode){
            dp[u][mode] += dfs(v,0);
        }else{
            ckmax(best,dfs(v,1) - dfs(v,0));
        }
    }
    dp[u][mode] += best;
    return dp[u][mode];
}



int main(){_
    int n;cin>>n;
    if(n&1){
        cout<<-1<<endl;
        return 0;
    }
    int q = 0, resp = 0;
    for(int i=1;i<=n;++i){
        string a,b;cin>>a>>b;
        if(mp.find(a)==mp.end())mp[a]=++q;
        if(mp.find(b)==mp.end())mp[b]=++q;
        int u = mp[a], v = mp[b];
        par[u] = v;
        g[v].pb(u);
    }   

    for(int i=1;i<=n;++i){
        if(!vis[i]){
            int cur = i;
            while(!vis[cur])vis[cur] = 1, cur = par[cur];
            int start = cur, cnt = 1;
            cur = par[cur];
            while(cur!=start)cur = par[cur], ++cnt;
            if(cnt!=2){
                //start can either be picked, or not be picked.
                //if a number is being picked, it means that it is pairing with its father, i.e.
                //its father cant be picked.
                //then problem turns out to count the maximum independent set.
                for(int i=0;i<sz(g[par[start]]);++i){
                    if(g[par[start]][i]==start)g[par[start]].erase(g[par[start]].begin()+i);
                }
                int nodes = countnodes(start);//count num of nodes in connected component
                //first, start is not picked. this means that we can remove the edge going from start->par[start]
                int ans = dfs(start,0);
                //second, start is picked. then, remove out-going edges from start and par[start]. and repeat the dp.
                cur = par[start];
                if(cnt!=1)ckmax(ans,dfs(start,1));
                resp += nodes - ans;
            }
        }
    }   
 
    cout<<resp<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2692 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Incorrect 2 ms 2644 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Incorrect 252 ms 32480 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 228 ms 15576 KB Output is correct
2 Correct 237 ms 19872 KB Output is correct
3 Correct 159 ms 16136 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 253 ms 33640 KB Output is correct
6 Correct 207 ms 14768 KB Output is correct
7 Correct 205 ms 14796 KB Output is correct
8 Correct 189 ms 15048 KB Output is correct
9 Correct 183 ms 14216 KB Output is correct
10 Correct 144 ms 13516 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2684 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2692 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 3 ms 2644 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Incorrect 2 ms 2644 KB Output isn't correct
8 Halted 0 ms 0 KB -