Submission #329811

# Submission time Handle Problem Language Result Execution time Memory
329811 2020-11-22T15:52:30 Z theshadow_04 Love Polygon (BOI18_polygon) C++14
0 / 100
2000 ms 10820 KB
// V T An
#include <bits/stdc++.h>
#define F first
#define S second
#define MOD 1000000007
#define pb push_back
#define ll long long
#define Task "POLYGON"

using namespace std;

const int maxn = 100005;

int n;
int cap[maxn];
map<string, int> M;
int cnt1 = 0, cnt2 = 0, cnt, a[maxn], b[maxn];
int ans = 2e9;

void Solve(int t) {
    cnt1 = cnt2 = 0;
    for(int i = 0; i < cnt; ++i) {
        if((t >> i) & 1) a[++cnt1] = i + 1;
        else b[++cnt2] = i + 1;
    }
    do {
        int dem = 0;
        for(int i = 1; i <= cnt1; ++ i) {
            if(cap[a[i]] != b[i]) dem ++;
            if(cap[b[i]] != a[i]) dem ++;
        }
        ans = min(ans, dem);
    } while(next_permutation(b + 1, b + cnt2 + 1));
}

int main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0); cin.tie(0);
	if(fopen(Task".inp", "r")){
		freopen(Task".inp", "r", stdin);
		freopen(Task".out", "w", stdout);
	}
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        string s, t;
        cin >> s >> t;
        if(!M[s]) M[s] = ++ cnt;
        if(!M[t]) M[t] = ++ cnt;
        cap[M[s]] = M[t];
    }
    if(cnt % 2) {
        cout << -1 << "\n";
        return 0;
    }
    for(int i = 0; i < (1 << cnt); ++ i) {
        int x = __builtin_popcount(i);
        if(x != cnt / 2) continue;
        Solve(i);
    }
    if(ans == 2e9) ans = -1;
    cout << ans;
}

// CHY-AKAV

Compilation message

polygon.cpp: In function 'int main()':
polygon.cpp:40:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   40 |   freopen(Task".inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
polygon.cpp:41:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   41 |   freopen(Task".out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 2021 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Incorrect 255 ms 10820 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 268 ms 10808 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2021 ms 364 KB Time limit exceeded
2 Halted 0 ms 0 KB -