shsh's blog

By shsh, history, 7 months ago, In English

Problem: 2155F - Juan's Colorful Tree

For this problem, it turns out that if you just iterate over the colors in the smaller of the two sets $$$C_u$$$ and $$$C_v$$$ (and also de-dupe queries), total complexity is bounded by something like $$$s \sqrt {q}$$$. More generally, let's say we have an array $$$a_i$$$ such that $$$\sum a_i = s$$$. If we pick $$$q$$$ distinct pairs of indices $$$x_i \leq y_i$$$, then it turns out that over all possible choices, $$$\sum \min(a_{x_i}, a_{y_i})$$$ is bounded by $$$s \sqrt {q}$$$. Equality is achieved when $$$a$$$ has $$$\sqrt{q}$$$ values equal to $$$\frac{s}{\sqrt{q}}$$$, and all other values equal to $$$0$$$.

Are there any other problems that use this very interesting bound?

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

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by shsh (previous revision, new revision, compare).

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by shsh (previous revision, new revision, compare).

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by shsh (previous revision, new revision, compare).

»
7 months ago, hide # |
Rev. 2  
Vote: I like it +5 Vote: I do not like it

506D - Mr. Kitayuta's Colorful Graph has a similar solution.

»
7 months ago, hide # |
Rev. 2  
Vote: I like it +8 Vote: I do not like it

I haven't seen this bound before in any problems, but I think it might be somewhat well known for counting the number of triangles in a graph in O(Esqrt(E)).

»
7 months ago, hide # |
 
Vote: I like it +11 Vote: I do not like it

its all over the screen. so white

»
7 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

There is a problem I authored for EGOI 2024 that uses a similar idea, see here.

»
7 months ago, hide # |
Rev. 2  
Vote: I like it +3 Vote: I do not like it

It is a certain case of the problem of finding triangles in a graph. Imagine a graph on $$$n + k$$$ vertices, draw edges between $$$x$$$ and $$$v$$$ iff $$$x \in C_v$$$. For each query $$$(v, u)$$$ draw this edge. Then the solution would be something like this:

bruteforce color $$$x$$$. Find the connected components, then we need to find all such pairs $$$(v, u)$$$ such that they're the endpoints of some query, $$$x \in C_u$$$ and $$$x \in C_v$$$, which is a triangle in our graph. If someone draws an oriented edge from $$$v$$$ to $$$u$$$ such that $$$(|C_v|, v) \lt (|C_u|, u)$$$, then bruteforcing $$$x$$$ bruteforcing all vertices $$$v$$$ such that $$$x \in C_v$$$ bruteforcing all queries from $$$v$$$ is actually searching for all triangles in a graph and works in $$$m \sqrt m$$$ where $$$m$$$ is the total number of edges. (just to clarify, this search doesn't work simply because the number of triangles is bounded, but because it itself works in this complexity, which can also be shown)

  • »
    »
    7 months ago, hide # ^ |
     
    Vote: I like it +8 Vote: I do not like it

    Got it! Do you have a link to a good resource explaining triangle counting?

    • »
      »
      »
      7 months ago, hide # ^ |
       
      Vote: I like it +8 Vote: I do not like it

      I only have one in russian, but the topic is easy, so I can as well explain it here.

      Suppose there is a graph with $$$n$$$ vertices and $$$m$$$ edges.

      First I'm gonna prove, that there are $$$O(m \sqrt m)$$$ triangles. For that I'll split vertices into light, when $$$deg_v \le \sqrt m$$$ and heavy, when $$$deg_v \gt \sqrt m$$$. Then there are $$$O(\sqrt m)$$$ heavy vertices. Each triangle has either a light or a heavy edge, that means, the edge connects both vertices of that type.

      Fix a light edge, there are $$$O(m)$$$ of these, for each edge there is $$$O(\sqrt m)$$$ ways to pick the third vertice, since it must lie in the intersection of two adjacency lists.

      For a heavy edge we'll fix one of the endpoints and the third vertice, there are $$$O(m)$$$ variants to do that since it's an edge of the graph. There are $$$O(\sqrt m)$$$ ways to find a heavy vertice since there are only $$$O(sqrt m)$$$ in total.

      This proves an upperbound on the number of triangles. To get the lowerbound just construct a full graph on $$$\sqrt{2m}$$$ vertices. Also this is an algorithm to find the triangles.

      But there is another approach which is easier to code, so some people use it instead (maybe without knowing the proof, I've only learned it today)

      That is to order vertices by their degree (and number in case of same degrees). Then we want to find all triples $$$(v, u, w)$$$ such that $$$v \lt u \lt w$$$ and for all three pairs there's an edge. So in a way we orient edges. Now the key observation is that outdegree is bounded by $$$\sqrt {2m}$$$. For light vertices the bound is achieved by their degree and for heavy vertices the bound is achieved by the number of the vertices to the right of them. So this code

      for v
          for u in g[v]
              mark[u] = true
          for u in g[v]
              for w in g[u]
                  if mark[w]:
                       #(v, u, w) triangle
          for u in g[v]
              mark[u] = false
      

      works in $$$O(m \sqrt m)$$$, if all of the edges are to some bigger vertice and there are no repeated edges.

»
7 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The first problem that I've seen with that idea is 805F - Expected diameter of a tree (the problem appeared around the same time I started doing CF so I saw it before 506D which is another notable appearence of that idea). There's also the very common "find the number of triangles in a sparse graph" type of problem that uses the same idea.