Submission #579908

# Submission time Handle Problem Language Result Execution time Memory
579908 2022-06-20T08:53:35 Z drdilyor Love Polygon (BOI18_polygon) C++17
100 / 100
389 ms 33276 KB
#if defined(ONPC) && !defined(_GLIBCXX_ASSERTIONS)
    #define _GLIBCXX_ASSERTIONS
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#define cerr cout
#ifdef ONPC
    #include "t_debug.cpp"
#else
    #define debug(...) 42
#endif
#define allit(a) (a).begin(), (a).end()
#define sz(a) ((int) (a).size())

using namespace std;
using ll = long long;
using vi = vector<int>;
namespace pd = __gnu_pbds;
template<typename K>
using ordered_set = pd::tree<K, pd::null_type, less<int>, pd::rb_tree_tag, pd::tree_order_statistics_node_update>;
template<typename... T>
using gp_hash_table = pd::gp_hash_table<T...>;//simple using statement gives warnings

const int INF = 1e9;
const ll INFL = 1e18;
const int N = 1e5;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rng(RANDOM);

int ans;
void confirm(int i) {
#ifdef ONPC
    if (i == ans) cout << "ok " << i << endl;
    else cout << "wrong " << i << ", expected " << ans << endl;
#else
    cout << i << '\n';
#endif
}

int solve() {
    int n; cin >> n;
    map<string, int> code;
    vector<string> name(n);

    vi adj(n);
    vector<set<int>> rev(n);

    for (int i = 0, t = 0; i < n; i++) {
        string u, v;
        cin >> u >> v;
        if (code.count(u) == 0) {
            name[t] = u;
            code[u] = t++;
        }
        if (code.count(v) == 0) {
            name[t] = v;
            code[v] = t++;
        }
        adj[code[u]] = code[v];
        rev[code[v]].insert(code[u]);
    }

    if (n % 2 == 1) {confirm(-1); return 0;}

    vi inrel(n);
    int inrel_count = 0;
    for (int i = 0; i <n; i++) {
        inrel_count += inrel[i] = (
            i != adj[i] && adj[adj[i]] == i);
    }
    debug(name);
    debug(adj, rev, inrel);


    int res = 0;
    //unloved ones shall be matched
    {
        set<int> unloved;
        auto set_love = [&](int from, int to) {
            rev[adj[from]].erase(from);
            rev[to].insert(from);
            adj[from] = to;
        };
        auto check = [&](int i) {
            if (rev[i].empty()) {
                if (inrel[adj[i]]) {set_love(i, i); return false; }
                else {unloved.insert(i); return true; }
            }
            return false;
        };
        function<void()> action = [&]() {
            for (int i = 0; i < n; i++) {
                check(i);
            }

            while (!unloved.empty()) {
                int i = *unloved.begin();
                if (check(i)) {
                    int j = adj[i];
                    // remove those who love j
                    for (int k : rev[j]) {
                        adj[k] = k;
                        rev[k].insert(k);
                    }
                    int dangling = adj[j];
                    rev[j].clear();
                    set_love(j, i);
                    set_love(i, j);
                    inrel[j] = inrel[i] = 1;
                    inrel_count += 2;
                    res++;
                    check(dangling);
                }
                debug(i, adj, rev, inrel, unloved);
                unloved.erase(i);
            }
        };

        action();
        vi adj_p = adj;
        debug("run 2");
        action();
        assert(adj == adj_p);

        for (int i = 0; i < n; i++) {
            assert(rev[i].size() == 1);
        }
    }
    debug(res);
    // polygons shall no longer survive
    if (inrel_count != n) {
        vi visited(n, 0);
        auto cycle = [&](int i, int depth) {
            res += (depth + 1) / 2;
            debug("cycle", i, depth);
        };
        function<pair<int,int>(int)> dfs = [&](int i) -> pair<int,int> {
            if (inrel[i]) return {-1, -1};
            if (visited[i]) { return {i, 0}; }
            debug("dfs", i);
            visited[i] = 1;
            auto [end, depth] = dfs(adj[i]);
            if (end == -1) return {-1, -1};
            depth++;

            inrel[i] = 1;
            if (end == i) {
                cycle(end, depth);
                assert(depth != 2);
                return {-1, -1};
            }
            return {end, depth};
        };
        for (int i = 0; i < n; i++) {
            if (!inrel[i] && !visited[i]) {
                auto [end, depth] = dfs(i);
                if (end != -1) cycle(end, depth);
            }
        }
    }
    confirm(res);

    return 0;
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
#ifdef ONPC
    cin >> t;
    while (t-- && cin) {
        cin >> ans;
        if (!cin) break;
#else
    while (t-- && cin) {
#endif
        if (solve()) break;
        #ifdef ONPC
            cout << "____________________" << endl;
        #endif
    }
    return 0;
}

Compilation message

polygon.cpp: In function 'int solve()':
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:72:5: note: in expansion of macro 'debug'
   72 |     debug(name);
      |     ^~~~~
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:73:5: note: in expansion of macro 'debug'
   73 |     debug(adj, rev, inrel);
      |     ^~~~~
polygon.cpp: In lambda function:
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:115:17: note: in expansion of macro 'debug'
  115 |                 debug(i, adj, rev, inrel, unloved);
      |                 ^~~~~
polygon.cpp: In function 'int solve()':
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:122:9: note: in expansion of macro 'debug'
  122 |         debug("run 2");
      |         ^~~~~
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:130:5: note: in expansion of macro 'debug'
  130 |     debug(res);
      |     ^~~~~
polygon.cpp: In lambda function:
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:136:13: note: in expansion of macro 'debug'
  136 |             debug("cycle", i, depth);
      |             ^~~~~
polygon.cpp: In lambda function:
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:141:13: note: in expansion of macro 'debug'
  141 |             debug("dfs", i);
      |             ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 269 ms 26052 KB Output is correct
5 Correct 278 ms 22100 KB Output is correct
6 Correct 262 ms 26732 KB Output is correct
7 Correct 274 ms 21280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 304 ms 23304 KB Output is correct
2 Correct 357 ms 24556 KB Output is correct
3 Correct 231 ms 23832 KB Output is correct
4 Correct 274 ms 23168 KB Output is correct
5 Correct 353 ms 23804 KB Output is correct
6 Correct 344 ms 26228 KB Output is correct
7 Correct 389 ms 26352 KB Output is correct
8 Correct 296 ms 25660 KB Output is correct
9 Correct 347 ms 27804 KB Output is correct
10 Correct 251 ms 33260 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 324 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 324 KB Output is correct
19 Correct 269 ms 26052 KB Output is correct
20 Correct 278 ms 22100 KB Output is correct
21 Correct 262 ms 26732 KB Output is correct
22 Correct 274 ms 21280 KB Output is correct
23 Correct 304 ms 23304 KB Output is correct
24 Correct 357 ms 24556 KB Output is correct
25 Correct 231 ms 23832 KB Output is correct
26 Correct 274 ms 23168 KB Output is correct
27 Correct 353 ms 23804 KB Output is correct
28 Correct 344 ms 26228 KB Output is correct
29 Correct 389 ms 26352 KB Output is correct
30 Correct 296 ms 25660 KB Output is correct
31 Correct 347 ms 27804 KB Output is correct
32 Correct 251 ms 33260 KB Output is correct
33 Correct 1 ms 324 KB Output is correct
34 Correct 1 ms 324 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 301 ms 24384 KB Output is correct
38 Correct 323 ms 26032 KB Output is correct
39 Correct 330 ms 25984 KB Output is correct
40 Correct 303 ms 26232 KB Output is correct
41 Correct 332 ms 26304 KB Output is correct
42 Correct 328 ms 25664 KB Output is correct
43 Correct 335 ms 25576 KB Output is correct
44 Correct 316 ms 25576 KB Output is correct
45 Correct 308 ms 25616 KB Output is correct
46 Correct 311 ms 25600 KB Output is correct
47 Correct 323 ms 27460 KB Output is correct
48 Correct 338 ms 24984 KB Output is correct
49 Correct 281 ms 24608 KB Output is correct
50 Correct 230 ms 23900 KB Output is correct
51 Correct 286 ms 23276 KB Output is correct
52 Correct 292 ms 24004 KB Output is correct
53 Correct 332 ms 26184 KB Output is correct
54 Correct 314 ms 26244 KB Output is correct
55 Correct 287 ms 25552 KB Output is correct
56 Correct 318 ms 27568 KB Output is correct
57 Correct 286 ms 33276 KB Output is correct
58 Correct 1 ms 212 KB Output is correct
59 Correct 1 ms 212 KB Output is correct
60 Correct 1 ms 324 KB Output is correct
61 Correct 1 ms 212 KB Output is correct
62 Correct 1 ms 324 KB Output is correct
63 Correct 1 ms 212 KB Output is correct
64 Correct 1 ms 320 KB Output is correct
65 Correct 311 ms 27860 KB Output is correct
66 Correct 273 ms 23880 KB Output is correct
67 Correct 280 ms 28488 KB Output is correct
68 Correct 274 ms 23224 KB Output is correct
69 Correct 1 ms 324 KB Output is correct
70 Correct 1 ms 212 KB Output is correct
71 Correct 1 ms 212 KB Output is correct
72 Correct 1 ms 212 KB Output is correct
73 Correct 1 ms 212 KB Output is correct
74 Correct 1 ms 212 KB Output is correct
75 Correct 1 ms 324 KB Output is correct
76 Correct 1 ms 212 KB Output is correct
77 Correct 1 ms 212 KB Output is correct
78 Correct 1 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 1 ms 212 KB Output is correct
82 Correct 1 ms 212 KB Output is correct
83 Correct 1 ms 324 KB Output is correct