답안 #413537

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
413537 2021-05-28T21:01:54 Z SuhaibSawalha1 기지국 (IOI20_stations) C++17
39 / 100
1039 ms 732 KB
#include "stations.h"
#include <bits/stdc++.h>
using namespace std;
 
vector<vector<int>> adj;
vector<int> lab;
int cnt, inc;
 
void dfs (int u, int p = -1) {
	for (int v : adj[u]) {
		if (v ^ p) {
			dfs(v, u);
		}
	}
	lab[u] = cnt++;
}
 
void dfs2 (int u, int p, int lb) {
	lab[u] = lb;
	for (int v : adj[u]) {
		if (v ^ p) {
			dfs2(v, u, lb + 1);
		}
	}
}

void dfs3 (int u = 0, int p = -1, int lb = 0) {
	lab[u] = lb += pow(10, inc--);
	for (int v : adj[u]) {
		if (v ^ p) {
			dfs3(v, u, lb);
		}
	}
}
 
bool subtask2 () {
	for (int i = 0; i < (int)adj.size(); ++i) {
		vector<int> y{(i - 1) / 2, 2 * i + 1, 2 * i + 2};
		if (!i) {
			y.erase(y.begin());
		}
		sort(adj[i].begin(), adj[i].end());
		while (y.size() > adj[i].size()) {
			y.pop_back();
		}
		if (y != adj[i]) {
			return 0;
		}
	}
	return 1;
}
 
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
	adj.assign(n, {});
	lab.resize(n);
	cnt = 0;
	for (int i = 0; i < n - 1; ++i) {
		adj[u[i]].push_back(v[i]);
		adj[v[i]].push_back(u[i]);
	}
	if (subtask2()) {
		iota(lab.begin(), lab.end(), 0);
		return lab;
	}
	if (all_of(adj.begin(), adj.end(), [](auto &e) {return e.size() < 3;})) {
		for (int i = 0; i < n; ++i) {
			if (adj[i].size() == 1) {
				dfs(i);
				break;
			}
		}
		return lab;
	}
	if (k == 1e6) {
		array<int, 2> p {0};
		for (int i = 0; i < n; ++i) {
			p = max(p, {(int)adj[i].size(), i});
		}
		int lb = 1e3;
		lab[p[1]] = lb;
		for (int v : adj[p[1]]) {
			lb += 1e3;
			dfs2(v, p[1], lb);
		}
		return lab;
	}
	inc = 7;
	dfs3();
	return lab;
}

vector<int> go (int v) {
	string s = to_string(v);
	vector<int> a;
	for (int i = 7; ~i; --i) {
		if (s[7 - i] == '1') {
			a.push_back(pow(10, i));
		}
	}
	return a;
}

int first (vector<int> &a, vector<int> &b) {
	for (int i = 0; i < (int)min(a.size(), b.size()); ++i) {
		if (a[i] != b[i]) {
			return 0;
		}
	}
	if (a.size() < b.size()) {
		return b[a.size()];
	}
	return 0;
}
 
int find_next_station(int s, int t, vector<int> c) {
	if (c.size() == 1) {
		return c[0];
	}
	int o = 1e7;
	if (s >= o) {
		vector<int> a = go(s), b = go(t);
		if (int v = first(a, b)) {
			return s + v;
		}
		return s - a.back();
	}
	o = 1e3;
	if (s >= o) {
		if (s / o == t / o) {
			return s - (t < s) + (t > s);
		}
		return s == o ? t / o * o : s % o ? s - 1 : o;	
	}
	sort(c.begin(), c.end());
	vector<int> y{(s - 1) / 2, 2 * s + 1, 2 * s + 2};
	if (!s) {
		y.erase(y.begin());
	}
	while (y.size() > c.size()) {
		y.pop_back();
	}
	if (y != c) {
		return s - (t < s) + (t > s);
	}
	int e = t;
	while (e) {
		int p = (e - 1) / 2;
		if (p == s) {
			return e;
		}
		e = p;
	}
	return (s - 1) / 2;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 615 ms 528 KB Output is correct
2 Correct 537 ms 612 KB Output is correct
3 Correct 1002 ms 492 KB Output is correct
4 Correct 716 ms 400 KB Output is correct
5 Correct 773 ms 400 KB Output is correct
6 Correct 558 ms 608 KB Output is correct
7 Correct 449 ms 488 KB Output is correct
8 Correct 3 ms 464 KB Output is correct
9 Correct 5 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 506 ms 528 KB Output is correct
2 Correct 561 ms 488 KB Output is correct
3 Correct 888 ms 484 KB Output is correct
4 Correct 592 ms 488 KB Output is correct
5 Correct 798 ms 400 KB Output is correct
6 Correct 470 ms 464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 567 ms 612 KB Output is correct
2 Correct 442 ms 528 KB Output is correct
3 Correct 886 ms 400 KB Output is correct
4 Correct 785 ms 488 KB Output is correct
5 Correct 740 ms 400 KB Output is correct
6 Correct 498 ms 616 KB Output is correct
7 Correct 547 ms 528 KB Output is correct
8 Correct 3 ms 476 KB Output is correct
9 Correct 5 ms 468 KB Output is correct
10 Correct 2 ms 476 KB Output is correct
11 Correct 750 ms 400 KB Output is correct
12 Correct 503 ms 732 KB Output is correct
13 Correct 489 ms 528 KB Output is correct
14 Correct 551 ms 556 KB Output is correct
15 Correct 58 ms 468 KB Output is correct
16 Correct 63 ms 556 KB Output is correct
17 Correct 120 ms 544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 992 ms 464 KB Output is correct
2 Correct 681 ms 400 KB Output is correct
3 Correct 529 ms 488 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 5 ms 476 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 654 ms 404 KB Output is correct
8 Correct 925 ms 400 KB Output is correct
9 Correct 674 ms 488 KB Output is correct
10 Correct 684 ms 400 KB Output is correct
11 Correct 7 ms 476 KB Output is correct
12 Correct 7 ms 468 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 4 ms 468 KB Output is correct
15 Correct 2 ms 468 KB Output is correct
16 Correct 584 ms 400 KB Output is correct
17 Correct 684 ms 556 KB Output is correct
18 Correct 594 ms 524 KB Output is correct
19 Correct 606 ms 484 KB Output is correct
20 Correct 620 ms 400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 735 ms 612 KB Output is correct
2 Correct 529 ms 528 KB Output is correct
3 Correct 1039 ms 488 KB Output is correct
4 Correct 778 ms 400 KB Output is correct
5 Correct 675 ms 400 KB Output is correct
6 Correct 483 ms 616 KB Output is correct
7 Correct 574 ms 528 KB Output is correct
8 Correct 3 ms 476 KB Output is correct
9 Correct 5 ms 468 KB Output is correct
10 Correct 2 ms 468 KB Output is correct
11 Correct 497 ms 480 KB Output is correct
12 Correct 544 ms 484 KB Output is correct
13 Correct 980 ms 400 KB Output is correct
14 Correct 911 ms 616 KB Output is correct
15 Correct 832 ms 616 KB Output is correct
16 Correct 465 ms 532 KB Output is correct
17 Partially correct 844 ms 488 KB Partially correct
18 Incorrect 5 ms 404 KB Invalid labels (duplicates values). scenario=0, label=11111111
19 Halted 0 ms 0 KB -