Submission #130012

# Submission time Handle Problem Language Result Execution time Memory
130012 2019-07-13T18:52:19 Z qkxwsm Mergers (JOI19_mergers) C++14
100 / 100
1324 ms 158820 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

random_device(rd);
mt19937 rng(rd());
const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();

struct custom_hash
{
	template<class T>
	unsigned long long operator()(T v) const
	{
		unsigned long long x = v;
		x += FIXED_RANDOM;
		// x += 11400714819323198485ull;
		// x = (x ^ (x >> 30)) * 13787848793156543929ull;
		x = (x ^ (x >> 27)) * 10723151780598845931ull;
		return x ^ (x >> 31);
	}
};

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>;

template<class T>
T randomize(T mod)
{
	return (uniform_int_distribution<T>(0, mod - 1))(rng);
}
template<class T>
void readi(T &x)
{
	x = 0;
	bool negative = false;
	char c = ' ';
	while (c < '-')
	{
		c = getchar();
	}
	if (c == '-')
	{
		negative = true;
		c = getchar();
	}
	while (c >= '0')
	{
		x = x * 10 + (c - '0');
		c = getchar();
	}
	if (negative)
	{
		x = -x;
	}
}
template<class T>
void printi(T output)
{
	if (output == 0)
	{
		putchar('0');
		return;
	}
	if (output < 0)
	{
		putchar('-');
		output = -output;
	}
	int buf[20], n = 0;
	while(output)
	{
		buf[n] = ((output % 10));
		output /= 10;
		n++;
	}
	for (n--; n >= 0; n--)
	{
		putchar(buf[n] + '0');
	}
	return;
}
template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
long long expo(long long a, long long e, long long mod)
{
	return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod));
}
template<class T, class U>
void nmod(T &x, U mod)
{
	if (x >= mod) x -= mod;
}
template<class T>
T gcd(T a, T b)
{
	return (b ? gcd(b, a % b) : a);
}

#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define DBG(x) cerr << #x << " = " << (x) << endl
#define SZ(x) ((int) ((x).size()))
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define ALL(x) (x).begin(), (x).end()

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-9;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 500013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdd> vpd;

int N, K, R, T;
vi edge[MAXN];
int id[MAXN], parent[MAXN], depth[MAXN], st[MAXN], ft[MAXN];
vi ord;
vi guys[MAXN];
int lead[MAXN];
int ancestor[25][MAXN];
int dsu[MAXN];
vi edge1[MAXN];
int ans;

void dfs(int u)
{
	st[u] = ft[u] = T;
	T++;
	for (int v : edge[u])
	{
		if (v == parent[u]) continue;
		parent[v] = u;
		depth[v] = depth[u] + 1;
		dfs(v);
		ft[u] = ft[v];
	}
	ord.PB(u);
	// DBG(u);
}
bool insubt(int u, int v)
{
	//is v in the subtree of u?
	return ((u == N) || (st[u] <= st[v] && st[v] <= ft[u]));
}
int lca(int u, int v)
{
	//find the last vertex that v goes to before hitting u
	if (insubt(v, u)) return v;
	FORD(i, 20, 0)
	{
		if (!insubt(ancestor[i][v], u))
		{
			v = ancestor[i][v];
		}
	}
	return parent[v];
}
int get(int u)
{
	return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void merge(int u, int v)
{
	u = get(u);
	v = get(v);
	dsu[u] = v;
	return;
}

int32_t main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	// cout << fixed << setprecision(10);
	// cerr << fixed << setprecision(10);
	// if (fopen("file.in", "r"))
	// {
	// 	freopen ("file.in", "r", stdin);
	// 	freopen ("file.out", "w", stdout);
	// }
	cin >> N >> K;
	if (N == 1)
	{
		cout << "0\n";
		return 0;
	}
	if (K == 1)
	{
		cout << "0\n";
		return 0;
	}
	if (N == 2 && K == 2)
	{
		cout << "1\n";
		return 0;
	}
	FOR(i, 0, N - 1)
	{
		int u, v;
		cin >> u >> v;
		u--; v--;
		edge[u].PB(v);
		edge[v].PB(u);
	}
	FOR(i, 0, N)
	{
		cin >> id[i];
		id[i]--;
		guys[id[i]].PB(i);
	}
	FOR(i, 0, N)
	{
		if (SZ(edge[i]) != 1) R = i;
	}
	FOR(i, 0, N)
	{
		parent[i] = N;
	}
	dfs(R);
	FOR(i, 0, 24)
	{
		ancestor[i][N] = N;
	}
	FOR(i, 0, N)
	{
		ancestor[0][i] = parent[i];
	}
	FOR(i, 1, 23)
	{
		FOR(j, 0, N)
		{
			ancestor[i][j] = ancestor[i - 1][ancestor[i - 1][j]];
		}
	}
	FOR(i, 0, K)
	{
		int head = guys[i][0];
		for (int u : guys[i])
		{
			head = lca(u, head);
		}
		for (int u : guys[i])
		{
			lead[u] = head;
		}
	}
	FOR(i, 0, N)
	{
		dsu[i] = i;
	}
	for (int u : ord)
	{
		if (u == R) break;
		if (lead[u] != u)
		{
			merge(u, parent[u]);
			if (depth[lead[u]] < depth[lead[parent[u]]])
			{
				lead[parent[u]] = lead[u];
			}
		}
	}
	FOR(i, 0, N)
	{
		for (int u : edge[i])
		{
			if (get(u) != get(i))
			{
				edge1[get(u)].PB(get(i));
			}
		}
	}
	FOR(i, 0, N)
	{
		if (SZ(edge1[i]) == 1) ans++;
	}
	cout << (ans + 1) / 2 << '\n';
	// cerr << "time elapsed = " << (clock() / (CLOCKS_PER_SEC / 1000)) << " ms" << endl;
	return 0;
}
/* READ READ READ
* int overflow, maxn too small, special cases (n=1?, two distinct?), cin.tie() interactive
* reread the problem, try small cases
* note down possible sources of error as you go
* do smth instead of nothing
*/
# Verdict Execution time Memory Grader output
1 Correct 36 ms 35832 KB Output is correct
2 Correct 37 ms 35832 KB Output is correct
3 Correct 37 ms 35832 KB Output is correct
4 Correct 37 ms 35832 KB Output is correct
5 Correct 37 ms 35832 KB Output is correct
6 Correct 36 ms 35632 KB Output is correct
7 Correct 36 ms 35576 KB Output is correct
8 Correct 36 ms 35832 KB Output is correct
9 Correct 36 ms 35832 KB Output is correct
10 Correct 37 ms 35832 KB Output is correct
11 Correct 37 ms 35832 KB Output is correct
12 Correct 37 ms 35804 KB Output is correct
13 Correct 43 ms 35832 KB Output is correct
14 Correct 37 ms 35816 KB Output is correct
15 Correct 37 ms 35832 KB Output is correct
16 Correct 37 ms 35832 KB Output is correct
17 Correct 37 ms 35832 KB Output is correct
18 Correct 37 ms 35832 KB Output is correct
19 Correct 36 ms 35832 KB Output is correct
20 Correct 36 ms 35832 KB Output is correct
21 Correct 37 ms 35832 KB Output is correct
22 Correct 37 ms 35960 KB Output is correct
23 Correct 37 ms 35832 KB Output is correct
24 Correct 37 ms 35832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 35832 KB Output is correct
2 Correct 37 ms 35832 KB Output is correct
3 Correct 37 ms 35832 KB Output is correct
4 Correct 37 ms 35832 KB Output is correct
5 Correct 37 ms 35832 KB Output is correct
6 Correct 36 ms 35632 KB Output is correct
7 Correct 36 ms 35576 KB Output is correct
8 Correct 36 ms 35832 KB Output is correct
9 Correct 36 ms 35832 KB Output is correct
10 Correct 37 ms 35832 KB Output is correct
11 Correct 37 ms 35832 KB Output is correct
12 Correct 37 ms 35804 KB Output is correct
13 Correct 43 ms 35832 KB Output is correct
14 Correct 37 ms 35816 KB Output is correct
15 Correct 37 ms 35832 KB Output is correct
16 Correct 37 ms 35832 KB Output is correct
17 Correct 37 ms 35832 KB Output is correct
18 Correct 37 ms 35832 KB Output is correct
19 Correct 36 ms 35832 KB Output is correct
20 Correct 36 ms 35832 KB Output is correct
21 Correct 37 ms 35832 KB Output is correct
22 Correct 37 ms 35960 KB Output is correct
23 Correct 37 ms 35832 KB Output is correct
24 Correct 37 ms 35832 KB Output is correct
25 Correct 37 ms 35832 KB Output is correct
26 Correct 39 ms 36472 KB Output is correct
27 Correct 39 ms 36380 KB Output is correct
28 Correct 41 ms 36600 KB Output is correct
29 Correct 46 ms 36472 KB Output is correct
30 Correct 46 ms 36344 KB Output is correct
31 Correct 45 ms 35832 KB Output is correct
32 Correct 39 ms 36472 KB Output is correct
33 Correct 36 ms 35832 KB Output is correct
34 Correct 39 ms 36344 KB Output is correct
35 Correct 40 ms 36476 KB Output is correct
36 Correct 39 ms 36344 KB Output is correct
37 Correct 39 ms 36472 KB Output is correct
38 Correct 37 ms 35832 KB Output is correct
39 Correct 39 ms 36344 KB Output is correct
40 Correct 37 ms 35696 KB Output is correct
41 Correct 39 ms 36344 KB Output is correct
42 Correct 39 ms 36472 KB Output is correct
43 Correct 37 ms 35576 KB Output is correct
44 Correct 37 ms 35832 KB Output is correct
45 Correct 40 ms 36516 KB Output is correct
46 Correct 40 ms 36344 KB Output is correct
47 Correct 37 ms 35756 KB Output is correct
48 Correct 39 ms 36344 KB Output is correct
49 Correct 39 ms 36600 KB Output is correct
50 Correct 39 ms 36472 KB Output is correct
51 Correct 39 ms 36344 KB Output is correct
52 Correct 39 ms 36600 KB Output is correct
53 Correct 39 ms 36472 KB Output is correct
54 Correct 45 ms 36344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 35832 KB Output is correct
2 Correct 37 ms 35832 KB Output is correct
3 Correct 37 ms 35832 KB Output is correct
4 Correct 37 ms 35832 KB Output is correct
5 Correct 37 ms 35832 KB Output is correct
6 Correct 36 ms 35632 KB Output is correct
7 Correct 36 ms 35576 KB Output is correct
8 Correct 36 ms 35832 KB Output is correct
9 Correct 36 ms 35832 KB Output is correct
10 Correct 37 ms 35832 KB Output is correct
11 Correct 37 ms 35832 KB Output is correct
12 Correct 37 ms 35804 KB Output is correct
13 Correct 43 ms 35832 KB Output is correct
14 Correct 37 ms 35816 KB Output is correct
15 Correct 37 ms 35832 KB Output is correct
16 Correct 37 ms 35832 KB Output is correct
17 Correct 37 ms 35832 KB Output is correct
18 Correct 37 ms 35832 KB Output is correct
19 Correct 36 ms 35832 KB Output is correct
20 Correct 36 ms 35832 KB Output is correct
21 Correct 37 ms 35832 KB Output is correct
22 Correct 37 ms 35960 KB Output is correct
23 Correct 37 ms 35832 KB Output is correct
24 Correct 37 ms 35832 KB Output is correct
25 Correct 41 ms 35840 KB Output is correct
26 Correct 110 ms 53504 KB Output is correct
27 Correct 141 ms 53172 KB Output is correct
28 Correct 39 ms 36344 KB Output is correct
29 Correct 37 ms 35832 KB Output is correct
30 Correct 37 ms 35860 KB Output is correct
31 Correct 131 ms 53124 KB Output is correct
32 Correct 39 ms 36344 KB Output is correct
33 Correct 143 ms 54896 KB Output is correct
34 Correct 130 ms 53232 KB Output is correct
35 Correct 39 ms 36344 KB Output is correct
36 Correct 129 ms 53480 KB Output is correct
37 Correct 36 ms 35624 KB Output is correct
38 Correct 39 ms 36344 KB Output is correct
39 Correct 113 ms 53496 KB Output is correct
40 Correct 37 ms 35576 KB Output is correct
41 Correct 36 ms 35576 KB Output is correct
42 Correct 142 ms 54472 KB Output is correct
43 Correct 37 ms 35832 KB Output is correct
44 Correct 36 ms 35576 KB Output is correct
45 Correct 135 ms 54812 KB Output is correct
46 Correct 43 ms 36472 KB Output is correct
47 Correct 39 ms 36380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 111 ms 53492 KB Output is correct
2 Correct 161 ms 60100 KB Output is correct
3 Correct 40 ms 36472 KB Output is correct
4 Correct 39 ms 36344 KB Output is correct
5 Correct 37 ms 35832 KB Output is correct
6 Correct 37 ms 35960 KB Output is correct
7 Correct 39 ms 36344 KB Output is correct
8 Correct 161 ms 54868 KB Output is correct
9 Correct 49 ms 36344 KB Output is correct
10 Correct 140 ms 53488 KB Output is correct
11 Correct 38 ms 35880 KB Output is correct
12 Correct 158 ms 53324 KB Output is correct
13 Correct 181 ms 55028 KB Output is correct
14 Correct 226 ms 59292 KB Output is correct
15 Correct 136 ms 53500 KB Output is correct
16 Correct 39 ms 36472 KB Output is correct
17 Correct 44 ms 35832 KB Output is correct
18 Correct 205 ms 59372 KB Output is correct
19 Correct 214 ms 60768 KB Output is correct
20 Correct 49 ms 36344 KB Output is correct
21 Correct 43 ms 35832 KB Output is correct
22 Correct 150 ms 55480 KB Output is correct
23 Correct 39 ms 36348 KB Output is correct
24 Correct 192 ms 53844 KB Output is correct
25 Correct 216 ms 60396 KB Output is correct
26 Correct 39 ms 36600 KB Output is correct
27 Correct 40 ms 36476 KB Output is correct
28 Correct 43 ms 36416 KB Output is correct
29 Correct 39 ms 36344 KB Output is correct
30 Correct 39 ms 36472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 35832 KB Output is correct
2 Correct 37 ms 35832 KB Output is correct
3 Correct 37 ms 35832 KB Output is correct
4 Correct 37 ms 35832 KB Output is correct
5 Correct 37 ms 35832 KB Output is correct
6 Correct 36 ms 35632 KB Output is correct
7 Correct 36 ms 35576 KB Output is correct
8 Correct 36 ms 35832 KB Output is correct
9 Correct 36 ms 35832 KB Output is correct
10 Correct 37 ms 35832 KB Output is correct
11 Correct 37 ms 35832 KB Output is correct
12 Correct 37 ms 35804 KB Output is correct
13 Correct 43 ms 35832 KB Output is correct
14 Correct 37 ms 35816 KB Output is correct
15 Correct 37 ms 35832 KB Output is correct
16 Correct 37 ms 35832 KB Output is correct
17 Correct 37 ms 35832 KB Output is correct
18 Correct 37 ms 35832 KB Output is correct
19 Correct 36 ms 35832 KB Output is correct
20 Correct 36 ms 35832 KB Output is correct
21 Correct 37 ms 35832 KB Output is correct
22 Correct 37 ms 35960 KB Output is correct
23 Correct 37 ms 35832 KB Output is correct
24 Correct 37 ms 35832 KB Output is correct
25 Correct 37 ms 35832 KB Output is correct
26 Correct 39 ms 36472 KB Output is correct
27 Correct 39 ms 36380 KB Output is correct
28 Correct 41 ms 36600 KB Output is correct
29 Correct 46 ms 36472 KB Output is correct
30 Correct 46 ms 36344 KB Output is correct
31 Correct 45 ms 35832 KB Output is correct
32 Correct 39 ms 36472 KB Output is correct
33 Correct 36 ms 35832 KB Output is correct
34 Correct 39 ms 36344 KB Output is correct
35 Correct 40 ms 36476 KB Output is correct
36 Correct 39 ms 36344 KB Output is correct
37 Correct 39 ms 36472 KB Output is correct
38 Correct 37 ms 35832 KB Output is correct
39 Correct 39 ms 36344 KB Output is correct
40 Correct 37 ms 35696 KB Output is correct
41 Correct 39 ms 36344 KB Output is correct
42 Correct 39 ms 36472 KB Output is correct
43 Correct 37 ms 35576 KB Output is correct
44 Correct 37 ms 35832 KB Output is correct
45 Correct 40 ms 36516 KB Output is correct
46 Correct 40 ms 36344 KB Output is correct
47 Correct 37 ms 35756 KB Output is correct
48 Correct 39 ms 36344 KB Output is correct
49 Correct 39 ms 36600 KB Output is correct
50 Correct 39 ms 36472 KB Output is correct
51 Correct 39 ms 36344 KB Output is correct
52 Correct 39 ms 36600 KB Output is correct
53 Correct 39 ms 36472 KB Output is correct
54 Correct 45 ms 36344 KB Output is correct
55 Correct 41 ms 35840 KB Output is correct
56 Correct 110 ms 53504 KB Output is correct
57 Correct 141 ms 53172 KB Output is correct
58 Correct 39 ms 36344 KB Output is correct
59 Correct 37 ms 35832 KB Output is correct
60 Correct 37 ms 35860 KB Output is correct
61 Correct 131 ms 53124 KB Output is correct
62 Correct 39 ms 36344 KB Output is correct
63 Correct 143 ms 54896 KB Output is correct
64 Correct 130 ms 53232 KB Output is correct
65 Correct 39 ms 36344 KB Output is correct
66 Correct 129 ms 53480 KB Output is correct
67 Correct 36 ms 35624 KB Output is correct
68 Correct 39 ms 36344 KB Output is correct
69 Correct 113 ms 53496 KB Output is correct
70 Correct 37 ms 35576 KB Output is correct
71 Correct 36 ms 35576 KB Output is correct
72 Correct 142 ms 54472 KB Output is correct
73 Correct 37 ms 35832 KB Output is correct
74 Correct 36 ms 35576 KB Output is correct
75 Correct 135 ms 54812 KB Output is correct
76 Correct 43 ms 36472 KB Output is correct
77 Correct 39 ms 36380 KB Output is correct
78 Correct 111 ms 53492 KB Output is correct
79 Correct 161 ms 60100 KB Output is correct
80 Correct 40 ms 36472 KB Output is correct
81 Correct 39 ms 36344 KB Output is correct
82 Correct 37 ms 35832 KB Output is correct
83 Correct 37 ms 35960 KB Output is correct
84 Correct 39 ms 36344 KB Output is correct
85 Correct 161 ms 54868 KB Output is correct
86 Correct 49 ms 36344 KB Output is correct
87 Correct 140 ms 53488 KB Output is correct
88 Correct 38 ms 35880 KB Output is correct
89 Correct 158 ms 53324 KB Output is correct
90 Correct 181 ms 55028 KB Output is correct
91 Correct 226 ms 59292 KB Output is correct
92 Correct 136 ms 53500 KB Output is correct
93 Correct 39 ms 36472 KB Output is correct
94 Correct 44 ms 35832 KB Output is correct
95 Correct 205 ms 59372 KB Output is correct
96 Correct 214 ms 60768 KB Output is correct
97 Correct 49 ms 36344 KB Output is correct
98 Correct 43 ms 35832 KB Output is correct
99 Correct 150 ms 55480 KB Output is correct
100 Correct 39 ms 36348 KB Output is correct
101 Correct 192 ms 53844 KB Output is correct
102 Correct 216 ms 60396 KB Output is correct
103 Correct 39 ms 36600 KB Output is correct
104 Correct 40 ms 36476 KB Output is correct
105 Correct 43 ms 36416 KB Output is correct
106 Correct 39 ms 36344 KB Output is correct
107 Correct 39 ms 36472 KB Output is correct
108 Correct 727 ms 127196 KB Output is correct
109 Correct 782 ms 125476 KB Output is correct
110 Correct 37 ms 35580 KB Output is correct
111 Correct 1324 ms 158820 KB Output is correct
112 Correct 1099 ms 149692 KB Output is correct
113 Correct 902 ms 147676 KB Output is correct
114 Correct 715 ms 120192 KB Output is correct
115 Correct 37 ms 35576 KB Output is correct
116 Correct 909 ms 122840 KB Output is correct
117 Correct 1250 ms 149864 KB Output is correct
118 Correct 708 ms 119780 KB Output is correct
119 Correct 1239 ms 149736 KB Output is correct
120 Correct 1315 ms 156260 KB Output is correct
121 Correct 1276 ms 149992 KB Output is correct
122 Correct 1089 ms 130500 KB Output is correct
123 Correct 1073 ms 153696 KB Output is correct
124 Correct 1230 ms 141548 KB Output is correct