제출 #1370406

#제출 시각아이디문제언어결과실행 시간메모리
1370406blackscreen1Mergers (JOI19_mergers)C++20
0 / 100
45 ms29536 KiB
#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_multiset;
#define ll long long
#define iloop(m, h) for (auto i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define pll pair<ll, ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
ll n, m, t1, t2, ans = 0;
vector<ll> adj[500005], b[500005];
ll twok[20][500005], dpt[500005], a[500005];
void dfs(ll nd, ll pr, ll dp) {
	twok[0][nd] = pr;
	dpt[nd] = dp;
	iloop(1, 20) {
		if (twok[i-1][nd] == -1) break;
		twok[i][nd] = twok[i-1][twok[i-1][nd]];
	}
	for (auto it : adj[nd]) if (it != pr) dfs(it, nd, dp+1);
}
ll lca(ll x, ll y) {
	if (dpt[x] < dpt[y]) swap(x, y);
	iloop(19, -1) if (dpt[x] - dpt[y] >= 1<<i) x = twok[i][x];
	if (x == y) return x;
	iloop(19, -1) if (twok[i][x] != twok[i][y]) x = twok[i][x], y = twok[i][y];
	return twok[0][x];
}
pair<ll, bool> dfs2(ll nd, ll pr) {
	pair<ll, bool> res = {a[nd], 0}, tm;
	for (auto it : adj[nd]) if (it != pr) {
		tm = dfs2(it, nd);
		res.first += tm.first, res.second |= tm.second;
	}
	if (res.first == 0 && !res.second && pr != -1) {
		ans++;
		res.second = 1;
	}
	return res;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	iloop(1, n) {
		cin >> t1 >> t2;
		t1--, t2--;
		adj[t1].push_back(t2);
		adj[t2].push_back(t1);
	}
	iloop(0, n) {
		cin >> t1;
		t1--;
		b[t1].push_back(i);
	}
	dfs(0, -1, 0);
	ll cn;
	iloop(0, m) {
		cn = b[i][0];
		for (auto it : b[i]) a[it]++, cn = lca(cn, it);
		a[cn] -= b[i].size();
	}
	dfs2(0, -1);
	cout << (ans+1)/2;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…