gelastropod's blog

By gelastropod, history, 2 weeks ago, In English

This blog is coauthored by gohchingjayk.

Hello Codeforces!

In this blog, we will first introduce this property, and then use it to improve the solution to 2260F - Edge Three-Coloring! =D

What is the property?

Theorem 1. Suppose we have a set $$$S$$$ which is a subset of the vertices on a rooted tree. Now, let $$$T = \lbrace \operatorname{LCA}(u, v) \mid u, v \in S \rbrace$$$. We claim that $$$|T \setminus S| \le |S| - 1$$$.

Proof. Consider the Eulerian Tour of the tree. Let $$$pre_u$$$ be the first visit of node $$$u$$$ in the Eulerian Tour. It is known that $$$\operatorname{LCA}(u, v)$$$ is the node of the minimum depth between $$$pre_u$$$ and $$$pre_v$$$ (inclusive).

Now, consider 2 nodes, $$$u, v \in S$$$. Suppose there exists node $$$x \in S$$$ such that $$$pre_u \lt pre_x \lt pre_v$$$. Observe that the node of minimum depth between $$$pre_u$$$ and $$$pre_v$$$ is either $$$\operatorname{LCA}(u, x)$$$ or $$$\operatorname{LCA}(x, v)$$$.

There are exactly $$$|S| - 1$$$ pairs $$$u, v \in S$$$ where there exists no $$$x \in S$$$ such that $$$pre_u \lt pre_x \lt pre_v$$$. Thus, $$$|T \setminus S| \le |S| - 1$$$. $$$\square$$$

Taking a look at 2260F - Edge Three-Coloring

If you haven't solved this problem before, I recommend taking a look first. Otherwise, here is a short summary of the solution.

The solution takes advantage of property 6 in this blog — that cycles in a graph form a linear space using symmetric difference. Now, take an arbitrary spanning tree of the graph. Each of the $$$C = M - (N - 1)$$$ edges (call them "special") outside this spanning tree forms a unique cycle together with the tree path between its endpoints. We show that these $$$C$$$ cycles form a basis of this linear space.

Firstly, let's show that these $$$C$$$ cycles span the linear space. Recall that in a tree, there exists exactly one simple path between any 2 nodes. Thus, at least one of these $$$C$$$ edges must be used in order to create a cycle.

Next, let's show that these $$$C$$$ cycles are linearly independent. Note that as each cycle uses a different edge external to the spanning tree, it cannot be created as a linear combination of the other cycles.

Now, the solution to the problem is as follows:
Bruteforce all $$$2^C - 1$$$ non-empty subsets of cycles in the basis. Now, for a subset, we find the symmetric difference of all the cycles in it, and check if the remaining edges form a connected graph.

The final complexity of the solution is $$$O(2^C \cdot (N + M) \cdot\alpha(N))$$$.

Can we do better?

Here, "original tree" refers to the arbitrary spanning tree chosen in the earlier part.

Let $$$S$$$ be the set of all endpoints of the $$$C$$$ "special" edges. We can define $$$T$$$ similarly to above, and let $$$S'=S\cup T$$$. It is easy to see that $$$|S'|\le 4\cdot C-1$$$. For any vertex of the tree not in $$$S'$$$, if we root the tree at that vertex, there exists at most two child subtrees which contain vertices in $$$S'$$$.

Now, consider the symmetric difference of subsets of cycles in the basis. Consider vertices $$$u,v\in S'$$$ such that there does not exist a vertex $$$x$$$ on the simple path between $$$u,v$$$ which is also contained in $$$S'$$$. We assert that in the symmetric difference of any subset of cycles, if any edge on the simple path from $$$u$$$ to $$$v$$$ is included, then all edges on the path are included.

Suppose otherwise. Then, there exists a vertex on the simple path $$$x\neq u,v$$$ which is the endpoint of exactly one edge on the path. Since the degree of all vertices must be even when considering symmetric difference of cycles, $$$x$$$ must be the endpoint of at least 1 other edge not on the path. But this can only occur if there exists another endpoint of one of the $$$C$$$ "special" edges in some other direction. In other words, rooting the tree at $$$x$$$, there exist at least 3 child subtrees with nodes in $$$S'$$$. This contradicts the earlier statement, and the result follows.

With this observation, we are able to consider the "virtual tree" of the original tree consisting only of vertices in $$$S'$$$: add an edge between two nodes $$$u$$$ and $$$v$$$ in the virtual tree if no other vertex in $$$S'$$$ is present on the simple path between $$$u$$$ and $$$v$$$ in the original tree. Now, we can compute the symmetric difference of cycles on this tree, reducing the time complexity of that part to $$$O(2^C\cdot C)$$$, from $$$O(2^C\cdot N)$$$.

Also, consider all edges on the "virtual tree" which represented more than one edge on the original tree. If this edge $$$(u,v)$$$ was included in the symmetric difference of cycles, and therefore removed from the original graph, there exists at least 1 intermediate vertex on that edge, which becomes disconnected from $$$u$$$ and $$$v$$$. Since this intermediate vertex is not in $$$S'$$$, by definition of virtual tree, no other edges connect this vertex to the rest of the graph. Therefore, the remaining graph becomes disconnected.

Therefore, if the virtual tree (with the addition of the unselected "special" edges) remains connected after removing some symmetric difference of cycles, then the original graph will also remain connected with that removal. We can compute the virtual tree in $$$O(N)$$$ using a DFS. This reduces the overall time complexity of the algorithm to $$$O(2^C\cdot C\cdot\alpha(C)+N)$$$.

With this optimisation, we can solve the problem for $$$N\le 10^6$$$, $$$C\le 20$$$ !

Sample code that implements this: 398537962

Full text and comments »

  • Vote: I like it
  • +55
  • Vote: I do not like it

By gelastropod, history, 6 months ago, In English

So I was trying to upsolve IOI 2024 Q6 Sphinx. In the editorial phase 2 states to split the condensed graph into two sets, and perform binary search on these sets for every colour, to determine which components have what colour. However, each binary search needs $$$\log N\approx8$$$ queries to perform the actual binary search, along with an additional query to "confirm" that no other component in the set has that colour. Along with the rest of the solution, this gives a query count of $$$3N+a+N\log N$$$, where $$$a$$$ is the number of monochromatic components. This means that, in the worst case, the solution would take $$$4N+N\log N$$$, which is up to $$$3000$$$ queries, more than is allowed in the problem. At first, I thought that there would be some amortisation which keeps the query count under the limit of $$$2750$$$ queries, but then I made the following construction:

Consider the test case with $$$N=250$$$ vertices, all with different colours. Then, the graph is constructed by taking the first 248 vertices, and for each of these vertices construct an edge between that vertex and both vertices 249 and 250 (1-indexed). Testing this test case on the model code provided by IOI, it took $$$2977 \gt 2750$$$ queries to finish?

Is this a counterexample to the model solution for IOI Sphinx? The testcase I used is below, you can test it out yourself.

Test case used

Edit: I used the model_solution/solution.cpp file, apparantly it works on other solutions.

Edit: This testcase also breaks some people's solutions, https://oj.uz/submission/1372712 for example.

Full text and comments »

  • Vote: I like it
  • +117
  • Vote: I do not like it

By gelastropod, history, 7 months ago, In English

bigW and GirrySnake777 will AK ioi!

we need to flip the problem inside out

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

namespace {
	using ull = unsigned long long;

	struct info {
		int idx1, idx2;
		ull weight;
	};

	const ull c1 = 0xFF, c2 = 0xFFFFFFFFFFFF, c3 = 0xFFFFFFFFFFFFFF;
	constexpr ull pack(info i, int type) { return ((ull)(i.idx1) << 56) + type * ((ull)(i.idx2) << 48) + i.weight; }
	constexpr info unpack(ull i, int type) { return info{ (int)(i >> 56), (int)((i >> 48) & c1), i & (type ? c2 : c3) }; }

	int N, r, i;
	ull total;
	vector<ull> A, B, C;
	vector<info> infos, auxinfo;
	vector<int> p, ss;
	vector<int> leaders;
	vector<ull> edgeweights;
	vector<pair<int, int>> bedges;

	int fin(int n) {
		if (p[n] == n) return n;
		return p[n] = fin(p[n]);
	}

	void join(int a, int b, bool add = 1) {
		int oa = a, ob = b;
		a = fin(a), b = fin(b);
		if (a == b) return;
		if (a < b) swap(a, b);
		if (add && i == min(oa, ob)) total += A[max(oa, ob)];
		ss[b] += ss[a];
		p[a] = b;
	}

	void reset() {
		A.clear(); B.clear(); C.clear();
		infos.clear(); auxinfo.clear(); p.clear(); ss.clear();
		leaders.clear(); edgeweights.clear(); bedges.clear();
	}
}

vector<ull> strategy(int _N, int _r, int _i, vector<ull> _A, vector<ull> _B) {
	reset();
	N = _N, r = _r, i = _i, A = _A, B = _B;
	if (N == 2) return { A[!i] };
	C.resize(N, 0);
	ss.resize(N, 1);
	for (int j = 0; j < N; j++) p.push_back(j);
	for (int j = 0; j < N; j++) infos.push_back(unpack(B[j], !((i == j && r != 4 && r != 1) || (i == 0 && r == 4))));
	auxinfo.resize(N, { -1, 0, c2 });
	total = (r == 1 ? 0ull : infos[i].weight);
	info crnt{ 0, 0, c2 }; ull minw = c2;
	for (int j = 0; j < N; j++) {
		if (i == j) continue;
		if (A[j] < crnt.weight) crnt.weight = A[j], crnt.idx2 = j;
		if (crnt.weight < minw) {
			swap(minw, crnt.weight);
			swap(crnt.idx1, crnt.idx2);
		}
	}
	if (r == 1) {
		for (int j = 0; j < N; j++) join(j, infos[j].idx1);
		for (int j = 0; j < N; j++) {
			if (ss[fin(j)] == 2) {
				if (infos[j].weight < infos[infos[j].idx1].weight) join(j, infos[j].idx2);
				else join(infos[j].idx1, infos[infos[j].idx1].idx2);
			}
		}
	}
	else if (r <= 5) for (int j = 0; j < N; j++) p[j] = infos[j].idx1;
	if (r == 2 || r == 3) {
		crnt.weight = c2;
		for (int j = 0; j < N; j++) if (infos[i].idx1 != fin(j) && A[j] < crnt.weight) crnt.weight = A[j], crnt.idx2 = j;
		infos[i] = crnt;
		for (int j = 0; j < N; j++) if (infos[j].weight < auxinfo[fin(j)].weight) auxinfo[fin(j)].weight = infos[j].weight, auxinfo[fin(j)].idx2 = infos[j].idx2, auxinfo[fin(j)].idx1 = j;
		for (int j = 0; j < N; j++) if (auxinfo[fin(j)].idx1 != -1) join(auxinfo[fin(j)].idx1, auxinfo[fin(j)].idx2);
	}
	else if (r == 4) {
		if (i == 0) for (int j = 1; j < N; j++) total += infos[j].weight;
	}
	if (r == 3 || r == 4 || (r == 5 && i == 0)) {
		for (int j = 0; j < N; j++) if (p[j] == j) leaders.push_back(j);
		for (int j = 0; j < leaders.size(); j++) for (int k = 0; k < j; k++) bedges.push_back({ j, k });
		edgeweights.resize(leaders.size(), c2);
	}
	if (r == 0) for (int j = 0; j < N; j++) C[j] = pack(crnt, 1);
	else if (r == 1 || r == 2) {
		crnt.weight = c2, crnt.idx1 = fin(i);
		for (int j = 0; j < N; j++) if (crnt.idx1 != fin(j) && A[j] < crnt.weight) crnt.weight = A[j], crnt.idx2 = j;
		for (int j = 0; j < N; j++) C[j] = pack(crnt, 1);
		C[i] = pack({ crnt.idx1, 0, total }, 0);
	}
	else if (r == 3) {
		for (int j = 0; j < leaders.size(); j++) for (int k = 0; k < N; k++) if (fin(k) == leaders[j] && A[k] < edgeweights[j]) edgeweights[j] = A[k];
		for (int j = 1; j <= bedges.size(); j++) C[j] = pack({ fin(i), 0, edgeweights[bedges[j - 1].first] }, 1);
		for (int j = bedges.size() + 1; j < N; j++) C[j] = pack({ fin(i), 0, 0 }, 1);
		C[0] = pack({ fin(i), 0, total }, 0);
	}
	else if (r == 4) {
		if (i == 0) C[0] = pack({ fin(i), 0, total }, 0);
		else if (i <= bedges.size()) {
			crnt.weight = c2, crnt.idx1 = fin(i);
			for (int j = 0; j < N; j++) if (infos[j].idx1 == leaders[bedges[i - 1].second] && infos[j].weight < crnt.weight) crnt.weight = infos[j].weight;
			C[0] = pack(crnt, 1);
		}
		else C[0] = pack({ fin(i), 0, 0 }, 1);
	}
	else if (r == 5) {
		if (i == 0) {
			vector<pair<ull, pair<int, int>>> edges;
			for (int j = 1; j <= bedges.size(); j++) edges.push_back({ infos[j].weight, bedges[j - 1] });
			sort(edges.begin(), edges.end());
			p.clear();
			for (int j = 0; j < leaders.size(); j++) p.push_back(j);
			for (int j = 0; j < bedges.size(); j++) {
				if (fin(edges[j].second.first) == fin(edges[j].second.second)) continue;
				join(edges[j].second.first, edges[j].second.second, 0);
				total += edges[j].first;
			}
			return { total };
		}
	}
	return C;
}

Edit:

peak

Thank you all for the amazing support I have recieved!

Full text and comments »

  • Vote: I like it
  • -18
  • Vote: I do not like it