답안 #647506

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
647506 2022-10-03T02:22:15 Z ghostwriter Mergers (JOI19_mergers) C++14
0 / 100
104 ms 39960 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
template<typename T> T gcd(T a, T b) { return (b == 0? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
#define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
#define FRN(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define FSN(i, n) for (int (i) = (n) - 1; (i) >= 0; --(i))
#define EACH(i, x) for (auto &(i) : (x))
#define WHILE while
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
    END OF TEMPLATE
----------------------------------------------------------------
    Tran The Bao - ghostwriter
    Training for VOI23 gold medal
----------------------------------------------------------------
    DIT ME CHUYEN BAO LOC
----------------------------------------------------------------
*/
const int N = 5e5 + 1;
int n, k, h[N], s[N], s1[N], s2[N], p[N][19], root = 0, ct = 0;
vi adj[N], d[N];
void build() {
	FOR(j, 1, 18)
	FOR(i, 1, n)
		p[i][j] = p[p[i][j - 1]][j - 1];
}
int lca(int a, int b) {
	if (h[a] > h[b]) swap(a, b);
	int diff = h[b] - h[a];
	FOR(i, 0, 18)
		if (diff & (1 << i))
			b = p[b][i];
	if (a == b) return a;
	FOS(i, 18, 0)
		if (p[a][i] != p[b][i]) {
			a = p[a][i];
			b = p[b][i];
		}
	return p[a][0];
}
void dfs(int u, int p) {
	::p[u][0] = p;
	s[u] = 1;
	EACH(v, adj[u]) {
		if (v == p) continue;
		h[v] = h[u] + 1;
		dfs(v, u);
		s[u] += s[v];
	}
}
void dfs1(int u, int p) {
	s2[u] = 0;
	EACH(v, adj[u]) {
		if (v == p) continue;
		dfs1(v, u);
		s2[u] += s2[v];
	}
	if (!s2[u]) { if (s1[u] == s[u]) ++s2[u]; }
}
int fct(int u, int p, int maxn) {
	EACH(v, adj[u]) {
		if (v == p) continue;
		if (s1[v] > maxn / 2) return fct(v, u, maxn);
	}
	return u;
}
void dfs2(int u, int p) {
	s2[u] = 0;
	EACH(v, adj[u]) {
		if (v == p) continue;
		dfs2(v, u);
		s2[u] += s2[v];
	}
	if (!s2[u]) { if (s1[u] == s[u]) ++s2[u]; }
}
signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    // freopen(file".inp", "r", stdin);
    // freopen(file".out", "w", stdout);
	cin >> n >> k;
	FOR(i, 1, n - 1) {
		int u, v;
		cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	FOR(i, 1, n) {
		int s;
		cin >> s;
		d[s].pb(i);
	}
	if (k == 1) {
		cout << 0;
		return 0;
	}
	if (n == 2) {
		cout << (k == 1? 0 : 1);
		return 0;
	}
	FOR(i, 1, n)
		if (sz(adj[i]) > 1)
			root = i;
	dfs(root, 0);
	build();
	FOR(i, 1, k) {
		int LCA = d[i].ft();
		EACH(j, d[i]) LCA = lca(LCA, j);
		s1[LCA] += sz(d[i]);
	}
	dfs1(root, 0);
	ct = fct(root, 0, s2[root]);
	dfs(ct, 0);
	build();
	memset(s1, 0, sizeof s1);
	FOR(i, 1, k) {
		int LCA = d[i].ft();
		EACH(j, d[i]) LCA = lca(LCA, j);
		s1[LCA] += sz(d[i]);
	}
	dfs2(ct, 0);
	cout << (s2[ct] + 1) / 2;
    return 0;
}
/*
----------------------------------------------------------------
From Benq:
    stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
        * DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/

Compilation message

mergers.cpp: In function 'void build()':
mergers.cpp:32:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mergers.cpp:55:2: note: in expansion of macro 'FOR'
   55 |  FOR(j, 1, 18)
      |  ^~~
mergers.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mergers.cpp:56:2: note: in expansion of macro 'FOR'
   56 |  FOR(i, 1, n)
      |  ^~~
mergers.cpp: In function 'int lca(int, int)':
mergers.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mergers.cpp:62:2: note: in expansion of macro 'FOR'
   62 |  FOR(i, 0, 18)
      |  ^~~
mergers.cpp:33:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   33 | #define FOS(i, r, l) for (int (i) = (r); (i) >= (l); --(i))
      |                               ^
mergers.cpp:66:2: note: in expansion of macro 'FOS'
   66 |  FOS(i, 18, 0)
      |  ^~~
mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:36:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mergers.cpp:76:2: note: in expansion of macro 'EACH'
   76 |  EACH(v, adj[u]) {
      |  ^~~~
mergers.cpp: In function 'void dfs1(int, int)':
mergers.cpp:36:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mergers.cpp:85:2: note: in expansion of macro 'EACH'
   85 |  EACH(v, adj[u]) {
      |  ^~~~
mergers.cpp: In function 'int fct(int, int, int)':
mergers.cpp:36:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mergers.cpp:93:2: note: in expansion of macro 'EACH'
   93 |  EACH(v, adj[u]) {
      |  ^~~~
mergers.cpp: In function 'void dfs2(int, int)':
mergers.cpp:36:31: warning: unnecessary parentheses in declaration of 'v' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mergers.cpp:101:2: note: in expansion of macro 'EACH'
  101 |  EACH(v, adj[u]) {
      |  ^~~~
mergers.cpp: In function 'int main()':
mergers.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mergers.cpp:113:2: note: in expansion of macro 'FOR'
  113 |  FOR(i, 1, n - 1) {
      |  ^~~
mergers.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mergers.cpp:119:2: note: in expansion of macro 'FOR'
  119 |  FOR(i, 1, n) {
      |  ^~~
mergers.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mergers.cpp:132:2: note: in expansion of macro 'FOR'
  132 |  FOR(i, 1, n)
      |  ^~~
mergers.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mergers.cpp:137:2: note: in expansion of macro 'FOR'
  137 |  FOR(i, 1, k) {
      |  ^~~
mergers.cpp:36:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mergers.cpp:139:3: note: in expansion of macro 'EACH'
  139 |   EACH(j, d[i]) LCA = lca(LCA, j);
      |   ^~~~
mergers.cpp:32:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   32 | #define FOR(i, l, r) for (int (i) = (l); (i) <= (r); ++(i))
      |                               ^
mergers.cpp:147:2: note: in expansion of macro 'FOR'
  147 |  FOR(i, 1, k) {
      |  ^~~
mergers.cpp:36:31: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
   36 | #define EACH(i, x) for (auto &(i) : (x))
      |                               ^
mergers.cpp:149:3: note: in expansion of macro 'EACH'
  149 |   EACH(j, d[i]) LCA = lca(LCA, j);
      |   ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 25684 KB Output is correct
2 Incorrect 14 ms 25684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 25684 KB Output is correct
2 Incorrect 14 ms 25684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 25684 KB Output is correct
2 Incorrect 14 ms 25684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 104 ms 39960 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 25684 KB Output is correct
2 Incorrect 14 ms 25684 KB Output isn't correct
3 Halted 0 ms 0 KB -