Submission #433871

# Submission time Handle Problem Language Result Execution time Memory
433871 2021-06-20T11:41:14 Z AmineTrabelsi Love Polygon (BOI18_polygon) C++14
0 / 100
2000 ms 460 KB
#include "bits/stdc++.h"
using namespace std;
// Hi 
int go[30];
int perm[21];
bool vis[21];
int n;
int ans = 1000000009;
void solve(int m){
    if(m == (n/2)){
        int cnt = 0;
        for(int i=0;i<n;i++){
            cnt += (perm[i] != go[i]);
        }
        ans = min(ans,cnt);
        return;
    }
    for(int i=0;i<n;i++){
        if(!vis[i]){
            vis[i] = 1;
            for(int j=i+1;j<n;j++){
                if(!vis[j]){
                    vis[j] = 1;
                    perm[i] = j;
                    perm[j] = i;
                    solve(m+1);
                    vis[j] = 0;
                }
            }
            vis[i] = 0;
        }
    }
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    cin>>n;
    map<string,int> mp;
    int nxt = 0;
    for(int i=0;i<n;i++){
        string s,t;
        cin>>s>>t;
        int first = 0,second = 0;
        if(mp.find(s) != mp.end()){
            first = mp[s];
        }else{
            first = nxt;
            mp[s] = nxt++;
        }
        if(mp.find(t) != mp.end()){
            second = mp[t];
        }else{
            second = nxt;
            mp[t] = nxt++;
        }
        go[first] = second;
    }
    if(n&1){
        cout << -1 << '\n';
        return 0;
    }
    solve(0);
    cout << (ans == 1000000009 ? -1 : ans) << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Execution timed out 2079 ms 204 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Runtime error 1 ms 460 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2079 ms 204 KB Time limit exceeded
2 Halted 0 ms 0 KB -