题目链接

题意:






题解:















图片说明






AC代码

/*
    Author : zzugzx
    Lang : C++
    Blog : blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define SZ(x) (int)x.size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1e9+7;
//const int mod = 998244353;
const double eps = 1e-10;
const double pi = acos(-1.0);
const int maxn = 1e6+10;
const ll inf = 0x3f3f3f3f;
const int dir[][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

int n,depth[maxn], f[maxn][50];
int from[maxn], to[maxn << 1], nxt[maxn << 1], cnt, Log[maxn];
int L[maxn], R[maxn], id, pos[maxn], top[maxn];
void addEdge (int u, int v) {
    to[++cnt] = v, nxt[cnt] = from[u], from[u] = cnt;
}
void dfs (int u, int fa) {
    depth[u] = depth[fa] + 1;
    L[u] = ++id;
    pos[id] = u;
    for (register int i = 1; i <= Log[n]; ++i) {
        if ((1 << i) > depth[u]) break;
        f[u][i] =  f[f[u][i - 1]][i - 1];
    }
    for (register int i = from[u]; i; i = nxt[i]) {
        ll v = to[i];
        if (v == fa) continue;
        f[v][0] = u;
        dfs (v, u);
    }
    R[u] = id;
}
inline int LCA (int x, int y) {
    if (depth[x] < depth[y]) swap(x, y);
    for(register int i = Log[n] ; i >= 0 ; --i)
        if(depth[x] - (1 << i) >= depth[y]) x = f[x][i];
    if (x == y) return x;
    for (register int i = Log[n]; i >= 0; --i)
        if (f[x][i] != f[y][i])
            x = f[x][i], y = f[y][i];
    return f[x][0];
}
void init(){
    Log[0] = -1;
    for (register int i = 1, u, v; i < n; ++i) {
        cin >> u >> v;
        addEdge (u, v); addEdge(v, u);
        Log[i] = Log[i >> 1] + 1;
    }
    Log[n] = Log[n >> 1] + 1;
    dfs(1,0);
}
int dis(int p , int q){return depth[p] + depth[q] - 2 * depth[LCA(p , q)];}
vector<int> g[maxn];
int a[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
//  freopen("in.txt", "r", stdin);
//  freopen("out.txt", "w", stdout);
    cin >> n;
    init();
    int q;
    cin >> q;
    for (int i = 1; i <= q; i++) {
        int k;
        cin >> k;
        for(int j = 1; j <= k; j++) {
            int x;
            cin >> x;
            g[i].pb(L[x]);
            if (j == 1) top[i] = x;
            else top[i] = LCA(top[i], x);
        }
        sort(all(g[i]));
    }
    cin >> q;
    while (q--) {
        int v, t;
        cin >> v >> t;
        int rt;
        for (int i = 1; i <= t; i++){
            cin >> a[i];
            if (i == 1) rt = top[a[i]];
            else rt = LCA(rt, top[a[i]]);
        }
        if (L[v] < L[rt] || L[v] > R[rt]) {
            cout << dis(v, rt) << endl;
            continue;
        }
        int ans = inf;
        for (int i = 1; i <= t; i++){
            auto p = lower_bound(all(g[a[i]]), L[v]);
            if (p != g[a[i]].end())
                ans = min(ans, dis(v, LCA(v, pos[*p])));
            if (p !=g[a[i]].begin())
                ans = min(ans, dis(v, LCA(v, pos[*prev(p)])));
        }
        cout << ans << endl;
    }
    return 0;
}