This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "stations.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 1005;
vector<int> adj[SIZE];
int id[SIZE];
vector<int> label(int n, int k, vector<int> u, vector<int> v) {
vector<int> labels(n, -1);
FOR (i, 0, n - 1) {
adj[i].clear();
id[i] = -1;
}
FOR (i, 0, n - 2) {
adj[u[i]].pb(v[i]);
adj[v[i]].pb(u[i]);
}
int dfsid = 0;
auto dfs = [&](auto dfs, int pos, int fa, bool f)->void {
if (f) id[pos] = dfsid++;
for (int np : adj[pos]) if (np != fa) {
dfs(dfs, np, pos, !f);
}
if (!f) id[pos] = dfsid++;
};
dfs(dfs, 0, 0, 1);
FOR (i, 0, n - 1) labels[i] = id[i];
return labels;
}
int find_next_station(int s, int t, vector<int> c) {
sort(c.begin(), c.end());
if (s == 0) {
for (int x : c) if (t <= x) return x;
}
if (s < c[0]) {
int l = s;
for (int i = 0; i + 1 < c.size(); i++) {
if (l <= t && t <= c[i]) return c[i];
l = c[i] + 1;
}
return c.back();
}
int r = s;
for (int i = c.size() - 1; i >= 1; i--) {
if (c[i] <= t && t <= r) return c[i];
r = c[i] - 1;
}
return c[0];
}
/*
in1
1
5 10000000
0 1
1 2
1 3
2 4
2
2 0 1
1 3 3
out1
9
1
3
case1
1
5 1000000
4 2
2 3
3 0
0 1
2
2 1 3
1 4 0
case2
1
7 100000
0 1
0 2
1 3
1 4
2 5
2 6
3
4 5 1
6 0 2
1 4 4
case3
1
6 1000000
1 4
4 3
3 5
4 2
2 0
5
1 4 4
4 5 3
0 2 2
2 0 0
3 2 4
*/
#ifdef WAIMAI
static int max_label = 0;
static int r, n, k, q;
static vector<int> u, v, labels, answers;
static map<int, int> reverse_labels;
static vector<vector<int>> adjlist;
static int s, t, w;
static vector<int> c;
int main() {
assert(scanf("%d", &r) == 1);
for (int tc = 0; tc < r; tc++) {
assert(scanf("%d%d", &n, &k) == 2);
u.resize(n - 1);
v.resize(n - 1);
adjlist.clear();
adjlist.resize(n);
for (int i = 0; i < n - 1; i++) {
assert(scanf("%d%d", &u[i], &v[i]) == 2);
adjlist[u[i]].push_back(v[i]);
adjlist[v[i]].push_back(u[i]);
}
labels = label(n, k, u, v);
if ((int)labels.size() != n) {
printf("Number of labels not equal to %d\n", n);
exit(0);
}
reverse_labels.clear();
for (int i = 0; i < n; i++) {
if (labels[i] < 0 || labels[i] > k) {
printf("Label not in range 0 to %d\n", k);
exit(0);
}
if (reverse_labels.find(labels[i]) != reverse_labels.end()) {
printf("Labels not unique\n");
exit(0);
}
reverse_labels[labels[i]] = i;
if (labels[i] > max_label) {
max_label = labels[i];
}
}
assert(scanf("%d", &q) == 1);
for (int i = 0; i < q; i++) {
assert(scanf("%d%d%d", &s, &t, &w) == 3);
c.clear();
for (int v : adjlist[s]) {
c.push_back(labels[v]);
}
sort(c.begin(), c.end());
int answer = find_next_station(labels[s], labels[t], c);
if (!binary_search(c.begin(), c.end(), answer)) {
printf("Label %d returned by find_next_station not found in c\n", answer);
exit(0);
}
answers.push_back(reverse_labels[answer]);
}
}
printf("%d\n", max_label);
for (int index : answers) {
printf("%d\n", index);
}
exit(0);
}
#endif
Compilation message (stderr)
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:60:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i + 1 < c.size(); i++) {
| ~~~~~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |