#include "stations.h"
#include <cstdio>
#include <cassert>
#include <map>
#include <vector>
#include <algorithm>
static int max_label = 0;
static int r, n, k, q;
static std::vector<int> u, v, labels, answers;
static std::map<int, int> reverse_labels;
static std::vector<std::vector<int> > adjlist;
static int s, t, w;
static std::vector<int> c;
#include "stations.h"
#include<bits/stdc++.h>
using namespace std;
vector<int>adj[1010];
int in[1010], out[1010], cnt, vis[1010];
void dfs(int v)
{
in[v]=++cnt;
for(int i=0;i<adj[v].size();i++)
{
int p=adj[v][i];
if(!vis[p])
{
vis[p]=1;
dfs(p);
}
}
out[v]=cnt;
}
vector<int>label(int n, int k, vector<int>u, vector<int>v)
{
vis[0]=1;
for(int i=1;i<n;i++)vis[i]=0;
cnt=0;
for(int i=0;i<n;i++)in[i]=out[i]=0,adj[i].clear();
for(int i=0;i<u.size();i++)
{
adj[u[i]].push_back(v[i]);
adj[v[i]].push_back(u[i]);
}
dfs(0);
vector<int>ans;
for(int i=0;i<n;i++)ans.push_back(in[i]*1000+out[i]-1001);
return ans;
}
int find_next_station(int s, int t, vector<int>c)
{
int aa=s/1000;
int bb=s%1000;
int cc=t/1000;
int dd=t%1000;
if(cc<=aa&&bb<=dd||bb<cc||dd<aa)
{
for(int i=0;i<c.size();i++)
{
int ee=c[i]/1000;
int ff=c[i]%1000;
if(ee<=aa&&bb<=ff)return c[i];
}
}
for(int i=0;i<c.size();i++)
{
int ee=c[i]/1000;
int ff=c[i]%1000;
if(ee<=cc&&dd<=ff&&!(ee<=aa&&bb<=ff))return c[i];
}
}
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 j=0;j<adjlist[s].size();j++)
{
int v=adjlist[s][j];
c.push_back(labels[v]);
}
std::sort(c.begin(), c.end());
int answer = find_next_station(labels[s], labels[t], c);
if (!std::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 i=0;i<answers.size();i++) {int index=answers[i];
printf("%d\n", index);
}
exit(0);
}
Compilation message
stations.cpp: In function 'void dfs(int)':
stations.cpp:22:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i=0;i<adj[v].size();i++)
| ~^~~~~~~~~~~~~~
stations.cpp: In function 'std::vector<int> label(int, int, std::vector<int>, std::vector<int>)':
stations.cpp:39:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i=0;i<u.size();i++)
| ~^~~~~~~~~
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:55:14: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
55 | if(cc<=aa&&bb<=dd||bb<cc||dd<aa)
| ~~~~~~^~~~~~~~
stations.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i=0;i<c.size();i++)
| ~^~~~~~~~~
stations.cpp:64:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i=0;i<c.size();i++)
| ~^~~~~~~~~
stations.cpp: In function 'int main()':
stations.cpp:109:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (int j=0;j<adjlist[s].size();j++)
| ~^~~~~~~~~~~~~~~~~~
stations.cpp:124:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int i=0;i<answers.size();i++) {int index=answers[i];
| ~^~~~~~~~~~~~~~~
stations.cpp: In function 'int find_next_station(int, int, std::vector<int>)':
stations.cpp:70:1: warning: control reaches end of non-void function [-Wreturn-type]
70 | }
| ^
/tmp/cclLEL4g.o: In function `main':
stub.cpp:(.text.startup+0x0): multiple definition of `main'
/tmp/ccHHTjLF.o:stations.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status