Submission #891675

# Submission time Handle Problem Language Result Execution time Memory
891675 2023-12-23T14:31:22 Z vjudge1 Highway Tolls (IOI18_highway) C++17
51 / 100
194 ms 262144 KB
#include "highway.h"
#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>;
using ii = pair<int, int>;
vector<ii> E;

struct tree {
    int n;
    vi par, par_cent, sz;
    vector<vi> L, G;
    
    tree(int N) : n(N), L(N), par(N), par_cent(N), sz(N), G(N) {}

    void add_edge(int u, int v) {
        L[u].push_back(v);
        L[v].push_back(u);
    }

    void init() {
        vi on(n, 1);
        function<void(int, int)> dfs_sz = [&](int u, int p) {
            sz[u] = 1;
            for(auto it : L[u]) {
                if(on[it] && it != p) {
                    dfs_sz(it, u);
                    sz[u] += sz[it];
                }
            }
        };

        function<int(int, int, int)> find_c = [&](int u, int p, int slim) {
            for(auto it : L[u])
                if(on[it] && it != p && 2 * sz[it] > slim) return find_c(it, u, slim);
            return u;
        };

        function<void(int, int) > dfs_cent = [&](int u, int pc) {
            dfs_sz(u, -1);
            int c = find_c(u, -1, sz[u]);
            on[c] = 0;
            par_cent[c] = pc;
            if(pc != -1) {
                G[pc].push_back(c);
            }
            for(auto it : L[c])
                if(on[it]) dfs_cent(it, c);
        };

        dfs_cent(0, -1);
    }

    void solve() {
        function<void(int, int, vi&)> dfs_comp = [&](int u, int p, vi& V) {
            V.push_back(u);
            for(auto it : G[u])
                dfs_comp(it, u, V);
        };
        int cost_baza = ask(vi(E.size(), 0));
        auto dif = [&](int u, vector<vi>& Fii, int st, int dr) {
            set<int> S = {u};
            for(int i = st; i <= dr; ++i)
                for(auto it : Fii[i]) S.insert(it);
            vi w;
            for(auto [u, v] : E) {
                if(S.count(u) + S.count(v))
                    w.push_back(1);
                else
                    w.push_back(0); 
            }
            return ask(w) - cost_baza;
        };
        vi capete;
        function<void(int, int, int)> dfs_solve = [&](int c, int pc, int nr) {
            vector<vi> comp_fii;
            for(auto it : G[c]) {
                comp_fii.push_back(vi());
                dfs_comp(it, c, comp_fii.back());
            }
            int pst = -1, pdr = comp_fii.size(), nrfii = comp_fii.size();

            int v = dif(c, comp_fii, 0, nrfii - 1);
            if(!v) {
                if(nr == 1) { // v e unul dintre s & t 
                    capete.push_back(v);
                    return;
                } else {
                    ///uhhh, wtf?
                    assert(0);
                }
            }
            for(int step = 31 - __builtin_clz(nrfii); step >= 0; --step) {
                if(pst + (1 << step) < nrfii) {
                    if(!dif(c, comp_fii, 0, pst + (1 << step)))
                        pst += (1 << step);
                }
                if(pdr - (1 << step) >= 0) {
                    if(!dif(c, comp_fii, pdr - (1 << step), nrfii - 1))
                        pdr -= (1 << step);
                }
            }
            ++pst;
            --pdr;

        };
        assert(capete.size() == 2);
    }

    int cine(int rad) {
        vi niv(n);
        function<void(int, int, int)> dfs = [&](int u, int p, int li) {
            niv[u] = li;
            for(auto it : L[u]) {
                if(it != p) {
                    dfs(it, u, li + 1);
                }
            }
        };
        vi re;
        for(int i = 0; i < n; ++i) {
            re.push_back(i);
        }
        dfs(rad, -1, 0);
        sort(re.begin(), re.end(), [&](int a, int b) { return niv[a] < niv[b]; });
        int st = 0, dr = n - 1, mij;
        int q0 = 0;
        auto query = [&](int p) {
            static vi used(n, 0);
            for(int i = 0; i < n; ++i)
                used[i] = 0;
            for(int i = 0; i <= p; ++i)
                used[re[i]] = 1;
            vi w;
            for(auto [u, v] : E) {
                if(used[u] && used[v]) w.push_back(1);
                else w.push_back(0);
            }
            int v = ask(w) - q0;
            return v + q0;
        };
        q0 = query(dr);
        int cm = query(dr);
        while(st < dr) {
            mij = (st + dr) / 2;
            if(query(mij) == q0) {
                dr = mij;
            } else 
                st = mij + 1;
        }
        return re[st];
    }
};

void find_pair(int N, vi U, vi V, int A, int B) {
    tree T(N);
    for(int i = 0; i < U.size(); ++i) {
        T.add_edge(U[i], V[i]);
        E.push_back({U[i], V[i]});
    }
    int s = T.cine(0);
    int t = T.cine(s);
    answer(s, t);
}

Compilation message

highway.cpp: In constructor 'tree::tree(int)':
highway.cpp:13:16: warning: 'tree::L' will be initialized after [-Wreorder]
   13 |     vector<vi> L, G;
      |                ^
highway.cpp:12:8: warning:   'vi tree::par' [-Wreorder]
   12 |     vi par, par_cent, sz;
      |        ^~~
highway.cpp:15:5: warning:   when initialized here [-Wreorder]
   15 |     tree(int N) : n(N), L(N), par(N), par_cent(N), sz(N), G(N) {}
      |     ^~~~
highway.cpp: In member function 'int tree::cine(int)':
highway.cpp:144:13: warning: unused variable 'cm' [-Wunused-variable]
  144 |         int cm = query(dr);
      |             ^~
highway.cpp: In function 'void find_pair(int, vi, vi, int, int)':
highway.cpp:158:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |     for(int i = 0; i < U.size(); ++i) {
      |                    ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 10 ms 1856 KB Output is correct
3 Correct 118 ms 13388 KB Output is correct
4 Correct 112 ms 13448 KB Output is correct
5 Correct 115 ms 13392 KB Output is correct
6 Correct 101 ms 12944 KB Output is correct
7 Correct 111 ms 13152 KB Output is correct
8 Correct 110 ms 13548 KB Output is correct
9 Correct 106 ms 13140 KB Output is correct
10 Correct 110 ms 13644 KB Output is correct
11 Correct 129 ms 15364 KB Output is correct
12 Correct 131 ms 16304 KB Output is correct
13 Correct 119 ms 15308 KB Output is correct
14 Correct 125 ms 15456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2868 KB Output is correct
2 Correct 18 ms 5080 KB Output is correct
3 Correct 26 ms 7280 KB Output is correct
4 Correct 81 ms 21060 KB Output is correct
5 Correct 75 ms 21064 KB Output is correct
6 Correct 82 ms 21064 KB Output is correct
7 Correct 81 ms 21076 KB Output is correct
8 Correct 81 ms 21268 KB Output is correct
9 Correct 78 ms 21200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 12 ms 1864 KB Output is correct
3 Correct 73 ms 10216 KB Output is correct
4 Correct 102 ms 13168 KB Output is correct
5 Correct 95 ms 13376 KB Output is correct
6 Correct 105 ms 12952 KB Output is correct
7 Correct 96 ms 13180 KB Output is correct
8 Correct 108 ms 13428 KB Output is correct
9 Correct 109 ms 13404 KB Output is correct
10 Correct 125 ms 13396 KB Output is correct
11 Correct 128 ms 15364 KB Output is correct
12 Correct 114 ms 15920 KB Output is correct
13 Correct 133 ms 15748 KB Output is correct
14 Correct 124 ms 16076 KB Output is correct
15 Correct 97 ms 13144 KB Output is correct
16 Correct 91 ms 13380 KB Output is correct
17 Correct 132 ms 15888 KB Output is correct
18 Correct 132 ms 15616 KB Output is correct
19 Correct 99 ms 13180 KB Output is correct
20 Correct 109 ms 16536 KB Output is correct
21 Correct 101 ms 13200 KB Output is correct
22 Correct 107 ms 13200 KB Output is correct
23 Correct 110 ms 14024 KB Output is correct
24 Correct 101 ms 14348 KB Output is correct
25 Correct 115 ms 21304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 194 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 181 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -