제출 #359290

#제출 시각아이디문제언어결과실행 시간메모리
359290Return_0Mergers (JOI19_mergers)C++17
100 / 100
1421 ms129500 KiB
#pragma GCC optimize("Ofast,unroll-loops")
#pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")

#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 int ll;
typedef long double ld;
typedef pair <ll, ll> pll;

#ifdef SINA
#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; }
template <typename Arg1, typename... Args> void __f (const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...); }
#define dbg2(x, j, n) cout<< #x << " : "; output((j), (n), x, 1);
#else
#define dbg(...) 0
#define dbg2(x, j, n) 0
#endif
#define SZ(x) ((ll)((x).size()))
#define File(s, t) freopen(s ".txt", "r", stdin); freopen(t ".txt", "w", stdout);
#define input(j, n, a) for (int _i = (j); _i < (n)+(j); _i++) cin>> a[_i];
#define output(j, n, a, t) for (int _i = (j); _i < (n)+(j); _i++) cout<< a[_i] << (((t) && _i != (n)+(j)-1)? ' ' : '\n');
#define kill(x) return cout<< x << endl, 0
#define cl const ll
#define fr first
#define sc second
#define lc (v << 1)
#define rc (lc | 1)
#define mid ((l + r) >> 1)
#define All(x) (x).begin(), (x).end()

cl inf = sizeof(ll) == 4 ? (1e9 + 10) : (3e18), mod = 1e9 + 7, MOD = 998244353;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << '(' << a.first << ", " << a.second << ')';    }
template <class A> ostream& operator << (ostream& out, const vector<A> &a) {
	if(a.empty())return out<<"[]";out<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) out<< ", " << a[i];out<< ']';  }
template <class T, typename _t = less <T> > using Tree = tree <T, null_type, _t, rb_tree_tag, tree_order_statistics_node_update>;

cl N = 5e5 + 7, lg = 19;

ll par [N], tin [N], T, h [N], c [N], anc [N][lg], val [N], n, k, ans;
vector <ll> adj [N], col [N];
vector <pll> E;

ll root (cl &v) { return par[v] = (par[v] == v ? v : root(par[v])); }
inline void unite (cl &v, cl &u) { par[root(u)] = root(v); }

void DFS (cl &v = 1, cl &pr = 1) {
	tin[v] = ++T;
	h[v] = h[pr] + 1;
	anc[v][0] = pr;
	for (ll i = 1; i < lg; i++)	anc[v][i] = anc[anc[v][i-1]][i-1];
	for (auto &u : adj[v]) if (u ^ pr)	DFS(u, v);
}

inline ll up (ll v, ll dis) { for (ll i = 0; i < lg; i++) if (dis >> i & 1)	v = anc[v][i]; return v; }
inline ll LCA (ll v, ll u) {
	if (h[v] > h[u])	swap(v, u);
	u = up(u, h[u] - h[v]);
	if (v == u)	return v;
	for (ll i = lg; i--;) if (anc[v][i] ^ anc[u][i]) v = anc[v][i], u = anc[u][i];
	return anc[v][0];
}

ll dfs (cl &v = 1) {
	for (auto &u : adj[v]) if (u ^ anc[v][0])	val[v] += dfs(u);
	return val[v];
}

int main ()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	iota(par, par + N, 0);

	cin>> n >> k;
	for (ll i = 1, v, u; i < n; i++) {
		cin>> v >> u;
		adj[v].push_back(u);
		adj[u].push_back(v);
	}

	for (ll i = 1; i <= n; i++)	cin>> c[i], col[c[i]].push_back(i);

	DFS();

	for (ll i = 1; i <= k; i++)	{
		sort(All(col[i]), [&](ll x, ll y) { return tin[x] < tin[y]; });
		ll cur = col[i][0];
		for (auto &v : col[i]) {
			val[cur]++, val[v]++;
			cur = LCA(cur, v);
			val[cur] -= 2;
		}
	}

	dfs();

	for (ll i = 2; i <= n; i++) {
		if (val[i] > 0)	unite(i, anc[i][0]);
		else E.push_back({i, anc[i][0]});
	}

	memset(val, 0, sizeof val);
	for (auto &e : E)	val[root(e.fr)]++, val[root(e.sc)]++;

	for (ll i = 1; i <= n; i++) if (root(i) == i)	ans += (val[i] == 1);

	cout<< (ans + 1) /2 << endl;

	return 0;
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4

5 4
1 2
2 3
3 4
4 5
1
2
3
4
1

5 5
1 2
1 3
1 4
1 5
1
2
3
4
5

*/

컴파일 시 표준 에러 (stderr) 메시지

mergers.cpp:2: warning: ignoring #pragma comment  [-Wunknown-pragmas]
    2 | #pragma comment(linker, "/stack:200000000")
      | 
mergers.cpp: In function 'std::ostream& operator<<(std::ostream&, const std::vector<_Tp>&)':
mergers.cpp:44:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   44 |  if(a.empty())return out<<"[]";out<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) out<< ", " << a[i];out<< ']';  }
      |  ^~
mergers.cpp:44:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   44 |  if(a.empty())return out<<"[]";out<< '[' << a[0]; for (int i = 0; ++i < (int)(a.size());) out<< ", " << a[i];out<< ']';  }
      |                                ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...