제출 #429350

#제출 시각아이디문제언어결과실행 시간메모리
429350peuch통행료 (IOI18_highway)C++17
0 / 100
289 ms262148 KiB
#include "highway.h"
#include<bits/stdc++.h>
using namespace std;

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

long long iniVal;

void centDecomp(int cur, bool flag);
void preCalc(int cur, int pai);
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 = 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);
	answer(ans[0], ans[1]);
}

void centDecomp(int cur, bool flag){
	preCalc(cur, cur);
	int cent = getCent(cur, cur, sub[cur]);
	
	marc[cent] = 1;
	
	vector<int> w = nulo;
	vector<int> filhos;
	for(int i = 0; i < ar[cent].size(); i++){
		if(marc[ar[cent][i]]) continue;
		w[id[cent][i]] = 1;	
		filhos.push_back(ar[cent][i]);
	}
	if(filhos.empty()) {
		ans.push_back(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);
		if(n1 != -1)
			centDecomp(n1, 0);
		if(n2 != -1)
			centDecomp(n2, 0);
		if(n2 == -1 || n1 == -1)
			ans.push_back(cent);
		return;
	}
	int next = bb(filhos);
	if(next == -1){
		ans.push_back(cent);
		return;
	}
	centDecomp(next, flag);
}

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

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;
}

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

highway.cpp: In function 'void find_pair(int, std::vector<int>, std::vector<int>, int, int)':
highway.cpp:25:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |  for(int i = 0; i < U.size(); i++){
      |                 ~~^~~~~~~~~~
highway.cpp: In function 'void centDecomp(int, bool)':
highway.cpp:44:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |  for(int i = 0; i < ar[cent].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~~
highway.cpp:56:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |   for(int i = 0; i < filhos.size(); i++){
      |                  ~~^~~~~~~~~~~~~~~
highway.cpp: In function 'void preCalc(int, int)':
highway.cpp:79:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'int getCent(int, int, int)':
highway.cpp:88:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'void paint(int, int, std::vector<int>&)':
highway.cpp:97:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
highway.cpp: In function 'int bb(std::vector<int>)':
highway.cpp:108:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |  for(int i = 0; i < values.size(); i++)
      |                 ~~^~~~~~~~~~~~~~~
#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...