Submission #490546

# Submission time Handle Problem Language Result Execution time Memory
490546 2021-11-27T23:11:15 Z mraron Stations (IOI20_stations) C++14
100 / 100
876 ms 736 KB
#include "stations.h"
#include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

vector<vector<int>> adj;
vector<int> L, R, lvl;
int ind;
void dfs(int x) {
	L[x]=ind++;
	for(auto i:adj[x]) {
		if(!L[i]) {
			lvl[i]=lvl[x]+1;
			dfs(i);
		}
	}
	R[x]=ind++;
}

std::vector<int> label(int n, int k, std::vector<int> u, std::vector<int> v) {
	adj.resize(n);
	for(auto& i:adj) i.clear();
	
	for(int i=0;i<n-1;++i) {
		adj[u[i]].push_back(v[i]);
		adj[v[i]].push_back(u[i]);
		//~ cerr<<u[i]<<" "<<v[i]<<"\n";
	}
	
	L.assign(n, 0);
	R.assign(n, 0);
	lvl.assign(n, 0);
	ind=1;
	dfs(0);
	
	std::vector<int> labels(n);
	for (int i = 0; i < n; i++) {
		labels[i] = lvl[i]&1?L[i]:R[i];
		//~ std::cerr<<i<<"("<<lvl[i]<<")"<<" ";
	}
	//~ cerr<<"\n";
	
	vector<int> s(labels.begin(), labels.end());
	sort(s.begin(), s.end());
	for(int i=0;i<n;++i) labels[i]=lower_bound(s.begin(), s.end(), labels[i])-s.begin();
	
	//~ for(auto i:labels) cerr<<i<<" ";cerr<<"\n";
	
	return labels;
}

bool contains(int a, int b, int x) {
	return min(a, b)<=x && x<=max(a, b);
}


int find_next_station(int s, int t, std::vector<int> c) {
	int best=-1;
	for(int i:c) {
		if(contains(s, i, t)) {
			if(best==-1 || abs(best-s)>abs(s-i)) best=i;
		}
	}
	
	if(best==-1) {
		for(int i:c) {
			if(best==-1 || abs(best-s)<abs(s-i)) best=i;
		}
	}
	
	return best;
}
# Verdict Execution time Memory Grader output
1 Correct 473 ms 644 KB Output is correct
2 Correct 390 ms 572 KB Output is correct
3 Correct 876 ms 404 KB Output is correct
4 Correct 630 ms 488 KB Output is correct
5 Correct 501 ms 464 KB Output is correct
6 Correct 465 ms 532 KB Output is correct
7 Correct 408 ms 528 KB Output is correct
8 Correct 2 ms 476 KB Output is correct
9 Correct 4 ms 448 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 476 ms 556 KB Output is correct
2 Correct 431 ms 528 KB Output is correct
3 Correct 809 ms 400 KB Output is correct
4 Correct 698 ms 404 KB Output is correct
5 Correct 648 ms 400 KB Output is correct
6 Correct 505 ms 492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 499 ms 656 KB Output is correct
2 Correct 464 ms 520 KB Output is correct
3 Correct 740 ms 400 KB Output is correct
4 Correct 563 ms 488 KB Output is correct
5 Correct 571 ms 400 KB Output is correct
6 Correct 470 ms 656 KB Output is correct
7 Correct 401 ms 528 KB Output is correct
8 Correct 1 ms 472 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 608 ms 484 KB Output is correct
12 Correct 498 ms 688 KB Output is correct
13 Correct 383 ms 692 KB Output is correct
14 Correct 426 ms 528 KB Output is correct
15 Correct 44 ms 420 KB Output is correct
16 Correct 55 ms 532 KB Output is correct
17 Correct 90 ms 528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 752 ms 400 KB Output is correct
2 Correct 642 ms 428 KB Output is correct
3 Correct 591 ms 404 KB Output is correct
4 Correct 1 ms 476 KB Output is correct
5 Correct 3 ms 468 KB Output is correct
6 Correct 0 ms 468 KB Output is correct
7 Correct 591 ms 492 KB Output is correct
8 Correct 735 ms 488 KB Output is correct
9 Correct 623 ms 404 KB Output is correct
10 Correct 437 ms 492 KB Output is correct
11 Correct 4 ms 472 KB Output is correct
12 Correct 4 ms 524 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 3 ms 476 KB Output is correct
15 Correct 1 ms 476 KB Output is correct
16 Correct 476 ms 616 KB Output is correct
17 Correct 507 ms 484 KB Output is correct
18 Correct 421 ms 404 KB Output is correct
19 Correct 456 ms 400 KB Output is correct
20 Correct 425 ms 488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 355 ms 616 KB Output is correct
2 Correct 373 ms 664 KB Output is correct
3 Correct 697 ms 400 KB Output is correct
4 Correct 658 ms 404 KB Output is correct
5 Correct 578 ms 488 KB Output is correct
6 Correct 482 ms 616 KB Output is correct
7 Correct 439 ms 492 KB Output is correct
8 Correct 2 ms 464 KB Output is correct
9 Correct 3 ms 468 KB Output is correct
10 Correct 0 ms 468 KB Output is correct
11 Correct 426 ms 476 KB Output is correct
12 Correct 541 ms 532 KB Output is correct
13 Correct 854 ms 400 KB Output is correct
14 Correct 554 ms 488 KB Output is correct
15 Correct 551 ms 484 KB Output is correct
16 Correct 440 ms 484 KB Output is correct
17 Correct 501 ms 484 KB Output is correct
18 Correct 384 ms 608 KB Output is correct
19 Correct 363 ms 632 KB Output is correct
20 Correct 446 ms 484 KB Output is correct
21 Correct 56 ms 468 KB Output is correct
22 Correct 62 ms 532 KB Output is correct
23 Correct 104 ms 548 KB Output is correct
24 Correct 5 ms 468 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 4 ms 420 KB Output is correct
27 Correct 3 ms 420 KB Output is correct
28 Correct 2 ms 468 KB Output is correct
29 Correct 514 ms 484 KB Output is correct
30 Correct 450 ms 404 KB Output is correct
31 Correct 437 ms 404 KB Output is correct
32 Correct 462 ms 400 KB Output is correct
33 Correct 527 ms 400 KB Output is correct
34 Correct 280 ms 528 KB Output is correct
35 Correct 398 ms 604 KB Output is correct
36 Correct 434 ms 628 KB Output is correct
37 Correct 440 ms 704 KB Output is correct
38 Correct 445 ms 624 KB Output is correct
39 Correct 399 ms 608 KB Output is correct
40 Correct 377 ms 596 KB Output is correct
41 Correct 460 ms 736 KB Output is correct
42 Correct 55 ms 576 KB Output is correct
43 Correct 83 ms 548 KB Output is correct
44 Correct 140 ms 548 KB Output is correct
45 Correct 167 ms 616 KB Output is correct
46 Correct 257 ms 528 KB Output is correct
47 Correct 286 ms 660 KB Output is correct
48 Correct 60 ms 708 KB Output is correct
49 Correct 46 ms 576 KB Output is correct