Submission #939933

# Submission time Handle Problem Language Result Execution time Memory
939933 2024-03-07T01:33:50 Z Warinchai Love Polygon (BOI18_polygon) C++14
29 / 100
304 ms 25172 KB
#include<bits/stdc++.h>
using namespace std;
map<string,int>mp;
vector<int>v[100005];
vector<int>rt;
int dp[100005][2];
int dp2[100005][2];
int sz[100005];
int vis[100005];
int lp=0;
int p[100005];
int com[100005];
int fp(int a){return p[a]==a?a:p[a]=fp(p[a]);}
void un(int a,int b){p[fp(b)]=fp(a);}
void dfs(int u){
    //cerr<<"node"<<u<<"\n";
    if(vis[u])return;
    vis[u]=1;
    int sum=0;
    sz[u]=1;
    for(auto x:v[u]){
        if(vis[x])continue;
        dfs(x);
        sz[u]+=sz[x];
        sum+=dp[x][0];
    }
    dp[u][1]=sum;
    for(auto x:v[u])dp[u][0]=max(sum-dp[x][0]+dp[x][1]+1,dp[u][0]);
}
void dfs2(int u){
    if(u==lp)return;
    int sum=0;
    for(auto x:v[u]){
        
        dfs2(x);
        sum+=dp2[x][0];
    }
    dp2[u][1]=sum;
    for(auto x:v[u])dp2[u][0]=max(sum-dp2[x][0]+dp2[x][1]+1,dp2[u][0]);
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;cin>>n;
    int cur=0;
    for(int i=1;i<=n;i++)p[i]=i;
    for(int i=0;i<n;i++){
        string a,b;cin>>a>>b;
        if(mp[a]==0)cur++,mp[a]=cur;
        if(mp[b]==0)cur++,mp[b]=cur;
        //cerr<<a<<" "<<mp[a]<<","<<b<<" "<<mp[b]<<"\n";
        if(mp[a]==mp[b])rt.push_back(mp[a])/*,cerr<<"root\n"*/;
        else if(fp(mp[a])==fp(mp[b]))rt.push_back(mp[a]),com[mp[a]]=mp[b]/*,cerr<<"one loop\n"*/;
        else v[mp[b]].push_back(mp[a])/*,cerr<<"nothing\n"*/;
        un(mp[a],mp[b]);
    }
    if(n%2==1){
        cout<<"-1";
        return 0;
    }
    /*for(int i=1;i<=n;i++){
        cerr<<i<<": ";
        for(auto x:v[i])cerr<<x<<" ";
        cerr<<"\n";
    }*/
    int ans=0;
    //for(auto x:rt)cerr<<x<<" ";
    //cerr<<"\n";
    for(auto x:rt){
        //cerr<<x<<":\n";
        dfs(x);
        //cerr<<dp[x][0]<<" "<<dp[x][1]<<"\n";
        int temp=(max(dp[x][0],dp[x][1])+sz[x]-2*max(dp[x][0],dp[x][1]));
        //cerr<<"temp:"<<temp<<"\n";
        if(com[x]!=0){
            //cerr<<"loop\n";
            lp=com[x];
            //cerr<<lp<<"\n";
            dfs2(x);
            int pp=dp2[x][1]+dp[lp][1]+1;
            if(sz[x]!=2)ans+=min(temp,pp+(sz[x]-pp*2));
        }else ans+=(max(dp[x][0],dp[x][1])+sz[x]-2*max(dp[x][0],dp[x][1]));
        //cerr<<max(dp[x][0],dp[x][1])<<"\n";
    }
    cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5720 KB Output is correct
2 Correct 2 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Correct 2 ms 5776 KB Output is correct
5 Correct 1 ms 5724 KB Output is correct
6 Correct 2 ms 5728 KB Output is correct
7 Incorrect 2 ms 5768 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5720 KB Output is correct
2 Correct 2 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Incorrect 304 ms 24652 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 268 ms 16980 KB Output is correct
2 Correct 292 ms 19056 KB Output is correct
3 Correct 174 ms 15080 KB Output is correct
4 Correct 272 ms 16320 KB Output is correct
5 Correct 277 ms 25172 KB Output is correct
6 Correct 251 ms 16320 KB Output is correct
7 Correct 284 ms 16428 KB Output is correct
8 Correct 214 ms 16076 KB Output is correct
9 Correct 240 ms 15956 KB Output is correct
10 Correct 177 ms 15008 KB Output is correct
11 Correct 1 ms 5720 KB Output is correct
12 Correct 1 ms 5720 KB Output is correct
13 Correct 2 ms 5556 KB Output is correct
14 Correct 1 ms 5724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5720 KB Output is correct
2 Correct 2 ms 5724 KB Output is correct
3 Correct 2 ms 5724 KB Output is correct
4 Correct 2 ms 5776 KB Output is correct
5 Correct 1 ms 5724 KB Output is correct
6 Correct 2 ms 5728 KB Output is correct
7 Incorrect 2 ms 5768 KB Output isn't correct
8 Halted 0 ms 0 KB -