제출 #467232

#제출 시각아이디문제언어결과실행 시간메모리
467232Leonardo_PaesHighway Tolls (IOI18_highway)C++17
23 / 100
297 ms10892 KiB
#include "highway.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
#define f first
#define s second

/* ask with a vector saying the edge value (a or b) for each edge to know the smallest dist
    1) find an edge in the path S -> T:
         D&C in the edges, trying to find anyone that is in the path.
         To do so, just paint everything with A and get the min dist from S to T
         Then, in the D&C paint all edges at the current set with B, if it changes
         the distance from S to T, than of these edges is in the path.
    2) Let e be an edge in the path S -> T, how to find one of the endpoints, S or T ?
    	 multisource bfs from the endpoints of e. Then enumerate the edges 
    	 according to their distante. Do bb in the prefix of edges, the last one that
    	 changes the dist from S to T has S or T as the farthest endpoint. 
   	3) Just do step 2) again 
*/
const int maxn = 9e4 + 10;
vector<int> grafo[maxn];

int find_edge(int &n, int &m, vector<int> &u, vector<int> &v, int &a, int &b, int &d){ // log m queries
	int l = 0, mid, r = m-1;
	while(l != r){
		mid = (l + r) >> 1;
		vector<int> aux(m, 0);
		for(int i=0; i<=mid; i++) aux[i] = 1;
		if(ask(aux) != d) r = mid;
		else l = mid + 1;
	}
	return l;
}

vector<int> bfs(int &n, int &m, int &a, int &b, int &d, int root){
	queue<pii> fila;
	fila.push({root, 0});
	vector<int> dist(n, -1);
	while(!fila.empty()){
		int x = fila.front().f, dis = fila.front().s;
		fila.pop();
		if(dist[x] != -1) continue;
		dist[x] = dis;
		for(int viz : grafo[x]) fila.push({viz, dis+1});
	}
	return dist;
}

pii find_endpoints(int &n, int &m, vector<int> &u, vector<int> &v, int &a, int &b, int &d, int &e){ // 2*log m queries
	vector<int> dist[2];
	dist[0] = bfs(n, m, a, b, d, u[e]);
	dist[1] = bfs(n, m, a, b, d, v[e]);
	pii ans = {0, 0};
	vector<pii> sbt[2];
	for(int i=0; i<n; i++){
		if(dist[0][i] <= dist[1][i]) sbt[0].push_back({dist[0][i], i});
		else sbt[1].push_back({dist[1][i], i});
	}
	for(int i=0; i<2; i++) sort(sbt[i].begin(), sbt[i].end());
	for(int j=0; j<2; j++){
		int ini = 0, meio, fim = (int)sbt[j].size() - 1;
		while(ini != fim){
			meio = (ini + fim + 1) >> 1;
			vector<int> aux(m, 0);
			vector<bool> mark(n, 0);
			for(int i=meio; i<sbt[j].size(); i++) mark[sbt[j][i].s] = 1;
			for(int i=0; i<m; i++) if(mark[u[i]] != mark[v[i]]) aux[i] = 1;
			if(ask(aux) != d) ini = meio;
			else fim = meio - 1;
		}
		if(j) ans.f = sbt[j][ini].s;
		else ans.s = sbt[j][ini].s;
	}
	return ans;
 }

void find_pair(int n, vector<int> u, vector<int> v, int a, int b) {  
	int m = u.size();
	for(int i=0; i<m; i++){
		grafo[u[i]].push_back(v[i]);
		grafo[v[i]].push_back(u[i]);
	}
	vector<int> aux(m, 0); // 1 query
	int d = ask(aux);
	int e = find_edge(n, m, u, v, a, b, d);
	pii ans = find_endpoints(n, m, u, v, a, b, d, e);
	answer(ans.f, ans.s);
	/*
4 4
1
3
1
3
0 1 
0 2
0 3
1 2
	*/
}

컴파일 시 표준 에러 (stderr) 메시지

highway.cpp: In function 'pii find_endpoints(int&, int&, std::vector<int>&, std::vector<int>&, int&, int&, int&, int&)':
highway.cpp:66:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |    for(int i=meio; i<sbt[j].size(); i++) mark[sbt[j][i].s] = 1;
      |                    ~^~~~~~~~~~~~~~
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...