답안 #485508

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485508 2021-11-08T03:09:49 Z sam571128 Love Polygon (BOI18_polygon) C++17
46 / 100
714 ms 24244 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 visited[N], arr[N], sz[N];
vector<int> adj[N];

void dfs(int u){
    assert(u <= N);
    assert(!visited[u]);
    sz[u] = 1;
    visited[u] = 1;
    for(auto v : adj[u]){
        if(visited[v]) continue;
        dfs(v);
        sz[u] += sz[v];
    }
}

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
    int n;
    cin >> n;

    for(int i = 0;i < n;i++){
        string a,b;
        cin >> a >> b;
        int x = assign(a);
        int y = assign(b);
        arr[x] = y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }

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

    if(n <= 20){
        int dp[1<<n];
        fill(dp,dp+(1<<n),1e18);
        dp[0] = 0;

        for(int mask = 0;mask < (1<<n); mask++){
            for(int i = 0;i < n;i++){
                if(mask&(1<<i)) continue;
                for(int j = 0;j < n;j++){
                    if(i==j||mask&(1<<j)) continue;
                    dp[mask|(1<<i)|(1<<j)] = min(dp[mask]+(arr[i]!=j)+(arr[j]!=i),dp[mask|(1<<i)|(1<<j)]);
                }
            }
        }

        cout << dp[(1<<n)-1] << "\n";
    }else{
        int ans = 0;
        for(int i = 0;i < n;i++)
            ans += (arr[i] == i);
        for(int i = 0; i < n; i++){
            if(!visited[i]){
                dfs(i); 
                if(sz[i]>2){
                    ans += sz[i]/2; 
                    if(sz[i]%2) ans++;
                }

            }
        }
        cout << max(ans,(int)0) << "\n";
    }
}   
# 결과 실행 시간 메모리 Grader output
1 Correct 670 ms 10872 KB Output is correct
2 Correct 678 ms 10948 KB Output is correct
3 Correct 714 ms 10860 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 690 ms 10860 KB Output is correct
8 Correct 692 ms 10880 KB Output is correct
9 Correct 675 ms 10976 KB Output is correct
10 Correct 686 ms 10948 KB Output is correct
11 Correct 698 ms 10948 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 1 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2636 KB Output is correct
2 Correct 1 ms 2636 KB Output is correct
3 Correct 1 ms 2636 KB Output is correct
4 Correct 216 ms 21376 KB Output is correct
5 Correct 214 ms 18116 KB Output is correct
6 Correct 224 ms 24244 KB Output is correct
7 Correct 201 ms 16500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 243 ms 16308 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 670 ms 10872 KB Output is correct
2 Correct 678 ms 10948 KB Output is correct
3 Correct 714 ms 10860 KB Output is correct
4 Correct 2 ms 2636 KB Output is correct
5 Correct 2 ms 2636 KB Output is correct
6 Correct 1 ms 2636 KB Output is correct
7 Correct 690 ms 10860 KB Output is correct
8 Correct 692 ms 10880 KB Output is correct
9 Correct 675 ms 10976 KB Output is correct
10 Correct 686 ms 10948 KB Output is correct
11 Correct 698 ms 10948 KB Output is correct
12 Correct 2 ms 2636 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 1 ms 2636 KB Output is correct
15 Correct 1 ms 2636 KB Output is correct
16 Correct 1 ms 2636 KB Output is correct
17 Correct 1 ms 2636 KB Output is correct
18 Correct 1 ms 2636 KB Output is correct
19 Correct 216 ms 21376 KB Output is correct
20 Correct 214 ms 18116 KB Output is correct
21 Correct 224 ms 24244 KB Output is correct
22 Correct 201 ms 16500 KB Output is correct
23 Incorrect 243 ms 16308 KB Output isn't correct
24 Halted 0 ms 0 KB -