Submission #404331

#TimeUsernameProblemLanguageResultExecution timeMemory
404331AmineTrabelsiStations (IOI20_stations)C++14
69.87 / 100
1140 ms840 KiB
#include "stations.h"
#include <bits/stdc++.h>
using namespace std;
const int Mx = 1010;
int timer = 0;
void dfs(int node,int par,vector<int> &labels,vector<vector<int>> &tr,bool parity){
    if(parity)labels[node] = timer++;
    for(auto i:tr[node]){
        if(i != par){
            dfs(i,node,labels,tr,!parity);
        }
    }
    if(!parity)labels[node] = timer++;
}
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
    vector<int> labels(n);
    vector<vector<int>> tr(n+1,vector<int>(0));
	for (int i = 0; i < n-1; i++) {
		tr[u[i]].push_back(v[i]);
        tr[v[i]].push_back(u[i]);
	}
    dfs(0,-1,labels,tr,1);
	return labels;
}
int find_next_station(int s, int t, vector<int> c) {
    if ((int)c.size() == 1) return c.back();
    if (c.front() > s){ 
        // s is tin
        int l = s, r = c[(int)c.size() - 2];
        if (l > t || r < t) return c.back(); // t out go to parent
 
        int pos = lower_bound(c.begin(), c.end(), t) - c.begin(); // go to subtree where t
 
        return c[pos];
    } else { 
        // s is tout
        int l = c[1], r = s;
        if (l > t || r < t) return c.front(); // t out go to parent
 
        int pos = upper_bound(c.begin(), c.end(), t) - c.begin() - 1; // go to subtree where t
 
        return c[pos];
    }
 
    return c[0];
}
// c sorted
// small c deeper in the tree
// big c high in tree
#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...