#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define len(x) (int) x.size()
#define pb push_back
mt19937 rnd(time(0));
const int N = 1e5 + 7;
const ll INF = 1e16;
bool mark1[N], mark2[N];
vector<pii> adj[N];
vector<int> S, T;
ll dis[N];
int n, q;
void read_input() {
cin >> n >> q;
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
}
ll get() {
if (len(S) > len(T))
swap(S, T);
for (int u: S)
mark1[u] = true;
for (int u: T)
mark2[u] = true;
for (int u: S)
if (mark1[u] && mark2[u]) {
for (int v: S)
mark1[v] = false;
for (int v: T)
mark2[v] = false;
return 0;
}
queue<int> q;
for (int u: S) {
dis[u] = 0;
q.push(u);
}
while (!q.empty()) {
int u = q.front();
q.pop();
if (mark2[u])
continue;
for (auto [v, w]: adj[u])
if (dis[u] + w < dis[v]) {
dis[v] = dis[u] + w;
q.push(v);
}
}
ll ans = INF;
for (int u: T)
ans = min(ans, dis[u]);
for (int i = 0; i < n; i++)
dis[i] = INF;
for (int u: S)
mark1[u] = false;
for (int u: T)
mark2[u] = false;
return ans;
}
void solve() {
fill(dis, dis + n, INF);
while (q--) {
S.clear(), T.clear();
int s, t;
cin >> s >> t;
for (int i = 0; i < s; i++) {
int u;
cin >> u;
S.pb(u);
}
for (int i = 0; i < t; i++) {
int u;
cin >> u;
T.pb(u);
}
cout << get() << '\n';
}
}
int32_t main() {
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
read_input(), solve();
return 0;
}
Compilation message
/usr/bin/ld: /tmp/ccFSx6Hl.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccWVLrNk.o:factories.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccFSx6Hl.o: in function `main':
grader.cpp:(.text.startup+0x37d): undefined reference to `Init(int, int*, int*, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x412): undefined reference to `Query(int, int*, int, int*)'
collect2: error: ld returned 1 exit status