Submission #1037804

# Submission time Handle Problem Language Result Execution time Memory
1037804 2024-07-29T08:36:09 Z Forested Simurgh (IOI17_simurgh) C++17
51 / 100
91 ms 3272 KB
#include <bits/stdc++.h>
using namespace std;
using i32 = int;
using i64 = long long;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = V<V<T>>;
template <typename T>
using VVV = V<VV<T>>;
template <typename T>
bool chmin(T &x, const T &y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}
template <typename T>
bool chmax(T &x, const T &y) {
    if (x < y) {
        x = y;
        return true;
    }
    return false;
}
#define OVERRIDE4(a, b, c, d, ...) d
#define REP2(i, n) for (i32 i = 0; i < (i32)(n); ++i)
#define REP3(i, l, r) for (i32 i = (i32)(l); i < (i32)(r); ++i)
#define REP(...) OVERRIDE4(__VA_ARGS__, REP3, REP2)(__VA_ARGS__)
#define PER2(i, n) for (i32 i = (i32)(n)-1; i >= 0; --i)
#define PER3(i, l, r) for (i32 i = (i32)(r)-1; i >= (i32)(l); --i)
#define PER(...) OVERRIDE4(__VA_ARGS__, PER3, PER2)(__VA_ARGS__)
#define LEN(x) (i32) size(x)
#define ALL(x) begin(x), end(x)

void dbg(i32 x) { cerr << x; }
void dbg(i64 x) { cerr << x; }
template <typename T, typename U>
void dbg(pair<T, U> p) {
    cerr << "(";
    dbg(p.first);
    cerr << ", ";
    dbg(p.second);
    cerr << ")";
}
template <typename T>
void dbg(V<T> arr) {
    cerr << "[";
    REP(i, LEN(arr)) {
        if (i) {
            cerr << ", ";
        }
        dbg(arr[i]);
    }
    cerr << "]";
}
void debug() { cerr << '\n'; }
template <typename Head, typename... Tail>
void debug(Head head, Tail... tail) {
    dbg(head);
    cerr << ", ";
    debug(tail...);
}
#ifdef DEBUGF
#define DBG(...)                       \
    do {                               \
        cerr << #__VA_ARGS__ << " : "; \
        debug(__VA_ARGS__);            \
    } while (false)
#else
#define DBG(...) (void)0
#endif

#include "simurgh.h"

V<i32> subtask_3(i32 n, V<i32> u, V<i32> v) {
    struct Edge {
        i32 to, id;
    };
    i32 m = LEN(u);
    VV<Edge> g(n);
    REP(i, m) {
        g[u[i]].emplace_back(Edge{v[i], i});
        g[v[i]].emplace_back(Edge{u[i], i});
    }
    i32 root = 0;
    V<i32> par(n, -1), pid(n, -1), dep(n, -1);
    {
        queue<i32> que;
        que.push(root);
        dep[root] = 0;
        while (!que.empty()) {
            i32 v = que.front();
            que.pop();
            for (const Edge &e : g[v]) {
                if (dep[e.to] == -1) {
                    par[e.to] = v;
                    pid[e.to] = e.id;
                    dep[e.to] = dep[v] + 1;
                    que.push(e.to);
                }
            }
        }
    }
    V<i32> tree;
    REP(i, n) {
        if (pid[i] != -1) {
            tree.push_back(pid[i]);
        }
    }
    i32 base = count_common_roads(tree);
    V<i32> con(m, -1);
    REP(i, m) {
        if (count(ALL(tree), i)) {
            continue;
        }
        V<i32> loop;
        {
            i32 x = u[i], y = v[i];
            while (x != y) {
                if (dep[x] < dep[y]) {
                    swap(x, y);
                }
                loop.push_back(pid[x]);
                x = par[x];
            }
        }
        const i32 nil = 998;
        V<i32> diffs(LEN(loop), nil);
        REP(j, LEN(loop)) {
            i32 e = loop[j];
            if (con[e] != -1) {
                continue;
            }
            V<i32> ask = tree;
            ask.erase(find(ALL(ask), e));
            ask.push_back(i);
            diffs[j] = count_common_roads(ask) - base;
        }
        if (auto itr = find(ALL(diffs), nil); itr != end(diffs)) {
            {
                i32 e = loop[itr - begin(diffs)];
                V<i32> ask = tree;
                ask.erase(find(ALL(ask), e));
                ask.push_back(i);
                i32 di = count_common_roads(ask) - base;
                con[i] = di + con[e];
            }
            REP(j, LEN(loop)) {
                i32 e = loop[j];
                if (con[e] != -1) {
                    continue;
                }
                // diffs[j] = con[i] - con[e]
                con[e] = con[i] - diffs[j];
            }
        } else if (count(ALL(diffs), 0) == LEN(diffs)) {
            con[i] = 0;
            REP(j, LEN(diffs)) { con[loop[j]] = 0; }
        } else {
            REP(j, LEN(loop)) {
                if (diffs[j] == 1) {
                    con[i] = 1;
                }
                if (diffs[j] == -1) {
                    con[i] = 0;
                }
            }
            REP(j, LEN(loop)) {
                i32 e = loop[j];
                con[e] = con[i] - diffs[j];
            }
        }
    }
    REP(i, m) {
        if (con[i] == -1) {
            con[i] = 1;
        }
    }
    V<i32> ans;
    REP(i, m) {
        if (con[i] == 1) {
            ans.push_back(i);
        }
    }
    return ans;
}

V<i32> subtask_4(i32 n, V<i32> u, V<i32> v) {
    if (n == 2) {
        return V<i32>({0});
    }
    i32 m = LEN(u);
    VV<i32> eid(n, V<i32>(n, -1));
    REP(i, m) { eid[u[i]][v[i]] = eid[v[i]][u[i]] = i; }
    V<i32> con(m, -1);
    V<i32> inh;
    auto check = [&](i32 r) -> void {
        DBG(r);
        V<i32> star;
        REP(i, n) {
            if (i != r) {
                star.push_back(eid[r][i]);
            }
        }
        i32 base = count_common_roads(star);
        DBG(star, base);
        REP(i, n) {
            if (i == r) {
                continue;
            }
            i32 nx = i + 1;
            if (nx == r) {
                ++nx;
            }
            if (nx == n) {
                nx = 0;
            }
            if (nx == r) {
                ++nx;
            }
            if (nx == n) {
                nx = 0;
            }
            i32 e = eid[i][nx];
            DBG(i, nx, e);
            i32 d0 = 0, d1 = 0;
            {
                V<i32> ask = star;
                ask.erase(find(ALL(ask), eid[r][i]));
                ask.push_back(e);
                d0 = count_common_roads(ask) - base;
            }
            {
                V<i32> ask = star;
                ask.erase(find(ALL(ask), eid[r][nx]));
                ask.push_back(e);
                d1 = count_common_roads(ask) - base;
            }
            if (d0 == 0 && d1 == 0) {
                con[eid[r][i]] = con[eid[r][nx]] = con[e] = 0;
            } else {
                // d0 = con[e] - con[eid[r][i]]
                if (d0 != 0) {
                    con[e] = (d0 + 1) / 2;
                }
                if (d1 != 0) {
                    con[e] = (d1 + 1) / 2;
                }
                con[eid[r][i]] = con[e] - d0;
                con[eid[r][nx]] = con[e] - d1;
            }
        }
        DBG(con);
        inh.clear();
        REP(i, n) {
            if (i != r && con[eid[0][i]] == 1) {
                inh.push_back(i);
            }
        }
        V<i32> rems;
        star.clear();
        REP(i, n) {
            if (i != r && con[eid[r][i]] == 0) {
                rems.push_back(i);
            }
            if (i != r && con[eid[r][i]] == 1) {
                star.push_back(eid[r][i]);
            }
        }
        if (LEN(rems) <= 1) {
            return;
        }
        REP(i, LEN(rems) - 1) {
            i32 v = rems[i];
            i32 l = i + 1;
            while (l < LEN(rems)) {
                i32 ok = LEN(rems), ng = l - 1;
                while (ok - ng > 1) {
                    i32 mid = (ok + ng) / 2;
                    V<i32> ask = star;
                    REP(j, LEN(rems)) {
                        if (!(l <= j && j <= mid)) {
                            ask.push_back(eid[r][rems[j]]);
                        }
                    }
                    REP(j, l, mid + 1) {
                        ask.push_back(eid[v][rems[j]]);
                    }
                    if (count_common_roads(ask) == LEN(star)) {
                        ng = mid;
                    } else {
                        ok = mid;
                    }
                }
                REP(j, l, ok) {
                    con[eid[v][rems[j]]] = 0;
                }
                if (ok == LEN(rems)) {
                    break;
                }
                con[eid[v][rems[ok]]] = 1;
                l = ok + 1;
            }
        }
        DBG(con);
    };
    check(0);
    if (!inh.empty()) {
        check(inh[0]);
    }
    V<i32> ans;
    REP(i, m) {
        if (con[i] == 1) {
            ans.push_back(i);
        }
    }
    if (LEN(ans) != n - 1) {
        assert(false);
    }
    return ans;
}

V<i32> find_roads(i32 n, V<i32> u, V<i32> v) {
    if (LEN(u) == n * (n - 1) / 2) {
        return subtask_4(n, u, v);
    }
    if (n <= 240) {
        return subtask_3(n, u, v);
    }
    assert(false);
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 344 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 344 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 344 KB correct
21 Correct 1 ms 496 KB correct
22 Correct 1 ms 344 KB correct
23 Correct 1 ms 472 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 344 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 344 KB correct
21 Correct 1 ms 496 KB correct
22 Correct 1 ms 344 KB correct
23 Correct 1 ms 472 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 25 ms 1460 KB correct
35 Correct 87 ms 1752 KB correct
36 Correct 64 ms 1368 KB correct
37 Correct 4 ms 348 KB correct
38 Correct 91 ms 1776 KB correct
39 Correct 74 ms 1656 KB correct
40 Correct 56 ms 1368 KB correct
41 Correct 11 ms 1372 KB correct
42 Correct 85 ms 1628 KB correct
43 Correct 42 ms 1312 KB correct
44 Correct 43 ms 860 KB correct
45 Correct 42 ms 1116 KB correct
46 Correct 29 ms 996 KB correct
47 Correct 12 ms 728 KB correct
48 Correct 1 ms 344 KB correct
49 Correct 4 ms 348 KB correct
50 Correct 12 ms 604 KB correct
51 Correct 40 ms 1116 KB correct
52 Correct 36 ms 860 KB correct
53 Correct 30 ms 856 KB correct
54 Correct 43 ms 1116 KB correct
55 Correct 42 ms 1116 KB correct
56 Correct 48 ms 1116 KB correct
57 Correct 42 ms 1112 KB correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB correct
2 Correct 0 ms 344 KB correct
3 Incorrect 83 ms 3272 KB WA in grader: NO
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB correct
2 Correct 1 ms 348 KB correct
3 Correct 0 ms 348 KB correct
4 Correct 0 ms 348 KB correct
5 Correct 0 ms 348 KB correct
6 Correct 0 ms 348 KB correct
7 Correct 0 ms 348 KB correct
8 Correct 0 ms 344 KB correct
9 Correct 0 ms 348 KB correct
10 Correct 0 ms 348 KB correct
11 Correct 0 ms 348 KB correct
12 Correct 0 ms 348 KB correct
13 Correct 0 ms 348 KB correct
14 Correct 1 ms 348 KB correct
15 Correct 1 ms 348 KB correct
16 Correct 1 ms 348 KB correct
17 Correct 1 ms 348 KB correct
18 Correct 1 ms 348 KB correct
19 Correct 1 ms 348 KB correct
20 Correct 1 ms 344 KB correct
21 Correct 1 ms 496 KB correct
22 Correct 1 ms 344 KB correct
23 Correct 1 ms 472 KB correct
24 Correct 1 ms 348 KB correct
25 Correct 0 ms 348 KB correct
26 Correct 1 ms 348 KB correct
27 Correct 1 ms 348 KB correct
28 Correct 0 ms 348 KB correct
29 Correct 0 ms 348 KB correct
30 Correct 1 ms 348 KB correct
31 Correct 1 ms 348 KB correct
32 Correct 1 ms 348 KB correct
33 Correct 1 ms 348 KB correct
34 Correct 25 ms 1460 KB correct
35 Correct 87 ms 1752 KB correct
36 Correct 64 ms 1368 KB correct
37 Correct 4 ms 348 KB correct
38 Correct 91 ms 1776 KB correct
39 Correct 74 ms 1656 KB correct
40 Correct 56 ms 1368 KB correct
41 Correct 11 ms 1372 KB correct
42 Correct 85 ms 1628 KB correct
43 Correct 42 ms 1312 KB correct
44 Correct 43 ms 860 KB correct
45 Correct 42 ms 1116 KB correct
46 Correct 29 ms 996 KB correct
47 Correct 12 ms 728 KB correct
48 Correct 1 ms 344 KB correct
49 Correct 4 ms 348 KB correct
50 Correct 12 ms 604 KB correct
51 Correct 40 ms 1116 KB correct
52 Correct 36 ms 860 KB correct
53 Correct 30 ms 856 KB correct
54 Correct 43 ms 1116 KB correct
55 Correct 42 ms 1116 KB correct
56 Correct 48 ms 1116 KB correct
57 Correct 42 ms 1112 KB correct
58 Correct 0 ms 348 KB correct
59 Correct 0 ms 344 KB correct
60 Incorrect 83 ms 3272 KB WA in grader: NO
61 Halted 0 ms 0 KB -