Submission #429391

# Submission time Handle Problem Language Result Execution time Memory
429391 2021-06-15T22:26:07 Z peuch Highway Tolls (IOI18_highway) C++17
11 / 100
402 ms 262148 KB
#include "highway.h"
#include<bits/stdc++.h>
using namespace std;

int n;
vector<vector<int> > ar, id;
vector<int> marc, sub, pai;
vector<int> nulo;
vector<int> ans;

long long iniVal;

void centDecomp(int cur, bool flag, int p);
void preCalc(int cur, int p);
void preCalc2(int cur, int p);
int getCent(int cur, int pai, int tam);
void paint(int cur, int pai, vector<int>& w);
int bb(vector<int> values);
bool query(vector<int> w);

void find_pair(int N, std::vector<int> U, std::vector<int> V, int A, int B) {
	n = N;
	ar = id = vector<vector<int> > (n);
	marc = sub = pai = vector<int> (n);
	nulo = vector<int> (U.size());
	for(int i = 0; i < U.size(); i++){
		ar[U[i]].push_back(V[i]);
		ar[V[i]].push_back(U[i]);
		id[U[i]].push_back(i);
		id[V[i]].push_back(i);
	}
	iniVal = ask(nulo);
	centDecomp(0, 1, 0);
	sort(ans.begin(), ans.end());
//	printf("%d %d %d\n", ans.size(), ans[0], ans[1]);
	answer(ans[0], ans[1]);
}

void centDecomp(int cur, bool flag, int p){
//	printf("%d %d %d\n", cur, flag, p);
	if(marc[cur]){
		vector<int> w = nulo;
		for(int i = 0; i < ar[cur].size(); i++)
			w[id[cur][i]] = 1;
		if(query(w)) ans.push_back(cur);
		else centDecomp(pai[cur], 0, cur);
		return;
	}
	preCalc(cur, cur);
	int cent = getCent(cur, cur, sub[cur]);
//	printf("Cent: %d Flag: %d\n", cent, flag);
	
	marc[cent] = 1;
	
	vector<int> w = nulo;
	vector<int> filhos;
	for(int i = 0; i < ar[cent].size(); i++){
		if(!flag && ar[cent][i] == pai[cent]) continue;
		if(marc[ar[cent][i]]) continue;
		filhos.push_back(ar[cent][i]);
		w[id[cent][i]] = 1;	
	}
	if(filhos.empty()){
		vector<int> w = nulo;
		for(int i = 0; i < ar[cent].size(); i++)
			w[id[cent][i]] = 1;
		if(query(w))
			ans.push_back(cent);	
		else
			centDecomp(pai[cent], 0, cent);
		return;
	}
	if(flag && query(w)){
		int n1 = bb(filhos);
		vector<int> filhos2;
		for(int i = 0; i < filhos.size(); i++){
			if(filhos[i] == n1) continue;
			filhos2.push_back(filhos[i]);
		}
		int n2 = bb(filhos2);
		preCalc2(cent, cent);
		if(n1 != -1)
			centDecomp(n1, 0, cent);
		if(n2 != -1)
			centDecomp(n2, 0, cent);
		if(n2 == -1 || n1 == -1)
			ans.push_back(cent);
		return;
	}
	if(!flag){
		int next = bb(filhos);
		if(next == -1){
			vector<int> w = nulo;
			for(int i = 0; i < ar[cent].size(); i++)
				w[id[cent][i]] = 1;
			if(query(w))
				ans.push_back(cent);	
			else
				centDecomp(pai[cent], 0, cent);
		}
		else centDecomp(next, 0, cent);
		return;
	}
	int next = bb(filhos);
	centDecomp(next, 1, cent);
}

void preCalc(int cur, int p){
	sub[cur] = 1;
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		if(marc[viz] || viz == p) continue;
		preCalc(viz, cur);
		sub[cur] += sub[viz];
	}
}

void preCalc2(int cur, int p){
	sub[cur] = 1;
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		if(marc[viz] || viz == p) continue;
		preCalc2(viz, cur);
		pai[viz] = cur;
	}
}

int getCent(int cur, int pai, int tam){
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		if(marc[viz] || viz == pai|| sub[viz] <= tam / 2) continue;
		return getCent(viz, cur, tam);
	}
	return cur;
}

void paint(int cur, int pai, vector<int>& w){
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		w[id[cur][i]] = 1;
		if(marc[viz] || viz == pai) continue;
		paint(viz, cur, w);
	}
}

int bb(vector<int> values){
	int ini = 0, fim = values.size() - 1;
	vector<int> w = nulo;
	for(int i = 0; i < values.size(); i++)
		paint(values[i], values[i], w);
	if(!query(w)) return -1;
	while(ini != fim){
		int mid = (ini + fim) >> 1;
		vector<int> w = nulo;
		for(int i = ini; i <= mid; i++)
			paint(values[i], values[i], w);
		if(query(w)) fim = mid;
		else ini = mid + 1;
	}
	return values[ini];
}

bool query(vector<int> w){
	return ask(w) != iniVal;
}

Compilation message

highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:26:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |  for(int i = 0; i < U.size(); i++){
      |                 ~~^~~~~~~~~~
highway.cpp: In function 'void centDecomp(int, bool, int)':
highway.cpp:43:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |   for(int i = 0; i < ar[cur].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~
highway.cpp:57:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |  for(int i = 0; i < ar[cent].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~~
highway.cpp:65:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   65 |   for(int i = 0; i < ar[cent].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~
highway.cpp:76:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |   for(int i = 0; i < filhos.size(); i++){
      |                  ~~^~~~~~~~~~~~~~~
highway.cpp:94:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   94 |    for(int i = 0; i < ar[cent].size(); i++)
      |                   ~~^~~~~~~~~~~~~~~~~
highway.cpp: In function 'void preCalc(int, int)':
highway.cpp:110:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'void preCalc2(int, int)':
highway.cpp:120:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'int getCent(int, int, int)':
highway.cpp:129:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'void paint(int, int, std::vector<int>&)':
highway.cpp:138:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'int bb(std::vector<int>)':
highway.cpp:149:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  149 |  for(int i = 0; i < values.size(); i++)
      |                 ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 200 KB Output is correct
2 Correct 1 ms 200 KB Output is correct
3 Correct 1 ms 200 KB Output is correct
4 Correct 1 ms 200 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 200 KB Output is correct
8 Correct 1 ms 200 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 2 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 456 KB Output is correct
2 Correct 28 ms 2144 KB Output is correct
3 Incorrect 304 ms 18812 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 18 ms 3312 KB Output is correct
2 Correct 30 ms 6212 KB Output is correct
3 Correct 54 ms 9092 KB Output is correct
4 Correct 249 ms 30176 KB Output is correct
5 Correct 186 ms 27916 KB Output is correct
6 Correct 256 ms 29820 KB Output is correct
7 Correct 228 ms 28752 KB Output is correct
8 Correct 238 ms 30172 KB Output is correct
9 Correct 169 ms 29500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 456 KB Output is correct
2 Correct 23 ms 2124 KB Output is correct
3 Correct 256 ms 13572 KB Output is correct
4 Correct 283 ms 18940 KB Output is correct
5 Correct 339 ms 16656 KB Output is correct
6 Correct 280 ms 18780 KB Output is correct
7 Correct 310 ms 17016 KB Output is correct
8 Correct 328 ms 17692 KB Output is correct
9 Incorrect 402 ms 18612 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 296 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 274 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -