#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,prof,sz;
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;
color[pos] = 2;
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 prof,lli c) {
if(prof == 0) return raiz;
nodos.clear();
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);
inval.resize(m,0);
rep(i,0,m-1) status[i] = 0;
df = ask(status);
sz = df/A;
//busca centroid
lli raiz = 0;
lli p0,p1;
do {
pre_process(raiz,-1); // tambien limpia los colores
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;
for(auto h : hijos[act]) {
if (inval[h.des] == 1) continue;
if (tam[h.des]+unos <= mitad) {
marca(h.des,act,h.id,1);
unos += tam[h.des];
}
else marca(h.des,act,h.id,0);
}
//corregido
lli x = ask(status);
lli big = (x - df)/(B-A);
raiz = act;
if (big == sz) rep(i,0,m-1) if(color[i] == 0) inval[i] = 1;
else if (big == 0) rep(i,0,m-1) if(color[i] == 1) inval[i] = 1;
else {
p1 = big;
p0 = sz-big;
break;
}
} while(true);
//pregunta por ambas raices y reporta el resultado
lli x = solve(raiz,p0,0);
lli y = solve(raiz,p1,1);
answer(x,y);
}
Compilation message
highway.cpp: In function 'long long int solve(long long int, 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: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:141:17: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
141 | else if (big == 0) rep(i,0,m-1) if(color[i] == 1) inval[i] = 1;
| ^
highway.cpp:140:12: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
140 | if (big == sz) rep(i,0,m-1) if(color[i] == 0) inval[i] = 1;
| ^
highway.cpp: In function 'long long int solve(long long int, long long int, long long int)':
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 |
Runtime error |
2 ms |
2384 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
5 ms |
2512 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
12 ms |
3940 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
2500 KB |
Execution killed with signal 13 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
208 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
178 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |