Submission #505290

#TimeUsernameProblemLanguageResultExecution timeMemory
505290wiwihoLove Polygon (BOI18_polygon)C++14
21 / 100
186 ms16196 KiB
#include <bits/stdc++.h>

#define eb emplace_back
#define mp make_pair
#define F first
#define S second

using namespace std;

using pii = pair<int, int>;

map<string, int> id;
int num = 0;
int getid(string& s){
    if(id.find(s) == id.end()) id[s] = num++;
    return id[s];
}

int n;
vector<vector<int>> g;

bool check(int msk){
    for(int i = 0; i < n; i++){
        int cnt = 0;
        for(int j : g[i]){
            if(!(1 << j & msk)) cnt++;
        }
        //cerr << bitset<8>(msk) << " " << i << " " << cnt << "\n";
        if(1 << i & msk){
            if(cnt > 1) return false;
        }
        else{
            if(cnt > 0) return false;
        }
    }
    //cerr << "ok " << bitset<8>(msk) << "\n";
    return true;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;

    g.resize(n);
    vector<int> f(n);
    for(int i = 1; i <= n; i++){
        string s, t;
        cin >> s >> t;
        int u = getid(s), v = getid(t);
        f[u] = v;
    }
    for(int i = 0; i < n; i++){
        if(f[f[i]] == i && f[i] != i) continue;
        g[f[i]].eb(i);
    }

    //for(auto& i : id) cerr << i.F << " " << i.S << "\n";

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

    int ans = n;
    for(int i = 0; i < (1 << n); i++){
        if(check(i)) ans = min(ans, __builtin_popcount(i));
    }
    cout << ans << "\n";

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...