Submission #579860

# Submission time Handle Problem Language Result Execution time Memory
579860 2022-06-20T06:46:45 Z dxz05 Love Polygon (BOI18_polygon) C++14
21 / 100
2000 ms 9032 KB
#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

#include <random>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

#define MP make_pair

typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.000001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 3e2;
const int M = 1111;

map<string, int> mp;
int id(const string &s){
    if (mp.find(s) != mp.end()) return mp[s];
    return mp[s] = mp.size() + 1;
}

int f[N];

bool used[N];

int cnt[N];

void solve(int TC) {
    int n;
    cin >> n;

    if (n & 1){
        cout << -1;
        return;
    }

    for (int i = 1; i <= n; i++){
        string x, y;
        cin >> x >> y;
        f[id(x)] = id(y);
//        cout << id(x) << ' ' << id(y) << endl;
    }
//    cout << endl;

    for (int i = 1; i <= n; i++){
        if (!used[i] && !used[f[i]] && f[f[i]] == i && f[i] != i){
            used[i] = true;
            used[f[i]] = true;
        }
    }

    for (int i = 1; i <= n; i++){
        if (f[i] != i && !used[i] && !used[f[i]]) cnt[f[i]]++;
    }

    int ans = 0;
    for (int it = 1; it <= n; it++){
        int ind = -1;
        for (int i = 1; i <= n; i++){
            if (f[i] == i || used[i] || used[f[i]]) continue;
            if (ind == -1 || cnt[i] < cnt[ind]) ind = i;
        }

        if (ind == -1) break;

        ans++;

//        cout << ind << ' ' << f[ind] << endl;

        used[ind] = true;
        used[f[ind]] = true;
        cnt[f[f[ind]]]--;
    }

    for (int i = 1; i <= n; i++) ans += !used[i];
//    for (int i = 1; i <= n; i++) cout << used[i] << ' '; cout << endl;

    cout << ans << endl;

}

int main() {
    startTime = clock();
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int TC = 1;
    //    cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

#ifdef dddxxz
    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
#endif

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Execution timed out 2091 ms 9028 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 2096 ms 9032 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 292 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 308 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Execution timed out 2091 ms 9028 KB Time limit exceeded
20 Halted 0 ms 0 KB -