답안 #485583

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485583 2021-11-08T12:06:50 Z sam571128 Love Polygon (BOI18_polygon) C++17
100 / 100
248 ms 29788 KB
#include <bits/stdc++.h>

#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int N = 1e5+5;
int arr[N], dp[N][2], dp2[N][2], dsu[N];
vector<int> adj[N];

int find(int u){
    return dsu[u]==u ? u : dsu[u] = find(dsu[u]);
}

void unite(int u, int v){
    u = find(u), v = find(v);
    if(u==v) return;
    dsu[u] = v;
}


void dfs(int u, int p, int no = -1){
    for(auto v : adj[u]){
        if(v==p) continue;
        dfs(v,u,no);
        dp[u][0] += max(dp[v][0],dp[v][1]);
        dp2[u][0] += max(dp2[v][0],dp2[v][1]);
    }

    for(auto v : adj[u]){
        if(v==p) continue;
        dp[u][1] = max(dp[u][1],dp[u][0]-max(dp[v][0],dp[v][1])+dp[v][0]+1);
        if(v!=no) dp2[u][1] = max(dp2[u][1],dp2[u][0]-max(dp2[v][0],dp2[v][1])+dp2[v][0]+1);
    }

    if(u==no) dp2[u][1] = -1e18;
}

int idx;
map<string,int> m;

int assign(string x){
    if(m.find(x)==m.end()){
        m[x] = idx++;
    }
    return m[x];
}

signed main(){
    fastio
    iota(dsu,dsu+N,0);
    int n;
    cin >> n;

    vector<pair<int,int>> edges;
    for(int i = 0;i < n;i++){
        string a,b;
        cin >> a >> b;
        int x = assign(a);
        int y = assign(b);
        arr[x] = y;
        if(find(x)!=find(y)) unite(x,y), adj[x].push_back(y), adj[y].push_back(x);
        else edges.push_back({y,x});
    }

    if(n%2){
        cout << -1 << "\n";
        return 0;
    }


    int ans = n;

    for(auto [x,y] : edges){
        dfs(x,x,y);
        if(x!=y) ans -= max({dp[x][0],dp[x][1],dp2[x][0]+(arr[x]==y && arr[y]==x ? 2 : 1)});
        else ans -= max(dp[x][0],dp[x][1]);
    }
    


    cout << ans << "\n";
}   
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
3 Correct 1 ms 3404 KB Output is correct
4 Correct 2 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
6 Correct 1 ms 3404 KB Output is correct
7 Correct 2 ms 3404 KB Output is correct
8 Correct 2 ms 3404 KB Output is correct
9 Correct 2 ms 3404 KB Output is correct
10 Correct 2 ms 3404 KB Output is correct
11 Correct 2 ms 3452 KB Output is correct
12 Correct 2 ms 3404 KB Output is correct
13 Correct 2 ms 3456 KB Output is correct
14 Correct 2 ms 3404 KB Output is correct
15 Correct 2 ms 3452 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
3 Correct 2 ms 3404 KB Output is correct
4 Correct 248 ms 26596 KB Output is correct
5 Correct 210 ms 18492 KB Output is correct
6 Correct 238 ms 27572 KB Output is correct
7 Correct 209 ms 15132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 223 ms 18696 KB Output is correct
2 Correct 239 ms 20508 KB Output is correct
3 Correct 156 ms 15168 KB Output is correct
4 Correct 194 ms 15772 KB Output is correct
5 Correct 224 ms 26952 KB Output is correct
6 Correct 217 ms 18920 KB Output is correct
7 Correct 212 ms 18804 KB Output is correct
8 Correct 182 ms 18304 KB Output is correct
9 Correct 214 ms 19060 KB Output is correct
10 Correct 147 ms 16016 KB Output is correct
11 Correct 2 ms 3404 KB Output is correct
12 Correct 2 ms 3404 KB Output is correct
13 Correct 2 ms 3360 KB Output is correct
14 Correct 2 ms 3404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 3404 KB Output is correct
2 Correct 2 ms 3404 KB Output is correct
3 Correct 1 ms 3404 KB Output is correct
4 Correct 2 ms 3404 KB Output is correct
5 Correct 2 ms 3404 KB Output is correct
6 Correct 1 ms 3404 KB Output is correct
7 Correct 2 ms 3404 KB Output is correct
8 Correct 2 ms 3404 KB Output is correct
9 Correct 2 ms 3404 KB Output is correct
10 Correct 2 ms 3404 KB Output is correct
11 Correct 2 ms 3452 KB Output is correct
12 Correct 2 ms 3404 KB Output is correct
13 Correct 2 ms 3456 KB Output is correct
14 Correct 2 ms 3404 KB Output is correct
15 Correct 2 ms 3452 KB Output is correct
16 Correct 2 ms 3404 KB Output is correct
17 Correct 2 ms 3404 KB Output is correct
18 Correct 2 ms 3404 KB Output is correct
19 Correct 248 ms 26596 KB Output is correct
20 Correct 210 ms 18492 KB Output is correct
21 Correct 238 ms 27572 KB Output is correct
22 Correct 209 ms 15132 KB Output is correct
23 Correct 223 ms 18696 KB Output is correct
24 Correct 239 ms 20508 KB Output is correct
25 Correct 156 ms 15168 KB Output is correct
26 Correct 194 ms 15772 KB Output is correct
27 Correct 224 ms 26952 KB Output is correct
28 Correct 217 ms 18920 KB Output is correct
29 Correct 212 ms 18804 KB Output is correct
30 Correct 182 ms 18304 KB Output is correct
31 Correct 214 ms 19060 KB Output is correct
32 Correct 147 ms 16016 KB Output is correct
33 Correct 2 ms 3404 KB Output is correct
34 Correct 2 ms 3404 KB Output is correct
35 Correct 2 ms 3360 KB Output is correct
36 Correct 2 ms 3404 KB Output is correct
37 Correct 248 ms 20688 KB Output is correct
38 Correct 231 ms 22636 KB Output is correct
39 Correct 207 ms 20928 KB Output is correct
40 Correct 211 ms 21032 KB Output is correct
41 Correct 222 ms 21112 KB Output is correct
42 Correct 227 ms 20916 KB Output is correct
43 Correct 215 ms 20960 KB Output is correct
44 Correct 229 ms 21100 KB Output is correct
45 Correct 241 ms 20968 KB Output is correct
46 Correct 219 ms 21016 KB Output is correct
47 Correct 194 ms 21332 KB Output is correct
48 Correct 231 ms 20832 KB Output is correct
49 Correct 222 ms 22724 KB Output is correct
50 Correct 139 ms 17236 KB Output is correct
51 Correct 208 ms 17988 KB Output is correct
52 Correct 227 ms 29000 KB Output is correct
53 Correct 219 ms 21060 KB Output is correct
54 Correct 219 ms 21028 KB Output is correct
55 Correct 179 ms 20408 KB Output is correct
56 Correct 206 ms 21188 KB Output is correct
57 Correct 140 ms 18104 KB Output is correct
58 Correct 2 ms 3404 KB Output is correct
59 Correct 2 ms 3404 KB Output is correct
60 Correct 2 ms 3404 KB Output is correct
61 Correct 2 ms 3404 KB Output is correct
62 Correct 2 ms 3404 KB Output is correct
63 Correct 2 ms 3404 KB Output is correct
64 Correct 2 ms 3448 KB Output is correct
65 Correct 233 ms 28544 KB Output is correct
66 Correct 211 ms 20600 KB Output is correct
67 Correct 238 ms 29788 KB Output is correct
68 Correct 202 ms 17252 KB Output is correct
69 Correct 2 ms 3404 KB Output is correct
70 Correct 2 ms 3404 KB Output is correct
71 Correct 2 ms 3404 KB Output is correct
72 Correct 2 ms 3404 KB Output is correct
73 Correct 2 ms 3404 KB Output is correct
74 Correct 2 ms 3404 KB Output is correct
75 Correct 2 ms 3404 KB Output is correct
76 Correct 2 ms 3404 KB Output is correct
77 Correct 2 ms 3404 KB Output is correct
78 Correct 3 ms 3404 KB Output is correct
79 Correct 2 ms 3452 KB Output is correct
80 Correct 2 ms 3404 KB Output is correct
81 Correct 2 ms 3404 KB Output is correct
82 Correct 2 ms 3404 KB Output is correct
83 Correct 2 ms 3404 KB Output is correct