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
| #include<bits/stdc++.h> #define LL long long #define PII pair<int, int> using namespace std; template <class T> inline void read(T &res) { res = 0; bool flag = 0; char c = getchar(); while('0' > c || c > '9') { if(c == '-') flag = 1; c = getchar();} while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar(); if(flag) res = -res; } template <class T, class ...Arg> inline void read(T &res, Arg &...com){ read(res), read(com...);} template <class T> void out(T res) { if(res > 9) out(res / 10); putchar(res % 10 + '0'); } template <class T> inline void write(T res) { if(res < 0) putchar('-'), res = -res; out(res), putchar('\n'); } const int N = 3e5 + 5, Inf = 1e9; int n, m, T; int idx; int e[N << 1], ne[N << 1], h[N], depth[N]; inline void add(int x, int y) { idx ++; e[idx] = y, ne[idx] = h[x], h[x] = idx; } int dfn[N], cnt, lg[N << 1], f[N << 1][22], father[N], id[N]; int hh; int a[N]; inline int lca(int x, int y) { int l = id[x], r = id[y], len; if(l > r) swap(l, r); len = lg[r - l + 1]; if(depth[f[l][len]] < depth[f[r - (1 << len) + 1][len]]) return f[l][len]; return f[r - (1 << len) + 1][len]; } int mark[N], type; namespace dp{ vector<int> edge[N]; inline void init() { for(int i = hh, o;i >= 2;i --) { o = lca(a[i], a[i - 1]); if(a[i] == o) continue; edge[o].push_back(a[i]); } } int dp(int x) { int res = 0, ans = 0, op; for(int i : edge[x]) { if(depth[i] < depth[x]) continue; op = dp(i); ans += op; if(mark[x] && mark[i]) ans ++; else if(mark[i]) res ++; } if(res > 1) ans ++; if(res == 1) mark[x] = 1; return ans; } } inline bool cmp(int x, int y) { return dfn[x] < dfn[y]; } inline void work() { read(m); hh = 0; for(int i = 1, o;i <= m;i ++) read(o), a[++ hh] = o, mark[o] = 1; for(int i = 1;i <= hh;i ++) if(mark[father[a[i]]] && father[a[i]] != a[i]) { for(int j = 1;j <= hh;j ++) mark[a[j]] = 0; return puts("-1"), void(); } sort(a + 1, a + hh + 1, cmp); for(int i = hh, o;i >= 2;i --) { o = lca(a[i], a[i - 1]); if(o == a[i] || o == a[i - 1]) continue; a[++ hh] = o; } sort(a + 1, a + hh + 1); hh = unique(a + 1, a + hh + 1) - (a + 1); sort(a + 1, a + hh + 1, cmp); dp:: init(); write(dp:: dp(a[1])); for(int i = 1;i <= hh;i ++) dp:: edge[a[i]].clear(), mark[a[i]] = 0; } int now[N << 1], tot; void dfs(int x, int y) { depth[x] = depth[y] + 1; dfn[x] = ++ cnt, now[++ tot] = x, id[x] = tot, father[x] = y; for(int i = h[x], j; ~i;i = ne[i]) { j = e[i]; if(j == y) continue; dfs(j, x); now[++ tot] = x; } } inline void init() { lg[1] = 0, lg[2] = 1; for(int i = 3;i <= tot;i ++) lg[i] = lg[i >> 1] + 1; for(int i = 1;i <= tot;i ++) f[i][0] = now[i]; for(int j = 1;j < 22;j ++) for(int i = 1;i + (1 << j) - 1 <= tot;i ++) { if(depth[f[i][j - 1]] < depth[f[i + (1 << j - 1)][j - 1]]) f[i][j] = f[i][j - 1]; else f[i][j] = f[i + (1 << j - 1)][j - 1]; } } int main() { memset(h, -1, sizeof(h)); read(n); for(int i = 1, o, u;i < n;i ++) read(o, u), add(o, u), add(u, o); dfs(1, 1); init(); read(T); while(T --) work(); return 0; }
|