답안 #579866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579866 2022-06-20T06:59:48 Z dxz05 Love Polygon (BOI18_polygon) C++14
100 / 100
311 ms 67236 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];

set<int> s[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]]) s[f[i]].insert(i);
    }

    set<pair<int, int>> S;

    for (int i = 1; i <= n; i++){
        if (f[i] != i && !used[i] && !used[f[i]]) S.insert(MP(s[i].size(), i));
    }

    int ans = 0;
    while (!S.empty()){
        int i = S.begin()->second;
        S.erase(MP(s[i].size(), i));
        S.erase(MP(s[f[i]].size(), f[i]));

        ans++;
        used[i] = true;
        used[f[i]] = true;

        for (int j : s[i]) S.erase(MP(s[j].size(), j));
        for (int j : s[f[i]]) S.erase(MP(s[j].size(), j));

        int j = f[f[i]];
        if (S.count(MP(s[j].size(), j))){
            S.erase(MP(s[j].size(), j));
            s[j].erase(f[i]);
            if (!used[j] && !used[f[j]] && f[j] != j) S.insert(MP(s[j].size(), j));
        }

    }

    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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47316 KB Output is correct
2 Correct 28 ms 47296 KB Output is correct
3 Correct 26 ms 47316 KB Output is correct
4 Correct 33 ms 47300 KB Output is correct
5 Correct 30 ms 47316 KB Output is correct
6 Correct 27 ms 47208 KB Output is correct
7 Correct 27 ms 47320 KB Output is correct
8 Correct 29 ms 47236 KB Output is correct
9 Correct 28 ms 47320 KB Output is correct
10 Correct 26 ms 47216 KB Output is correct
11 Correct 31 ms 47304 KB Output is correct
12 Correct 31 ms 47308 KB Output is correct
13 Correct 27 ms 47272 KB Output is correct
14 Correct 26 ms 47316 KB Output is correct
15 Correct 26 ms 47288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 47320 KB Output is correct
2 Correct 28 ms 47316 KB Output is correct
3 Correct 27 ms 47252 KB Output is correct
4 Correct 293 ms 65100 KB Output is correct
5 Correct 270 ms 64596 KB Output is correct
6 Correct 282 ms 65280 KB Output is correct
7 Correct 26 ms 47180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 275 ms 65040 KB Output is correct
2 Correct 272 ms 67192 KB Output is correct
3 Correct 151 ms 57616 KB Output is correct
4 Correct 26 ms 47324 KB Output is correct
5 Correct 268 ms 67092 KB Output is correct
6 Correct 264 ms 67116 KB Output is correct
7 Correct 247 ms 67140 KB Output is correct
8 Correct 188 ms 62472 KB Output is correct
9 Correct 238 ms 67172 KB Output is correct
10 Correct 183 ms 66972 KB Output is correct
11 Correct 31 ms 47308 KB Output is correct
12 Correct 26 ms 47292 KB Output is correct
13 Correct 26 ms 47280 KB Output is correct
14 Correct 25 ms 47240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47316 KB Output is correct
2 Correct 28 ms 47296 KB Output is correct
3 Correct 26 ms 47316 KB Output is correct
4 Correct 33 ms 47300 KB Output is correct
5 Correct 30 ms 47316 KB Output is correct
6 Correct 27 ms 47208 KB Output is correct
7 Correct 27 ms 47320 KB Output is correct
8 Correct 29 ms 47236 KB Output is correct
9 Correct 28 ms 47320 KB Output is correct
10 Correct 26 ms 47216 KB Output is correct
11 Correct 31 ms 47304 KB Output is correct
12 Correct 31 ms 47308 KB Output is correct
13 Correct 27 ms 47272 KB Output is correct
14 Correct 26 ms 47316 KB Output is correct
15 Correct 26 ms 47288 KB Output is correct
16 Correct 29 ms 47320 KB Output is correct
17 Correct 28 ms 47316 KB Output is correct
18 Correct 27 ms 47252 KB Output is correct
19 Correct 293 ms 65100 KB Output is correct
20 Correct 270 ms 64596 KB Output is correct
21 Correct 282 ms 65280 KB Output is correct
22 Correct 26 ms 47180 KB Output is correct
23 Correct 275 ms 65040 KB Output is correct
24 Correct 272 ms 67192 KB Output is correct
25 Correct 151 ms 57616 KB Output is correct
26 Correct 26 ms 47324 KB Output is correct
27 Correct 268 ms 67092 KB Output is correct
28 Correct 264 ms 67116 KB Output is correct
29 Correct 247 ms 67140 KB Output is correct
30 Correct 188 ms 62472 KB Output is correct
31 Correct 238 ms 67172 KB Output is correct
32 Correct 183 ms 66972 KB Output is correct
33 Correct 31 ms 47308 KB Output is correct
34 Correct 26 ms 47292 KB Output is correct
35 Correct 26 ms 47280 KB Output is correct
36 Correct 25 ms 47240 KB Output is correct
37 Correct 258 ms 67236 KB Output is correct
38 Correct 252 ms 67180 KB Output is correct
39 Correct 269 ms 66728 KB Output is correct
40 Correct 288 ms 67084 KB Output is correct
41 Correct 248 ms 67148 KB Output is correct
42 Correct 299 ms 67220 KB Output is correct
43 Correct 295 ms 67076 KB Output is correct
44 Correct 291 ms 67072 KB Output is correct
45 Correct 286 ms 67064 KB Output is correct
46 Correct 311 ms 67056 KB Output is correct
47 Correct 262 ms 67172 KB Output is correct
48 Correct 273 ms 67020 KB Output is correct
49 Correct 260 ms 67120 KB Output is correct
50 Correct 184 ms 57588 KB Output is correct
51 Correct 27 ms 47300 KB Output is correct
52 Correct 287 ms 67060 KB Output is correct
53 Correct 259 ms 67176 KB Output is correct
54 Correct 254 ms 67052 KB Output is correct
55 Correct 215 ms 62484 KB Output is correct
56 Correct 258 ms 67088 KB Output is correct
57 Correct 199 ms 67020 KB Output is correct
58 Correct 26 ms 47316 KB Output is correct
59 Correct 27 ms 47316 KB Output is correct
60 Correct 29 ms 47216 KB Output is correct
61 Correct 26 ms 47228 KB Output is correct
62 Correct 27 ms 47300 KB Output is correct
63 Correct 28 ms 47308 KB Output is correct
64 Correct 27 ms 47316 KB Output is correct
65 Correct 276 ms 67148 KB Output is correct
66 Correct 244 ms 66540 KB Output is correct
67 Correct 276 ms 67060 KB Output is correct
68 Correct 26 ms 47300 KB Output is correct
69 Correct 26 ms 47316 KB Output is correct
70 Correct 30 ms 47308 KB Output is correct
71 Correct 25 ms 47228 KB Output is correct
72 Correct 27 ms 47324 KB Output is correct
73 Correct 27 ms 47296 KB Output is correct
74 Correct 25 ms 47316 KB Output is correct
75 Correct 29 ms 47304 KB Output is correct
76 Correct 26 ms 47292 KB Output is correct
77 Correct 26 ms 47316 KB Output is correct
78 Correct 25 ms 47312 KB Output is correct
79 Correct 26 ms 47316 KB Output is correct
80 Correct 25 ms 47316 KB Output is correct
81 Correct 29 ms 47316 KB Output is correct
82 Correct 27 ms 47304 KB Output is correct
83 Correct 27 ms 47248 KB Output is correct