Problem:
Formica Sokobanica
A new species of ant, named
Formica sokobanica
, was discovered recently. It attracted entomologists' attention due to its unique habit. These ants do not form colonies. Rather, individuals
make up their private nests and keep their food, nuts, in the nests. A nest comprises a lot
of small rooms connected with tunnels. They build the rooms only a little bigger than a nut
leaving just enough space for air flow; they cannot enter a room with a nut in it. To save the
labor, tunnels are so narrow that it exactly fits the size of a nut, and thus nuts should not be left in the tunnels to allow air flow through.
To enter a room with a nut in it, the nut has to be pushed away to any of the vacant rooms
adjacent to that room through the tunnel connecting them. When no adjacent rooms are vacant
except the room which the ant came from, the nut cannot be pushed away, and thus the ant
cannot enter the room.
Dr.Myrmink, one of the entomologists enthused about the ants, has drawn a diagram of a
typical nest. The diagram also shows which rooms store nuts in them, and which room the ant
is initially in. Your job is to write a program that counts up how many rooms the ant can reach
and enter. Pushing a nut into one of the vacant adjacent rooms may make some of the rooms
unreachable, while choosing another room to push into may keep the rooms reachable. There
can be many combinations of such choices. In such cases, all the rooms should be counted that
are possibly reached by one or more choice combinations.
You may assume that there is no nut in the room the ant is initially in, and that there is no
cyclic path in the nest.
Input
The input consists of a single test case of the following format, representing the diagram of a
nest.
$n$ $m$
$x_1$ $y_1$
...
$x_{n-1}$ $y_{n-1}$
$a_1$
...
$a_m$
Here, $n$ and $m$ are the numbers of rooms and nuts. They satisfy $1 \leq n \leq 2 \times 10^5$ and $0 \leq m < n$.
														Rooms are numbered from 1 to $n$. The ant is initially in the room 1.
Each of the following $n - 1$ lines has two integers $x_i$ and $y_i$ ($1 \leq i \leq n - 1$) indicating that a tunnel connects rooms numbered $x_i$ and $y_i$. $1 \leq x_i \leq n, 1 \leq y_i \leq n$, and $x_i \ne y_i$ hold. No two tunnels connect the same pair of rooms.
Each of the remaining $m$ lines has an integer $a_k$ ($1 \leq k \leq m, 2 \leq a_k \leq n$) which indicates that the room numbered $a_k$ has a nut in it. The numbers $a_k$ 's are all distinct.
Output
The output should be a single line containing a single integer, the number of rooms that the ant can reach and enter.
Sample Input 1
7 2
1 2
2 3
3 4
4 5
5 6
5 7
2
4
Sample Output 1
2
Sample Input 2
8 2
1 2
2 3
3 4
2 5
5 6
6 7
2 8
2
6
Sample Output 2
7
Sample Input 3
7 3
1 2
2 3
3 4
3 5
4 6
5 7
2
4
5
Sample Output 3
2
Sample Input 4
11 3
1 2
2 3
3 4
2 5
5 6
6 7
7 8
6 9
9 10
6 11
2
3
7
Sample Output 4
9

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)

int main() {
    ll n, m; cin >> n >> m;
    vector<vector<ll>> g(n);
    rep(i, n - 1) {
        ll x, y; cin >> x >> y;
        --x, --y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    vector<bool> nuts(n, false);
    rep(i, m) {
        ll a; cin >> a;
        --a;
        nuts[a] = true;
    }

    auto dfs = [&](auto dfs, ll v, ll p, ll cnt) -> ll {
        if (nuts[v]) cnt++;
        assert(cnt <= 1);

        ll deg = g[v].size();
        ll sum = 0;
        rep(i, deg) {
            if (g[v][i] == p) continue;
            if (!nuts[g[v][i]]) sum++;
        }

        ll res = 0;
        bool ok = false;
        rep(i, deg) {
            if (g[v][i] == p) continue;
            ll ncnt = cnt;
            if (!nuts[g[v][i]]) {
                if (ncnt > 0 && sum - 1 > 0) {
                    ncnt -= 1;
                }
            }
            else {
                if (ncnt > 0 && sum > 0) {
                    ncnt -= 1;
                }
            }
            if (ncnt > 0 && nuts[g[v][i]]) continue;

            res += dfs(dfs, g[v][i], v, ncnt);
            ok = true;
        }

        if (!ok) { 
            return res + (1 - cnt);
        }
        else {
            return res + 1;
        }
    };
    ll ans = dfs(dfs, 0, -1, 0);

    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 36120
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

const ll INF = 1e18;
ll mod = 998244353;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if (argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    ll n, m;
    cin >> n >> m;

    vector<vector<ll>> g(n);
    vector<char> nut(n, 0);
    for (int i = 0; i < n - 1; i++) {
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for (int i = 0; i < m; i++) {
        ll a;
        cin >> a;
        a--;
        nut[a] = 1;
    }

    ll ans = 0;
    auto dfs = [&](auto self, ll now, ll par, bool has) -> void {
        ll emp = 0;
        for (auto& x : g[now]) {
            if (x == par)
                continue;
            emp += nut[x] == 0;
        }
        for (auto& x : g[now]) {
            if (x == par)
                continue;
            if (has == false || emp)
                self(self, x, now, (has & (emp == 1)) | nut[x]);
        }
        if (emp || has == false) {
            ans++;
        }
    };
    dfs(dfs, 0, -1, false);
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 25648
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    for (int i = 1, u, v; i < N; i++) {
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<bool> A(N);
    for (int a; M--; ) {
        cin >> a;
        A[--a] = true;
    }
    auto dfs = [&] (auto dfs, int u, int p, int k) -> int {
        if (A[u]) {
            int cnt = 0;
            for (int v : G[u]) {
                if (v != p && !A[v]) cnt++;
            }
            if (cnt) {
                A[u] = false;
                if (cnt == 1) {
                    for (int v : G[u]) {
                        if (v != p && !A[v]) A[v] = true;
                    }
                }
            }
        }
        k += A[u];
        int res = 0;
        if (k == 0 || (k <= 1 && !A[u])) {
            res++;
            for (int v : G[u]) {
                if (v != p) {
                    res += dfs(dfs, v, u, k);
                }
            }
        }
        return res;
    };
    cout << dfs(dfs, 0, -1, 0) << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 33076
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    for (int i = 1, u, v; i < N; i++) {
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<bool> A(N);
    for (int a; M--; ) {
        cin >> a;
        A[--a] = true;
    }
    auto dfs = [&] (auto dfs, int u, int p, int k) -> int {
        if (A[u]) {
            int cnt = 0;
            for (int v : G[u]) {
                if (v != p && !A[v]) cnt++;
            }
            if (cnt) {
                A[u] = false;
                if (cnt == 1) {
                    for (int v : G[u]) {
                        if (v != p && !A[v]) A[v] = true;
                    }
                }
            }
        }
        int res = 0;
        k += A[u];
        for (int v : G[u]) {
            if (v != p) {
                res += dfs(dfs, v, u, k);
            }
        }
        if (k == 0 || (k <= 1 && !A[u])) res++;
        return res;
    };
    cout << dfs(dfs, 0, -1, 0) << endl;
}
accuracy: 0.4666666666666667
time_ms: 30
memory_kb: 33072
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

bool chmin(auto &a, auto b) { return a > b ? a = b, true : false; }
bool chmax(auto &a, auto b) { return a < b ? a = b, true : false; }

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M;
    cin >> N >> M;
    vector<vector<int>> G(N);
    for (int i = 1, u, v; i < N; i++) {
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    vector<bool> A(N);
    for (int a; M--; ) {
        cin >> a;
        A[--a] = true;
    }
    auto dfs = [&] (auto dfs, int u, int p, int k) -> int {
        if (A[u]) {
            int cnt = 0;
            for (int v : G[u]) {
                if (v != p && !A[v]) cnt++;
            }
            if (cnt) {
                A[u] = false;
                if (cnt == 1) {
                    for (int v : G[u]) {
                        if (v != p && !A[v]) A[v] = true;
                    }
                }
            }
        }
        bool fn = false;
        int res = 0;
        k += A[u];
        for (int v : G[u]) {
            if (v != p) {
                res += dfs(dfs, v, u, k);
                if (!A[v]) fn = true;
            }
        }
        if (k == 0 || (k <= 1 && fn)) res++;
        return res;
    };
    cout << dfs(dfs, 0, -1, 0) << endl;
}
accuracy: 0.4666666666666667
time_ms: 30
memory_kb: 33072
final_rank: 15

code_snippet: #include<bits/extc++.h>
#define rep(i,n) for(int i = 0; i<(n); i++)
#define all(a) begin(i),end(i)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int mod = 998244353;
const int inf = (1<<30);
const ll INF = (1ull<<62);
const int dx[8] = {1,0,-1,0,1,1,-1,-1};
const int dy[8] = {0,1,0,-1,1,-1,1,-1};
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b){return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmix(T1 &a, T2 b){return a > b && (a = b, true);}


int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout.setf(ios_base::fixed);
    cout.precision(12);
    int n,m; cin>>n>>m;
    vector<int> vis(n);
    vector<vector<int>> g(n);

    rep(i,n-1){
        int a,b; cin>>a>>b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int> nut(n);
    rep(i,m){
        int a; cin>>a;
        a--;
        nut[a] = 1;
    }
    int ans = 0;
    auto dfs = [&](auto dfs,int i,int p)-> void{

        vis[i] = 1;
        int np = p;
        if(nut[i] == 1) np = 1;
        int x = 0;
        for(auto j : g[i]){
           if(vis[j] == 1) continue;
           if(nut[j] == 1) continue;
              x++;
        }
        if(np == 1 && x == 0) return;
        ans++;
        if(x >= 2) np = 0;


        for(auto j : g[i]){
            if(vis[j] == 1) continue;
            dfs(dfs,j,np);
        }
    };
    dfs(dfs,0,0);
    cout<<ans<<endl;
}
accuracy: 1
time_ms: 40
memory_kb: 31176
final_rank: 6

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}

struct e{
  int to,we;
};

int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n,m;cin >> n >> m;
  vvi v(n);
  rep(i,0,n-1){
    int a,b;cin >> a >> b;
    a--;b--;
    v[a].emplace_back(b);
    v[b].emplace_back(a);
  }
  vb nuts(n,0);
  rep(i,0,m){
    int a;cin >> a;
    nuts[a-1] = 1;
  }
  vvi dp(n,vi(2));
  auto dfs = [&](auto &dfs,int ov,int pre)->void{
    int C = 0;
    for(auto nv : v[ov]){
      if(nv == pre)continue;
      dfs(dfs,nv,ov);
      C += !nuts[nv];
      dp[ov][0] += dp[nv][nuts[nv]];
    }
    dp[ov][0]++;
    if(C >= 2){
      for(auto nv : v[ov]){
        if(nv == pre)continue;
        dp[ov][1] += dp[nv][nuts[nv]];
      }
      dp[ov][1]++;
    }else if(C == 1){
      for(auto nv : v[ov]){
        if(nv == pre)continue;
        dp[ov][1] += dp[nv][1];
      }
      dp[ov][1]++;
    }else{
      dp[ov][1] = 0;
    }
  };
  dfs(dfs,0,-1);
  cout << dp[0][0] << endl;

  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 43712
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define sz(x) ((int)(x).size())
#define all(x) begin(x), end(x)
#define rep(i, l, r) for (int i = (l), i##end = (r); i <= i##end; ++i)
#define per(i, l, r) for (int i = (l), i##end = (r); i >= i##end; --i)
#ifdef memset0
#define log(...) fprintf(stderr, __VA_ARGS__)
#else
#define log(...) (void(0))
#define endl '\n'
#endif
using namespace std;
using ll = long long;
using lf = long double;
using lll = __int128;
using ull = unsigned long long;

const int N = 2e5 + 9;
int n, m, ans, a[N];
vector<int> G[N];

void solve(int u, int fa) {
  if (a[u]) {
    vector<int> empty;
    for (int v : G[u])
      if (v != fa) {
        if (!a[v]) {
          empty.push_back(v);
        }
      }
    if (empty.empty()) {
      return;
    }
    if (empty.size() == 1) {
      a[empty[0]] = 1;
      a[u] = 0;
    } else {
      a[u] = 0;
    }
  }
  for (int v : G[u])
    if (v != fa) {
      solve(v, u);
    }
}

void count(int u, int fa) {
  if (a[u]) {
    return;
  }
  ++ans;
  for (int v : G[u])
    if (v != fa) {
      count(v, u);
    }
}

int main() {
#ifdef memset0
  freopen("J.in", "r", stdin);
#endif
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int u, v, i = 1; i < n; i++) {
    cin >> u >> v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }
  for (int x, i = 1; i <= m; i++) {
    cin >> x;
    a[x] = 1;
  }
  solve(1, -1);
  count(1, -1);
  cout << ans << endl;
}
accuracy: 1
time_ms: 50
memory_kb: 40180
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 1000;
struct node{int to,next;}e[maxn * 2];
int cnt,n,k,ans;
int head[maxn],siz[maxn];
bool barrier[maxn],one[maxn];
void add(int u,int v){
	e[++cnt].to = v;
	e[cnt].next = head[u];
	head[u] = cnt;
}
void dfs1(int now,int fa){
	siz[now] = 1;
	int cnt = 0;
	for(int i = head[now];i;i = e[i].next){
		int to = e[i].to;
		if(to == fa) continue;
		cnt++;
		dfs1(to,now);
		siz[now] += siz[to];
	}
	if(cnt == 1){one[now] = 1;}
	return;
}
//bool check1(int now,int fa){//无障碍
//	for(int i = head[now];i;i = e[i].next){
//		int to = e[i].to;
//		if(to == fa) continue;
//		if(barrier[to]) return false;
//	}
//	return true;
//}
bool check1(int now,int fa){//全障碍
	for(int i = head[now];i;i = e[i].next){
		int to = e[i].to;
		if(to == fa) continue;
		if(!barrier[to]) return false;
	}
	return true;
}
void dfs2(int now,int fa){
	if(barrier[now] && check1(now,fa)){ans -= siz[now];return;}
	for(int i = head[now];i;i = e[i].next){
		int to = e[i].to;
		if(to == fa) continue;
		if(one[now] && barrier[now]){barrier[now] = 0;barrier[to] = 1;}
		dfs2(to,now);
	}
	return;
}
int main(){
	//freopen("","r",stdin);
	//freopen("","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin >> n >> k;
	ans = n;
	for(int i = 1;i <= n - 1;i++){
		int u,v;cin >> u >> v;
		add(u,v);add(v,u);
	}
	for(int i = 1;i <= k;i++){
		int a;cin >> a;
		barrier[a] = 1;
	}
	dfs1(1,0);
	dfs2(1,0);
	cout << ans << endl;
	return 0;
}
accuracy: 0.4666666666666667
time_ms: 30
memory_kb: 20972
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define fre {freopen("swaying.in","r",stdin);freopen("swaying,out","w",stdout);}
#define io {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define MAXN 200005
using namespace std;

int n,k;
vector<int> g[MAXN];
int stop[MAXN];
int vis[MAXN];

void dfs(int u,int fa){
    vis[u] = 1;
    for(int i = 0;i < g[u].size();i++){
        int v = g[u][i];
        if(v == fa) continue;
        if(stop[v]){
            int cnt = 0;
            int pos = -1;
            for(int j = 0;j < g[v].size();j++){
                int v1 = g[v][j];
                if(stop[v1]) cnt++;
                else pos = v1;
            }
            if(g[v].size() - 1 - cnt == 0) continue;
            else if(g[v].size() - 1 - cnt == 1){
                stop[pos] = 1;
                stop[v] = 0;
                dfs(v,u);
            } else if(g[v].size() - 1 - cnt >= 2){
                stop[v] = 0;
                dfs(v,u);
            }
        } else dfs(v,u);
    }
    return ;
}

int main(){
    // fre;
    // io;
    cin >> n >> k;
    for(int i = 0;i < n-1;i++){
        int u,v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i = 0;i < k;i++){
        int x;
        cin >> x;
        stop[x] = 1;
    }
    dfs(1,0);
    int ans = 0;
    for(int i = 1;i <= n;i++) if(vis[i]) ans++;
    cout << ans << endl;
    return 0;
}
accuracy: 0.13333333333333333
time_ms: 80
memory_kb: 24424
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define fre {freopen("swaying.in","r",stdin);freopen("swaying,out","w",stdout);}
#define io {ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define MAXN 200005
using namespace std;

int n,k;
vector<int> g[MAXN];
int stop[MAXN];
int vis[MAXN];

void dfs(int u,int fa){
    vis[u] = 1;
    for(int i = 0;i < g[u].size();i++){
        int v = g[u][i];
        if(v == fa) continue;
        if(stop[v]){
            int cnt = 0;
            int pos = -1;
            for(int j = 0;j < g[v].size();j++){
                int v1 = g[v][j];
                if(stop[v1]) cnt++;
                else pos = v1;
            }
            if(g[v].size() - 1 - cnt == 0){
                continue;
            } else if(g[v].size() - 1 - cnt == 1){
                stop[pos] = 1;
                stop[v] = 0;
                dfs(v,u);
            } else if(g[v].size() - 1 - cnt >= 2){
                stop[v] = 0;
                dfs(v,u);
            }
        } else {
            dfs(v,u);
        }
    }
    return ;
}

int main(){
    // fre;
    io;
    cin >> n >> k;
    for(int i = 0;i < n-1;i++){
        int u,v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for(int i = 0;i < k;i++){
        int x;
        cin >> x;
        stop[x] = 1;
    }
    dfs(1,0);
    int ans = 0;
    for(int i = 1;i <= n;i++){
        if(vis[i]) ans++;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.13333333333333333
time_ms: 40
memory_kb: 24412
final_rank: 18

code_snippet: #include<bits/stdc++.h>
#define N 1e6+10
using namespace std;
namespace Octane {
	// non terrae plus ultra
#define OCTANE
#define BUFFER_SIZE 100000
#define ll long long 
#define db double
#define ldb long double
	char ibuf[BUFFER_SIZE], obuf[BUFFER_SIZE];
	char *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,BUFFER_SIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+BUFFER_SIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif // fread in OJ, getchar in local
	
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33&&ch!=EOF)
	
	const ll pow10[] = {
		(ll)1e0,  (ll)1e1,  (ll)1e2,  (ll)1e3,  (ll)1e4,  (ll)1e5, 
		(ll)1e6,  (ll)1e7,  (ll)1e8,  (ll)1e9,  (ll)1e10, (ll)1e11,
		(ll)1e12, (ll)1e13, (ll)1e14, (ll)1e15, (ll)1e16, (ll)1e17, (ll)1e18,
	};
	
	struct Octane_t {
		~Octane_t() {
			fwrite(obuf, p3-obuf, 1, stdout);
		}
		bool flag = false;
		operator bool() {
			return flag;
		}
	}io;
	
	template<typename T> inline T read() {
		T s = 0; int w = 1; char ch;
		while(ch=getchar(), !isdigit(ch)&&(ch!=EOF))
			if(ch == '-') w = -1;
		if(ch == EOF) return 0;
		while(isdigit(ch))
			s = s*10+ch-48, ch=getchar();
		if(ch == '.') {
			ll flt = 0; int cnt = 0;
			while(ch=getchar(), isdigit(ch))
				if(cnt < 18) flt=flt*10+ch-48, cnt++;
			s += (db)flt/pow10[cnt];
		}
		return s *= w;
	}
	template<typename T> inline bool read(T &s) {
		s = 0; int w = 1; char ch;
		while(ch=getchar(), !isdigit(ch)&&(ch!=EOF))
			if(ch == '-') w = -1;
		if(ch == EOF) return false;
		while(isdigit(ch))
			s = s*10+ch-48, ch=getchar();
		if(ch == '.') {
			ll flt = 0; int cnt = 0;
			while(ch=getchar(), isdigit(ch))
				if(cnt < 18) flt=flt*10+ch-48, cnt++;
			s += (db)flt/pow10[cnt];
		}
		return s *= w, true;
	}
	inline bool read(char &s) {
		while(s = getchar(), isspace(s));
		return s != EOF;
	}
	inline bool read(char *s) {
		char ch;
		while(ch=getchar(), isspace(ch));
		if(ch == EOF) return false;
		while(!isspace(ch))
			*s++ = ch, ch=getchar();
		*s = '\000';
		return true;
	} 
	template<typename T> void print(T x) {
		static int t[20]; int top = 0;
		if(x < 0) putchar('-'), x = -x;
		do { t[++top] = x%10; x /= 10; } while(x);
		while(top) putchar(t[top--]+48);
	}
	struct empty_type{}; int pcs = 8;
	empty_type setpcs(int cnt) {
		return pcs = cnt, empty_type();
	}
	inline void print(empty_type x){}
	inline void print(double x) {
		if(x < 0) putchar('-'), x = -x;
		x += 5.0 / pow10[pcs+1];
		print((ll)(x)); x -= (ll)(x);
		if(pcs != 0) putchar('.');
		for(int i = 1; i <= pcs; i++)
			x *= 10, putchar((int)x+'0'), x -= (int)x;
	}
	inline void print(float x) {
		if(x < 0) putchar('-'), x = -x;
		x += 5.0 / pow10[pcs+1];
		print((ll)(x)); x -= (ll)(x);
		if(pcs != 0) putchar('.');
		for(int i = 1; i <= pcs; i++)
			x *= 10, putchar((int)x+'0'), x -= (int)x;
	}
	inline void print(char x) {
		putchar(x);
	}
	inline void print(char *x) {
		for(int i = 0; x[i]; i++)
			putchar(x[i]);
	}
	inline void print(const char *x) {
		for(int i = 0; x[i]; i++)
			putchar(x[i]);
	}
	
	// support for string
#ifdef _GLIBCXX_STRING
	inline bool read(std::string& s) {
		s = ""; char ch;
		while(ch=getchar(), isspace(ch));
		if(ch == EOF) return false;
		while(!isspace(ch))
			s += ch, ch = getchar();
		return true;
	}
	inline void print(std::string x) {
		for(string::iterator i = x.begin(); i != x.end(); i++)
			putchar(*i);
	}
	inline bool getline(Octane_t &io, string s) {
		s = ""; char ch = getchar();
		if(ch == EOF) return false;
		while(ch != '\n' and ch != EOF)
			s += ch, ch = getchar();
		return true;
	}
#endif 
	
	// support for initializer_list
#if __cplusplus >= 201103L 
	template<typename T, typename... T1>
	inline int read(T& a, T1& ...other) {
		return read(a)+read(other...);
	}
	template<typename T, typename... T1>
	inline void print(T a, T1... other) {
		print(a); print(other...);
	}
#endif 
	
	//  give up iostream
	template<typename T>
	Octane_t& operator >> (Octane_t &io, T &b) {
		return io.flag=read(b), io;
	}
	Octane_t& operator >> (Octane_t &io, char *b) {
		return io.flag=read(b), io;
	}
	template<typename T>
	Octane_t& operator << (Octane_t &io, T b) {
		return print(b), io;
	}
	
#define cout io
#define cin io
#define endl '\n'
#undef ll
#undef db
#undef ldb
#undef BUFFER_SIZE
}
using namespace Octane;
long long n,k;
int w[200100],ans[200100];
vector<int> g[200100];
void dfs(int u,int fa,int st){//st代表u有无箱子
	ans[u]=1;
	int flag=0;
	for(int i=0;i<g[u].size();i++){
		int v=g[u][i];
		if(v==fa) continue;
		if(w[v]==0){
			flag++;
		}
	}
	if(flag>=2){
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i];
			if(v==fa) continue;
			dfs(v,u,w[v]);
		}
	}
/*	else{
		if((st==1&&flag==0)||(st==1&&g[u].size()==1)){
			ans[u]=0;
			return ;
		}
		else if(st==0&&w[u]==0){
			for(int i=0;i<g[u].size();i++){
				int v=g[u][i];
				if(v==fa) continue;
				dfs(v,u,w[v]);
			}
		}
		else if(st==0&&w[u]==1){
			for(int i=0;i<g[u].size();i++){
				int v=g[u][i];
				if(v==fa) continue;
				dfs(v,u,1);
			}
		}
		else if(st==1&&flag==1){
			for(int i=0;i<g[u].size();i++){
				int v=g[u][i];
				if(v==fa) continue;
				dfs(v,u,1);
			}
		}
	}*/
	else{
		if((st==1&&flag==0)||(st==1&&g[u].size()==1)){//u有箱子且无退路
			ans[u]=0;
			return ;
		}
		else if(st==0){//u无箱子
			for(int i=0;i<g[u].size();i++){
				int v=g[u][i];
				if(v==fa) continue;
				dfs(v,u,w[v]);
			}
		}
		else if(st==1&&flag==1){//u有箱子，u有一格空，推到下一格
			for(int i=0;i<g[u].size();i++){
				int v=g[u][i];
				if(v==fa) continue;
				dfs(v,u,1);
			}
		}
	}
	return ;
}
int main(){
	cin>>n>>k;
	for(int i=1;i<n;i++){
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	for(int i=1;i<=k;i++){
		int x;
		cin>>x;
		w[x]=1;
	}
	dfs(1,0,0);
	int anst=0;
	for(int i=1;i<=n;i++){
		//if(ans[i]==1) cout<<i<<" ";
		anst+=ans[i];
	}
	cout<<anst<<endl;
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 25208
final_rank: 1

code_snippet: // AOJ #1420 Formica Sokobanica
// 2025.4.17

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int main(){
    int n = Cin(), m = Cin();
    vector<vector<int>> g(n+1), ch(n+1);
    for(int i = 1; i < n; i++){
        int u = Cin(), v = Cin();
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<char> nut(n+1,0);
    for(int i = 0; i < m; i++) nut[Cin()] = 1;

    vector<int> par(n+1,-1);
    queue<int> q;
    par[1]=0;
    q.push(1);
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int v:g[u]){
            if(par[v]==-1){
                par[v]=u;
                ch[u].push_back(v);
                q.push(v);
            }
        }
    }
    vector<int> ce(n+1,0), oc(n+1,0);
    for(int u=1;u<=n;u++){
        for(int v:ch[u]){
            if(!nut[v]){
                if(ce[u]==0) oc[u]=v;
                ce[u]++;
            }
        }
    }
    vector<char> vis(n+1,0);
    ll cnt = 0;
    vector<pair<int,char>> st;
    st.reserve(n);
    st.emplace_back(1,0);
    while(!st.empty()){
        auto [u,in]=st.back(); st.pop_back();
        bool has = in | (nut[u]!=0);
        if(has && ce[u]==0) continue;
        if(vis[u]) continue;
        vis[u]=1;
        cnt++;
        if(has && ce[u]==1){
            int only = oc[u];
            for(int v:ch[u]){
                char ni = (v==only ? 1 : nut[v]);
                st.emplace_back(v, ni);
            }
        } else  for(int v:ch[u]) st.emplace_back(v, nut[v]);
    }
    cout << cnt << endl;
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 27776
final_rank: 3

code_snippet: #include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <numeric>
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
int N, M;
bool A[200020];
std::vector<int> g[200020];
int solve() {
    std::vector dp(2, std::vector<bool>(N));        
    auto dfs = [&](auto dfs, bool f, int v, int p) -> void {
        dp[f][v] = true;
        int empty = 0;
        for (int x : g[v]) if (x != p and !A[x]) empty++;
        if (empty) dp[false][v] = true;
        for (int x : g[v]) if (x != p) {
            if (!f) dfs(dfs, A[x], x, v);
            else {
                if (!A[x]) {
                    assert(empty >= 1);
                    dfs(dfs, empty == 1, x, v);
                }
                else if (empty >= 1) {
                    dfs(dfs, true, x, v);
                }
            }
        } 
    };
    dfs(dfs, false, 0, -1);
    int ans = 0;
    for (int i = 0 ; i < N ; i++) ans += dp[false][i];
    return ans;
}
int brute() {
    std::vector dp(N, std::vector<bool>(1 << N));
    std::queue<std::pair<int, int>> que;
    int msk = 0;
    for (int i = 0 ; i < N ; i++) msk |= int(A[i]) << i;
    que.push({0, msk});
    dp[0][msk] = true;
    auto relax = [&](int v, int bit) {
        assert(0 <= v and v < N);
        assert(!(bit & (1 << v)));
        if (!dp[v][bit]) {
            dp[v][bit] = true;
            que.push({v, bit});
        }
    };
    while (que.size()) {
        auto [v, bit] = que.front();
        que.pop();
        assert((bit & (1 << v)) == 0);
        for (int x : g[v]) {
            if (bit & (1 << x)) {
                for (int r : g[x]) if (r != v and !(bit & (1 << r))) {
                    relax(x, bit ^ (1 << x) ^ (1 << r));
                }
            }
            else {
                relax(x, bit);
            }
        }
    }
    int ans = 0;
    for (int i = 0 ; i < N ; i++) {
        bool fn = false;
        for (int b = 0 ; b < (1 << N) ; b++) {
            fn |= dp[i][b];
        }
        ans += fn;
    }
    return ans;
}
#include <random>
std::mt19937 mt{std::random_device{}()};
int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);
    // while (true) {
    //     static int cnt = 0;
    //     std::cout << "-------------------" << cnt++ << "------------------" << std::endl;
    //     N = mt() % 20 + 1;
    //     for (int i = 0 ; i < N ; i++) g[i].clear();
    //     for (int i = 1 ; i < N ; i++) {
    //         A[i] = false;
    //         A[i] = mt() % 2 == 0;
    //     }
    //     std::cout << N << ' ' << std::count(A, A + N, true) << '\n';
    //     for (int i = 1 ; i < N ; i++) {
    //         int p = mt() % i;
    //         std::cout << p + 1 << ' ' << i + 1 << '\n';
    //         g[i].push_back(p);
    //         g[p].push_back(i);
    //     }
    //     for (int i = 0 ; i < N ; i++) if (A[i]) std::cout << i + 1 << '\n';
    //     std::cout.flush();
    //     int my = solve(), ans = brute();
    //     if (my != ans) {
    //         std::cout << my << " but " << ans << std::endl;
    //         std::exit(0);
    //     }
    // }
    std::cin >> N >> M;
    for (int i = 0 ; i < N - 1 ; i++) {
        int u, v;
        std::cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 0 ; i < M ; i++) {
        int a;
        std::cin >> a;
        A[a - 1] = true;
    }
    std::cout << solve() << '\n';
}
accuracy: 1
time_ms: 30
memory_kb: 30256
final_rank: 4

code_snippet: // AOJ #1420 Formica Sokobanica
// 2025.3.27

#include <bits/stdc++.h>
using namespace std;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) {
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

int main() {
    int n = Cin(), m = Cin();

    vector<vector<int>> adj(n+1);
    for (int i = 1; i <= n - 1; i++){
        int u = Cin(), v = Cin();
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    vector<bool> nut(n+1, false);
    for (int i = 0; i < m; i++) nut[Cin()] = true;

    vector<int> par(n+1, 0);
    vector<vector<int>> child(n+1);
    vector<bool> vis(n+1, false);
    deque<int> dq;
    dq.push_back(1);
    vis[1] = true;
    par[1] = 0;
    while(!dq.empty()){
        int cur = dq.front(); dq.pop_front();
        for(auto nx : adj[cur]){
            if(!vis[nx]){
                vis[nx] = true;
                par[nx] = cur;
                child[cur].push_back(nx);
                dq.push_back(nx);
            }
        }
    }
    vector<bool> mov(n+1, false);
    vector<int> order;
    order.reserve(n);
    {
        vector<int> st;
        st.push_back(1);
        while(!st.empty()){
            int u = st.back(); st.pop_back();
            order.push_back(u);
            for(auto v : child[u]) st.push_back(v);
        }
    }
    reverse(order.begin(), order.end());
    for(auto u : order){
        if(!nut[u]) mov[u] = true;
        else {
            bool ok = false;
            for(auto v : child[u]) if(mov[v]) { ok = true; break; }
            mov[u] = ok;
        }
    }
    vector<int> cand(n+1, 0);
    for (int u = 1; u <= n; u++){
        if(nut[u]){
            int cnt = 0;
            for(auto v : child[u]) if(mov[v]) cnt++;
            cand[u] = cnt;
        }
    }

    vector<bool> reach(n+1, false);
    function<void(int)> dfs = [&](int u){
        for(auto v : child[u]){
            bool ok = true;
            if(nut[u]){
                if(!nut[v]){
                    if(cand[u] < 2) ok = false;
                } else {
                    if(cand[u] < 1) ok = false;
                }
            }
            if(ok){
                if(nut[v] && cand[v] < 1) ok = false;
            }
            if(ok && !reach[v]){
                reach[v] = true;
                dfs(v);
            }
        }
    };
    reach[1] = true;
    dfs(1);

    int ans = 0;
    for (int i = 1; i <= n; i++) if(reach[i]) ans++;
    Cout(ans);
    return 0;
}
accuracy: 0.13333333333333333
time_ms: 30
memory_kb: 27176
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

int N, A[1 << 19], B[1 << 19];
int M; bool Nut[1 << 19];
int Count = 0;
vector<int> G[1 << 19];

void dfs(int pos, int prv, bool nut) {
    if (Nut[pos] == true) nut = true;
    int vacant_count = 0;

    // Search
    for (int idx : G[pos]) {
        if (idx == prv) continue;
        if (Nut[idx] == false) vacant_count += 1;
    }
    if (nut == true && vacant_count == 0) return;
    Count += 1;

    // DFS
    for (int idx : G[pos]) {
        if (idx == prv) continue;
        if (vacant_count == 1 && nut == true) {
            dfs(idx, pos, true);
        }
        else {
            dfs(idx, pos, false);
        }
    }
}

int main() {
    // Step 1. Input
    cin >> N >> M;
    for (int i = 0; i < N - 1; i++) cin >> A[i] >> B[i];
    for (int i = 0; i < M; i++) {
        int x; cin >> x;
        Nut[x] = true;
    }
    for (int i = 0; i < N - 1; i++) G[A[i]].push_back(B[i]);
    for (int i = 0; i < N - 1; i++) G[B[i]].push_back(A[i]);

    // Step 2. Solve
    dfs(1, -1, false);
    cout << Count << endl;
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 38468
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define ov4(a, b, c, d, name, ...) name
#define rep3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep2(i, a, b) rep3(i, a, b, 1)
#define rep1(i, n) rep2(i, 0, n)
#define rep0(n) rep1(aaaaa, n)
#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for(ll i = (a)-1; i >= (b); i--)
#define fore(e, v) for(auto&& e : v)
#define all(a) begin(a), end(a)
#define si(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back

template<typename T, typename S> bool chmin(T& a, const S& b) { return a > b ? a = b, 1 : 0; }
template<typename T, typename S> bool chmax(T& a, const S& b) { return a < b ? a = b, 1 : 0; }

const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;

#define i128 __int128_t

struct _ {
    _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

int main() {
    ll N, M;
    cin >> N >> M;
    using vvl = vector<vl>;
    vvl G(N);
    rep(i, N - 1) {
        ll a, b;
        cin >> a >> b;
        a--, b--;
        G[a].eb(b);
        G[b].eb(a);
    }
    vector<bool> block(N);
    rep(i, M) {
        ll a;
        cin >> a;
        block[a - 1] = 1; 
    }

    ll ans = 0;
    auto dfs = [&](auto sfs, ll now, ll par, bool pushing) -> void{
        ll cnt = 0;
        fore (to, G[now]) {
            if (to == par) continue;
            if (pushing && block[to]) continue;
            cnt++;
        }
        if (cnt > 0 || !pushing)
            ans++;
        // cout << "now: " << now << " " << pushing << " " << cnt << "\n";

        if (cnt > 1) pushing = false;
        fore (to, G[now]) {
            if (par == to) continue;
            if (cnt == 1 && pushing) {
                sfs(sfs, to, now, true);
                continue;
            }
            if (pushing && block[to]) continue;
            sfs(sfs, to, now, pushing | block[to]);
        }
    };
    dfs(dfs, 0, -1, false);
    cout << ans << "\n";
}
accuracy: 1
time_ms: 30
memory_kb: 36148
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define ov4(a, b, c, d, name, ...) name
#define rep3(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep2(i, a, b) rep3(i, a, b, 1)
#define rep1(i, n) rep2(i, 0, n)
#define rep0(n) rep1(aaaaa, n)
#define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__)
#define per(i, a, b) for(ll i = (a)-1; i >= (b); i--)
#define fore(e, v) for(auto&& e : v)
#define all(a) begin(a), end(a)
#define si(a) (int)(size(a))
#define lb(v, x) (lower_bound(all(v), x) - begin(v))
#define eb emplace_back

template<typename T, typename S> bool chmin(T& a, const S& b) { return a > b ? a = b, 1 : 0; }
template<typename T, typename S> bool chmax(T& a, const S& b) { return a < b ? a = b, 1 : 0; }

const int INF = 1e9 + 100;
const ll INFL = 3e18 + 100;

#define i128 __int128_t

struct _ {
    _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); }
} __;

int main() {
    ll N, M;
    cin >> N >> M;
    using vvl = vector<vl>;
    vvl G(N);
    rep(i, N - 1) {
        ll a, b;
        cin >> a >> b;
        a--, b--;
        G[a].eb(b);
        G[b].eb(a);
    }
    vector<bool> block(N);
    rep(i, M) {
        ll a;
        cin >> a;
        block[a - 1] = 1; 
    }

    ll ans = 0;
    auto dfs = [&](auto sfs, ll now, ll par, bool pushing) -> void{
        ll cnt = si(G[now]) - 1;
        fore (to, G[now]) {
            if (to == par) continue;
            if (pushing && block[to]) cnt--;
        }
        if (cnt > 0 || !pushing)
            ans++;
        // cout << "now: " << now << " " << pushing << " " << cnt << "\n";

        if (cnt > 1) pushing = false;
        fore (to, G[now]) {
            if (par == to) continue;
            if (pushing && block[to]) continue;
            // cout << "to: " << to << "\n";
            sfs(sfs, to, now, pushing | block[to]);
        }
    };
    dfs(dfs, 0, -1, false);
    cout << ans << "\n";
}
accuracy: 0.4666666666666667
time_ms: 30
memory_kb: 33072
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i = 0;i < (ll)(n);i++)
#define ld long double

int main() {
  ll n,m;cin >> n >> m;
  vector<vector<ll>>g(n);
  rep(i,n-1) {
    ll u,v;cin >> u >> v;
    u--;v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  set<ll> se;
  rep(i,m) {
    ll a;cin >> a;
    a--;
    se.insert(a);
  }
  ll ans = 0;
  vector<ll> find(n);
  auto dfs = [&](auto dfs,ll v,ll have) {
    find[v] = 1;
    if (have  == 1&&se.count(v))return ;//持ってるのにおいてある
    if (se.count(v))have = 1;//持ってないならもたせる
    //どこかに捨てて移動できるか
    ll vacantcnt = 0;
    for (auto &p:g[v]) {
      if (find[p] == 0 && !se.count(p)) {
        vacantcnt++ ;
      }
    }
    if (have && vacantcnt == 0) {
      return ;
    }
    //今の部屋まではたどり着ける
    ans++;
    for (auto &p:g[v]) {
      if (find[p] == 0) {
        dfs(dfs,p,have && vacantcnt == 1 && !se.count(p));
      }
    }


  };
  dfs(dfs,0,0);
  cout << ans << endl;

}
accuracy: 1
time_ms: 140
memory_kb: 31056
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(a) begin(a),end(a)

int n,m;
vector<vector<int>> g(2e5);
vector<bool> nut_exit(2e5,false);

int dfs(int cur,int pre,bool nut){
	if(nut_exit[cur]) nut = true;
	int cnt = 0;
	for(int next : g[cur])if(next != pre){
		if(nut_exit[next] == false) cnt++;
	}
	if(cnt == 0 && nut) return 0;
	if(cnt >= 2) nut = false;
	int ret = 0;
	for(int next : g[cur])if(next != pre){
		ret += dfs(next,cur,nut);
	}
	return ret + 1;
}

int main(){
	cin >> n >> m;
	for(int i = 0;i < n - 1;i++){
		int a,b;
		cin >> a >> b;
		a--,b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	for(int i = 0;i < m;i++){
		int a;
		cin >> a;
		a--;
		nut_exit[a] = true;
	}
	cout << dfs(0,-1,false) << endl;
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 26540
final_rank: 8