Submission #535850

# Submission time Handle Problem Language Result Execution time Memory
535850 2022-03-11T14:31:23 Z perchuts Love Polygon (BOI18_polygon) C++17
29 / 100
289 ms 24524 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], ans;
vector<int>g[maxn];
int vis[maxn], mark[maxn], mark2[maxn][2], dp[maxn][2];
int dfs(int u, bool mode){
    if(mark2[u][mode])return dp[u][mode];
    ckmax(vis[u],1);
    mark2[u][mode] = 1;
    ckmax(vis[u],1);
    for(auto v:g[u]){
        if(vis[v]!=2){
            if(mode)dp[u][1] += dfs(v,0);
            else dp[u][0] += max(dfs(v,0),dfs(v,1));
        }
    }
    return dp[u][mode] = dp[u][mode] + mode;
}

int main(){_
    int n;cin>>n;
    if(n&1){
        cout<<-1<<endl;
        return 0;
    }
    int q = 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){
                vis[cur] = 2, cur = par[cur], ++cnt;
            }
            vis[start] = 2;
            if(cnt==2){
                mark[cur] = mark[par[cur]] = 1;
                for(auto v:g[cur])if(vis[v]!=2)ans += dfs(v,0);
                for(auto v:g[par[cur]])if(vis[v]!=2)ans += dfs(v,0);
            }else{
                cur = par[start];
                if(sz(g[start])==1&&cnt==1){
                    ans++;
                    continue;
                }
                if(sz(g[start])!=1){
                    if(dfs(start,1)>dfs(start,0))mark[start] = 1;
                    ans += max(dfs(start,1),dfs(start,0));
                }
                while(cur!=start){
                    if(sz(g[cur])!=1){
                        if(dfs(cur,1)>dfs(cur,0))mark[cur] = 1;
                        ans += max(dfs(cur,1),dfs(cur,0));
                    }
                    cur = par[cur];
                }
                cur = par[start]; 
                while(cur!=start&&sz(g[cur])==1)cur = par[cur];
                start = cur, cur = par[cur], cnt = 0;
                while(cur!=start){
                    if(sz(g[cur])!=1)ans += (cnt+1)/2, cnt = 0;
                    else cnt++;
                    cur = par[cur];
                }
                ans += (cnt+1)/2;
            }
        }
    }   
    cout<<ans<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Incorrect 2 ms 2644 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Incorrect 213 ms 14368 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 272 ms 15468 KB Output is correct
2 Correct 289 ms 17564 KB Output is correct
3 Correct 164 ms 14368 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 282 ms 24524 KB Output is correct
6 Correct 216 ms 14412 KB Output is correct
7 Correct 210 ms 14676 KB Output is correct
8 Correct 239 ms 15060 KB Output is correct
9 Correct 193 ms 13956 KB Output is correct
10 Correct 139 ms 13172 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2644 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 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Incorrect 2 ms 2644 KB Output isn't correct
5 Halted 0 ms 0 KB -