1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| #include <bits/stdc++.h>
#define x first #define y second #define y1 Y1 #define y2 Y2 #define mp make_pair #define pb push_back
using namespace std;
typedef long long LL; typedef pair <int, int> pii;
template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> inline T read () { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; }
inline void proc_status () { ifstream t ("/proc/self/status"); cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl; }
const int Maxn = 1e5 + 100;
int N, Q; int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1]; int fa[Maxn], dep[Maxn], dfn[Maxn], dfs_clock, size[Maxn], son[Maxn], top[Maxn]; int K, A[Maxn << 1];
inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; }
inline void dfs0 (int x) { dep[x] = dep[fa[x]] + 1, size[x] = 1;
for (int i = Begin[x]; i; i = Next[i]) { int y = To[i]; if (y == fa[x]) continue; fa[y] = x;
dfs0(y);
if (size[y] > size[son[x]]) son[x] = y; size[x] += size[y]; } }
inline void dfs1 (int x, int now) { dfn[x] = ++dfs_clock, top[x] = now;
if (son[x]) dfs1 (son[x], now);
for (int i = Begin[x]; i; i = Next[i]) { int y = To[i]; if (y == fa[x] || y == son[x]) continue;
dfs1(y, y); } }
inline int get_lca (int x, int y) { while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); x = fa[top[x]]; } if (dep[x] < dep[y]) swap(x, y); return y; }
int f[Maxn], g[Maxn], Vis[Maxn];
namespace VTREE { int Begin[Maxn], To[Maxn << 2], Next[Maxn << 2]; int top, Stack[Maxn << 1];
inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; }
inline void get_dp (int x) { f[x] = g[x] = 0; int cnt = 0, sum = 0;
for (int i = Begin[x]; i; i = Next[i]) { int y = To[i]; if (y == fa[x]) continue;
get_dp (y);
f[x] += f[y]; if (g[y]) ++cnt, sum = g[y]; }
if (Vis[x]) { f[x] += cnt; g[x] = 1; } else { if (cnt <= 1) g[x] = sum; else ++f[x], g[x] = 0; } }
inline int cmp_by_dfn (int x, int y) { return dfn[x] < dfn[y]; }
inline void build () { sort(A + 1, A + K + 1, cmp_by_dfn); for (int i = K; i > 1; --i) A[++K] = get_lca (A[i], A[i - 1]); A[++K] = 1; sort(A + 1, A + K + 1, cmp_by_dfn); K = unique (A + 1, A + K + 1) - A - 1;
for (int i = 1; i <= K; ++i) { while (top && dfn[Stack[top]] + size[Stack[top]] - 1< dfn[A[i]]) --top; if (top) add_edge (Stack[top], A[i]); Stack[++top] = A[i]; }
}
inline int work () { get_dp (1); return f[1]; }
}
inline void Init () { for (int i = 1; i <= K; ++i) Vis[A[i]] = VTREE :: Begin[A[i]] = 0; e = VTREE :: top = 0; }
inline void Solve () { dfs0(1), dfs1(1, 1);
Q = read<int>(); while (Q--) { K = read<int>(); for (int i = 1; i <= K; ++i) A[i] = read<int>(), Vis[A[i]] = 1;
int fl = 0; for (int i = 1; i <= K; ++i) if (Vis[fa[A[i]]]) {fl = 1; break; }
if (fl) puts("-1"); else { VTREE :: build (); printf("%d\n", VTREE :: work()); }
Init(); } }
inline void Input () { N = read<int>(); for (int i = 1; i < N; ++i) { int x = read<int>(), y = read<int>(); add_edge (x, y); add_edge (y, x); } }
int main() {
#ifndef ONLINE_JUDGE freopen("D.in", "r", stdin); freopen("D.out", "w", stdout); #endif
Input (); Solve ();
return 0; }
|