Submission #784255

# Submission time Handle Problem Language Result Execution time Memory
784255 2023-07-15T22:31:19 Z Ozy Highway Tolls (IOI18_highway) C++17
51 / 100
219 ms 262144 KB
#include "highway.h"
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define rep(i,a,b) for(int i = (a); i <= (b); i++)
#define repa(i,a,b) for(int i = (a); i >= (b); i--)
#define pll pair<lli,lli>

#define MAX 90000
//para los hijos
#define des first
#define id second

lli m,df,sz,prof;
lli arista_padre[MAX+2],tam[MAX+2],color[MAX+2];
vector<pll> hijos[MAX+2];
vector<lli> nodos,inval;
vector<int> status;

void pre_process(lli pos, lli padre) {
    tam[pos] = 1;
    for(auto h : hijos[pos]) {
        if(padre == h.des || inval[h.id] == 1) continue;
        pre_process(h.des,pos);
        tam[pos] += tam[h.des];
    }
}

lli busca(lli pos, lli padre, lli lim) {

    for(auto h : hijos[pos]) {
        if (padre == h.des || inval[h.id] == 1) continue;
        if (tam[h.des] > lim) return busca(h.des,pos,lim);
    }
    return pos;
}

void marca(lli pos, lli padre, lli ari, lli c) {
    status[ari] = c;
    color[ari] = c;
    for(auto h : hijos[pos]) {
        if (h.des == padre || inval[h.id] == 1) continue;
        marca(h.des,pos,h.id,c);
    }
}

void dfs(lli pos, lli padre, lli p,lli c) {

    if(p == prof) {
        nodos.push_back(pos);
        return;
    }

    for(auto h : hijos[pos]) {
        if(padre == h.des || color[h.id] != c || inval[h.id] == 1) continue;
        arista_padre[h.des] = h.id;
        dfs(h.des,pos,p+1,c);
    }
}

lli solve(lli raiz, lli c) {

    if(prof == 0) return raiz;
    nodos.clear();
    rep(i,0,m-1) status[i] = 0;

    dfs(raiz,-1,0,c);

    lli last,ini = 0;
    lli mitad,fin = nodos.size()-1;
    while (ini <= fin) {
        mitad = (ini+fin)/2;
        rep(i,0,nodos.size()-1) {
            if (ini <= i && i <= mitad) status[ arista_padre[nodos[i]] ] = 1;
            else status[arista_padre[nodos[i]]] = 0;
        }

        lli x = ask(status);
        if (x != df) {
            fin = mitad-1;
            last = mitad;
        }
        else ini = mitad+1;
    }

    return(nodos[last]);

}

void find_pair(int n, std::vector<int> u, std::vector<int> v, int A, int B) {

    m = u.size();
    rep(i,0,m-1) {
        hijos[u[i]].push_back({v[i],i});
        hijos[v[i]].push_back({u[i],i});
    }

    status.resize(m,0);
    inval.resize(m,0);

    df = ask(status);
    sz = df/A;

    //busca centroid
    lli raiz = 0;
    lli p0,p1;
    do {
        pre_process(raiz,-1);

        if(tam[raiz] == 2) {
            for(auto h : hijos[raiz]) {
                if (inval[h.id] == 0) answer(raiz,h.des);
                return;
            }
        }

        lli mitad = tam[raiz]/2;
        lli act = busca(raiz,-1,mitad);
        //dividelo en los 2 colores
        lli unos = 0;

        rep(i,0,m-1) {
            status[i] = 0;
            color[i] = 2;
        }

        for(auto h : hijos[act]) {
            if (inval[h.id] == 1) continue;

            lli gh;
            if(tam[h.des] > tam[act]) gh = tam[raiz]-tam[act];
            else gh = tam[h.des];

            if (gh+unos <= mitad) {
                marca(h.des,act,h.id,1);
                unos += gh;
            }
            else {
                //debug("entro");
                marca(h.des,act,h.id,0);
            }
        }
        //corregido

        lli x = ask(status);
        lli bigg = (x - df)/(B-A);

        raiz = act;
        if (bigg == sz) rep(i,0,m-1) {
            //debug("entre con sz");
            if(color[i] == 0) inval[i] = 1;
        }
        else if (bigg == 0) {
            //debug("entre con 0");
            rep(i,0,m-1) if(color[i] == 1) inval[i] = 1;
        }
        else {
            p1 = bigg;
            p0 = sz-bigg;
            break;
        }

        //debug(raiz);
        //rep(i,0,m-1) {
        //    debugsl(i);
        //    debugsl(color[i]);
        //    debug(inval[i]);
        //}

    } while(true);

    //debug(raiz);
    //debug(p0);
    //debug(p1);

    //pregunta por ambas raices y reporta el resultado
    prof = p0;
    lli x = solve(raiz,0);
    prof = p1;
    lli y = solve(raiz,1);

    //debugsl(x);
    //debug(y);

    answer(x,y);
}

Compilation message

highway.cpp: In function 'long long int solve(long long int, long long int)':
highway.cpp:7:39: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define rep(i,a,b) for(int i = (a); i <= (b); i++)
      |                                       ^
highway.cpp:75:9: note: in expansion of macro 'rep'
   75 |         rep(i,0,nodos.size()-1) {
      |         ^~~
highway.cpp:88:22: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
   88 |     return(nodos[last]);
      |                      ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 1 ms 2384 KB Output is correct
4 Correct 1 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 1 ms 2384 KB Output is correct
8 Correct 2 ms 2384 KB Output is correct
9 Correct 1 ms 2424 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 1 ms 2308 KB Output is correct
12 Correct 1 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2500 KB Output is correct
2 Correct 10 ms 3372 KB Output is correct
3 Correct 93 ms 11716 KB Output is correct
4 Correct 77 ms 11728 KB Output is correct
5 Correct 89 ms 11672 KB Output is correct
6 Correct 71 ms 11672 KB Output is correct
7 Correct 91 ms 11584 KB Output is correct
8 Correct 89 ms 11636 KB Output is correct
9 Correct 84 ms 11672 KB Output is correct
10 Correct 88 ms 11516 KB Output is correct
11 Correct 132 ms 13020 KB Output is correct
12 Correct 117 ms 13612 KB Output is correct
13 Correct 122 ms 13064 KB Output is correct
14 Correct 88 ms 12608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 3920 KB Output is correct
2 Correct 18 ms 5456 KB Output is correct
3 Correct 32 ms 6992 KB Output is correct
4 Correct 86 ms 16292 KB Output is correct
5 Correct 69 ms 16152 KB Output is correct
6 Correct 74 ms 16528 KB Output is correct
7 Correct 57 ms 16096 KB Output is correct
8 Correct 102 ms 16400 KB Output is correct
9 Correct 65 ms 16152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2512 KB Output is correct
2 Correct 7 ms 3460 KB Output is correct
3 Correct 64 ms 9136 KB Output is correct
4 Correct 130 ms 11736 KB Output is correct
5 Correct 77 ms 10976 KB Output is correct
6 Correct 94 ms 11236 KB Output is correct
7 Correct 98 ms 10932 KB Output is correct
8 Correct 117 ms 10956 KB Output is correct
9 Correct 87 ms 11708 KB Output is correct
10 Correct 74 ms 11724 KB Output is correct
11 Correct 125 ms 12444 KB Output is correct
12 Correct 115 ms 13660 KB Output is correct
13 Correct 79 ms 13256 KB Output is correct
14 Correct 100 ms 13596 KB Output is correct
15 Correct 115 ms 10960 KB Output is correct
16 Correct 96 ms 10920 KB Output is correct
17 Correct 93 ms 13388 KB Output is correct
18 Correct 90 ms 12936 KB Output is correct
19 Correct 85 ms 10932 KB Output is correct
20 Correct 124 ms 14124 KB Output is correct
21 Correct 90 ms 12048 KB Output is correct
22 Correct 111 ms 11592 KB Output is correct
23 Correct 88 ms 12168 KB Output is correct
24 Correct 101 ms 12676 KB Output is correct
25 Correct 116 ms 16408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 219 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 175 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -