답안 #579892

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
579892 2022-06-20T08:15:43 Z drdilyor Love Polygon (BOI18_polygon) C++17
25 / 100
339 ms 26428 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>
#ifdef ONPC
    #include "t_debug.cpp"
#else
#endif
    #define debug(...) 42
#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;
        for (int i = 0; i < n; i++) {
            if (rev[i].empty()) {
                if (inrel[adj[i]]) adj[i] = i;
                else unloved.insert(i);
            }
        }
        function<void()> action = [&]() {
            while (!unloved.empty()) {
                int i = *unloved.begin();
                unloved.erase(unloved.begin());
                if (rev[i].empty() && !inrel[adj[i]]) {
                    int a1 = i, a2 = adj[i];
                    assert(!inrel[a1]);
                    rev[adj[a2]].erase(a2);

                    if (rev[i].empty()) {
                        if (inrel[adj[i]]) adj[i] = i;
                        else unloved.insert(i);
                    }

                    for (int i : rev[a2]) {
                        adj[i] = i;
                        rev[i].insert(i);
                    }
                    rev[a1] = {a2};
                    adj[a2] = a1;
                    adj[a1] = a2;
                    inrel[a1] = inrel[a2] = 1;
                    inrel_count+= 2;
                    res++;
                    //debug(i, adj, rev, inrel);
                }
            }
        };

        action();
        vi adj_p = adj;
        action();
        assert(adj == adj_p);
    }
    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:71:5: note: in expansion of macro 'debug'
   71 |     debug(name);
      |     ^~~~~
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(adj, rev, inrel);
      |     ^~~~~
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:118:5: note: in expansion of macro 'debug'
  118 |     debug(res);
      |     ^~~~~
polygon.cpp: In lambda function:
polygon.cpp:11:24: warning: statement has no effect [-Wunused-value]
   11 |     #define debug(...) 42
      |                        ^~
polygon.cpp:124:13: note: in expansion of macro 'debug'
  124 |             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:129:13: note: in expansion of macro 'debug'
  129 |             debug("dfs", i);
      |             ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 256 ms 25820 KB Output is correct
5 Correct 255 ms 21780 KB Output is correct
6 Correct 271 ms 26428 KB Output is correct
7 Correct 256 ms 20952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 339 ms 23092 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -