Submission #430342

#TimeUsernameProblemLanguageResultExecution timeMemory
430342dreezyStations (IOI20_stations)C++17
10 / 100
1230 ms772 KiB
#include "stations.h"
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
const int shift = 8;

vector<int> labels;
vector<vector<int>> graph;

vector<int> sz;
void dfs1(int n,int p){//calc sizes
	sz[n] = 1;
	for(int adj : graph[n])
	{
		if(adj == p) continue;
		dfs1(adj, n);
		sz[n] += sz[adj];
	}
}

vector<bool> vis;
int getcentroid(int n){
	dfs1(n, -1);
	vis[n] = true;
	
	for(int adj : graph[n]){
		if(!vis[adj] and sz[adj] >= sz.size()/2){
			//cout << adj<<": "<<sz[adj]<<endl;
			return getcentroid(adj);
		}
	}
	return n;
	
}

int counter;
void name_subtree(int n, int p){
	labels[n] =sz[n] + (counter << shift) ;
	if(p == -1) labels[n] = 0;
	counter++;
	for(int adj : graph[n]){
		if(adj == p)
		continue;
		name_subtree(adj, n);
	}
}


vector<int> label(int n, int k, vector<int> u, vector<int> v) {
	counter = 0;
	sz.clear();
	labels.clear();
	graph.clear();
	vis.clear();
	sz.assign(n, 0);
	vis.assign(n, 0);
	labels.assign(n, 0);
	graph.assign( n, vector<int>());
	for (int i = 0; i < n - 1; i++) {
		graph[u[i]].pb(v[i]);
		graph[v[i]].pb(u[i]);
	}
	

	//dfs1(0,-1);
	int root = getcentroid(0);		
	//for(int i =0; i<n; i++) cout <<i<<": "<< sz[i]<<endl;
	
	name_subtree(root, -1);
	
	return labels;
}

int find_next_station(int s, int t, vector<int> c) {
	
	int startind = ( s>> shift);
	int targetind = (t>>shift);
	for(int adj : c){
		int adjind = (adj >> shift);
		if(adjind < startind) continue;
		//cout << s <<"->"<<t<<": "<<adj<<", "<<sz[adj]<<endl;
		if(adjind<= targetind and targetind <= adjind + adj - (adjind<<shift) -1){
			//cout << startind<<"->"<<targetind<<": "<<adjind<<", "<<adj - (adjind<<10) -1 <<endl;
			return adj;
		}
	}
	return c[0];
}


/************/

Compilation message (stderr)

stations.cpp: In function 'int getcentroid(int)':
stations.cpp:29:28: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   29 |   if(!vis[adj] and sz[adj] >= sz.size()/2){
#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...