Submission #1037804

#TimeUsernameProblemLanguageResultExecution timeMemory
1037804ForestedSimurgh (IOI17_simurgh)C++17
51 / 100
91 ms3272 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...