Submission #429395

# Submission time Handle Problem Language Result Execution time Memory
429395 2021-06-15T22:32:54 Z peuch Highway Tolls (IOI18_highway) C++17
11 / 100
406 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]){
		ans.push_back(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:53:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   53 |  for(int i = 0; i < ar[cent].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~~
highway.cpp:61:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |   for(int i = 0; i < ar[cent].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~~~
highway.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |   for(int i = 0; i < filhos.size(); i++){
      |                  ~~^~~~~~~~~~~~~~~
highway.cpp:90:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   90 |    for(int i = 0; i < ar[cent].size(); i++)
      |                   ~~^~~~~~~~~~~~~~~~~
highway.cpp: In function 'void preCalc(int, int)':
highway.cpp:106:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'void preCalc2(int, int)':
highway.cpp:116:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  116 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'int getCent(int, int, int)':
highway.cpp:125:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  125 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'void paint(int, int, std::vector<int>&)':
highway.cpp:134:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'int bb(std::vector<int>)':
highway.cpp:145:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |  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 200 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 200 KB Output is correct
10 Correct 1 ms 200 KB Output is correct
11 Correct 1 ms 200 KB Output is correct
12 Correct 1 ms 200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 456 KB Output is correct
2 Correct 18 ms 2188 KB Output is correct
3 Incorrect 406 ms 18708 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 14 ms 3284 KB Output is correct
2 Correct 29 ms 6212 KB Output is correct
3 Correct 73 ms 9160 KB Output is correct
4 Correct 244 ms 30272 KB Output is correct
5 Correct 149 ms 27736 KB Output is correct
6 Correct 235 ms 29904 KB Output is correct
7 Correct 140 ms 28704 KB Output is correct
8 Correct 247 ms 30264 KB Output is correct
9 Correct 147 ms 29524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 456 KB Output is correct
2 Correct 31 ms 2084 KB Output is correct
3 Correct 163 ms 13596 KB Output is correct
4 Correct 333 ms 18812 KB Output is correct
5 Correct 276 ms 16776 KB Output is correct
6 Correct 364 ms 18804 KB Output is correct
7 Correct 249 ms 17020 KB Output is correct
8 Correct 255 ms 17676 KB Output is correct
9 Incorrect 362 ms 18476 KB Output isn't correct
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 308 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 293 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -