Skip to content

Instantly share code, notes, and snippets.

@meooow25
Created April 15, 2018 01:04
Show Gist options
  • Save meooow25/3f0c93a00ccfeb1143c3ed97a3d194c8 to your computer and use it in GitHub Desktop.
Save meooow25/3f0c93a00ccfeb1143c3ed97a3d194c8 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const int MAX_K = 27;
int N, K, Q;
vector<int> g[MAX_N];
set<int> Gset;
vector<int> G;
map<int, int> pos;
int dist[MAX_K + 1][MAX_K + 1];
int dp[1 << MAX_K], ans;
void dfs(int src, int u, int p, int d) {
if (Gset.count(u)) {
if (src == K) {
pos[u] = G.size();
G.push_back(u);
}
dist[src][pos[u]] = dist[pos[u]][src] = d;
}
for (int v : g[u]) if (v != p)
dfs(src, v, u, d + 1);
}
void solve() {
dfs(K, 1, 0, 0);
for (int i = 0; i < K; i++)
dfs(i, G[i], 0, 0);
ans = N - 1;
for (int mask = 1; mask < (1 << K); mask++) {
int last = __builtin_ctz(mask);
int m = mask ^ (1 << last);
if (m == 0) {
dp[mask] = dist[K][last];
ans ^= N - 1 - dp[mask];
continue;
}
int last2 = __builtin_ctz(m);
dp[mask] = dp[m] + dist[last][last2];
ans ^= N - 1 - (dp[mask] + dist[K][last]) / 2;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N;
for (int i = 0; i < N - 1; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> K;
for (int i = 0; i < K; i++) {
int x; cin >> x;
Gset.insert(x);
}
solve();
cout << ans << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment