Submission #940050

# Submission time Handle Problem Language Result Execution time Memory
940050 2024-03-07T04:46:42 Z Warinchai Love Polygon (BOI18_polygon) C++14
100 / 100
319 ms 24972 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);}
int lv[100005];
void dfs(int u,int rt){
    //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,rt);
        sz[u]+=sz[x];
        sum+=dp[x][0];
    }
    dp[u][1]=sum;
    for(auto x:v[u])if(x!=rt)dp[u][0]=max(sum-dp[x][0]+dp[x][1]+1,dp[u][0]);
}
void dfs2(int u,int p=0){
    lv[u]=lv[p]+1;
    if(u==lp)return;
    int sum=0;
    for(auto x:v[u]){
        dfs2(x,u);
        sum+=dp2[x][0];
    }
    dp2[u][1]=sum;
    for(auto x:v[u])if(x!=lp)dp2[u][0]=max(sum-dp2[x][0]+dp2[x][1]+1,dp2[u][0]);
    //cerr<<u<<" "<<dp[u][1]<<"\n";
}
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,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";
            //cerr<<"info:\n";
            dfs2(x);
            //cerr<<"info:\n";
            ////for(int i=0;i<n;i++)cerr<<dp2[i][1]<<" ";
            //cerr<<"\n";
            //cerr<<dp2[x][1]<<" "<<dp[lp][1]<<"\n";
            int pp=dp2[x][1]+dp[lp][1]+1;
            //cerr<<pp<<"\n";
            //cerr<<lv[lp]<<"\n";
            if(sz[x]!=2)ans+=min(temp,pp+(sz[x]-pp*2)-(lv[lp]==2?1:0));
        }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 1 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 1 ms 5980 KB Output is correct
7 Correct 2 ms 5976 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5976 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 2 ms 6128 KB Output is correct
14 Correct 1 ms 5980 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5976 KB Output is correct
2 Correct 2 ms 5976 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 278 ms 24028 KB Output is correct
5 Correct 259 ms 16724 KB Output is correct
6 Correct 280 ms 24912 KB Output is correct
7 Correct 246 ms 19032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 263 ms 16096 KB Output is correct
2 Correct 257 ms 18260 KB Output is correct
3 Correct 169 ms 14196 KB Output is correct
4 Correct 258 ms 17492 KB Output is correct
5 Correct 276 ms 24148 KB Output is correct
6 Correct 298 ms 15340 KB Output is correct
7 Correct 284 ms 15440 KB Output is correct
8 Correct 210 ms 15188 KB Output is correct
9 Correct 242 ms 14916 KB Output is correct
10 Correct 179 ms 14188 KB Output is correct
11 Correct 2 ms 5976 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 2 ms 5980 KB Output is correct
14 Correct 1 ms 5976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5980 KB Output is correct
2 Correct 2 ms 5980 KB Output is correct
3 Correct 2 ms 5980 KB Output is correct
4 Correct 2 ms 5980 KB Output is correct
5 Correct 1 ms 5980 KB Output is correct
6 Correct 1 ms 5980 KB Output is correct
7 Correct 2 ms 5976 KB Output is correct
8 Correct 2 ms 5980 KB Output is correct
9 Correct 2 ms 5980 KB Output is correct
10 Correct 2 ms 5976 KB Output is correct
11 Correct 2 ms 5980 KB Output is correct
12 Correct 2 ms 5980 KB Output is correct
13 Correct 2 ms 6128 KB Output is correct
14 Correct 1 ms 5980 KB Output is correct
15 Correct 2 ms 5980 KB Output is correct
16 Correct 2 ms 5976 KB Output is correct
17 Correct 2 ms 5976 KB Output is correct
18 Correct 2 ms 5980 KB Output is correct
19 Correct 278 ms 24028 KB Output is correct
20 Correct 259 ms 16724 KB Output is correct
21 Correct 280 ms 24912 KB Output is correct
22 Correct 246 ms 19032 KB Output is correct
23 Correct 263 ms 16096 KB Output is correct
24 Correct 257 ms 18260 KB Output is correct
25 Correct 169 ms 14196 KB Output is correct
26 Correct 258 ms 17492 KB Output is correct
27 Correct 276 ms 24148 KB Output is correct
28 Correct 298 ms 15340 KB Output is correct
29 Correct 284 ms 15440 KB Output is correct
30 Correct 210 ms 15188 KB Output is correct
31 Correct 242 ms 14916 KB Output is correct
32 Correct 179 ms 14188 KB Output is correct
33 Correct 2 ms 5976 KB Output is correct
34 Correct 2 ms 5980 KB Output is correct
35 Correct 2 ms 5980 KB Output is correct
36 Correct 1 ms 5976 KB Output is correct
37 Correct 274 ms 16980 KB Output is correct
38 Correct 263 ms 16980 KB Output is correct
39 Correct 265 ms 15824 KB Output is correct
40 Correct 279 ms 15620 KB Output is correct
41 Correct 271 ms 15564 KB Output is correct
42 Correct 254 ms 15956 KB Output is correct
43 Correct 272 ms 15996 KB Output is correct
44 Correct 282 ms 16152 KB Output is correct
45 Correct 261 ms 15956 KB Output is correct
46 Correct 280 ms 16096 KB Output is correct
47 Correct 282 ms 15112 KB Output is correct
48 Correct 276 ms 16452 KB Output is correct
49 Correct 270 ms 18236 KB Output is correct
50 Correct 167 ms 14284 KB Output is correct
51 Correct 256 ms 17916 KB Output is correct
52 Correct 319 ms 24320 KB Output is correct
53 Correct 265 ms 15724 KB Output is correct
54 Correct 243 ms 15444 KB Output is correct
55 Correct 210 ms 15212 KB Output is correct
56 Correct 256 ms 15084 KB Output is correct
57 Correct 185 ms 14280 KB Output is correct
58 Correct 2 ms 5976 KB Output is correct
59 Correct 1 ms 5980 KB Output is correct
60 Correct 1 ms 5976 KB Output is correct
61 Correct 1 ms 5976 KB Output is correct
62 Correct 1 ms 5976 KB Output is correct
63 Correct 2 ms 5976 KB Output is correct
64 Correct 2 ms 5980 KB Output is correct
65 Correct 311 ms 24000 KB Output is correct
66 Correct 273 ms 16764 KB Output is correct
67 Correct 273 ms 24972 KB Output is correct
68 Correct 247 ms 19072 KB Output is correct
69 Correct 1 ms 5976 KB Output is correct
70 Correct 2 ms 5980 KB Output is correct
71 Correct 2 ms 5980 KB Output is correct
72 Correct 1 ms 5980 KB Output is correct
73 Correct 2 ms 5980 KB Output is correct
74 Correct 2 ms 5980 KB Output is correct
75 Correct 2 ms 5976 KB Output is correct
76 Correct 2 ms 5980 KB Output is correct
77 Correct 1 ms 5976 KB Output is correct
78 Correct 1 ms 5980 KB Output is correct
79 Correct 2 ms 5976 KB Output is correct
80 Correct 2 ms 5980 KB Output is correct
81 Correct 1 ms 5980 KB Output is correct
82 Correct 2 ms 5980 KB Output is correct
83 Correct 2 ms 5980 KB Output is correct