Блог пользователя onepersonintheuniverse

Автор onepersonintheuniverse, история, 4 часа назад, По-английски

I'm studying for TÜBTİAK's second step of Middle-School Competitive Programming branch, which is OI-style. I have translated the problem statement into English, keeping all Turkish proper names.


Time limit: 1 s, memory limit, 64 MB

In the cute town of Ilgınkent, there are $$$n$$$ regions and are connected by $$$n-1$$$ roads, forming a tree-like structure. The distance between a region and all of its neighbors† is equal to 1 unit.

The mayor, Ms Kaya, has decided that it would make certain tasks easier if she organizes the complicated tree structure into roads‡ of length $$$k$$$. Ms Kaya wants you to design an algorithm to determine, for a given $$$k$$$ and a tree $$$T$$$, whether it is possible to split up $$$T$$$ into roads of length $$$k$$$.


† A region is another region's neighbor iff there exists a path between the two regions with no other region within said path.
‡ A road is a collection of edges which forms a continuous path. For example, in the tree
  1
  |
2-3-4-6
  |
  5

examples of roads of length 2 are 1-3-5, 2-3-4, and 3-4-5, but not 1-3-6, 2-3-6. An edge may not be in two roads at the same time, making 1-2-3, 2-3-4; 2-3-4, 3-4-6 etc. invalid.

Input.

The first line contains the integer $$$n$$$. The next $$$n-1$$$ lines contain a description of Ilgınkent, where the line "u v" (w/o the quotes) means that there is a road between region $$$u$$$ and region $$$v$$$.

Output.

The output should be a single line — a bit array made of $$$0$$$ and $$$1$$$. For every $$$k$$$ output $$$1$$$ if the tree can be split up into roads of length $$$k$$$ and $$$0$$$ otherwise, left to right.

Constraints.

  • $$$2 \le n \le 10^{5}$$$
  • $$$1 \le k \le n-1$$$
  • $$$1 \le u, v \le n$$$
Or, you can help me understand the code provided
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
3 часа назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
105 минут назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

correct me if iam wrong but the given solution runs at $$$O(K \cdot N^2)$$$ . Its not fast enough for $$$N = 10^5$$$. (maybe process divisors of N-1). Anyway, here's my understanding of the solution:

For a given length K, root the tree at node i and check the sizes of its neighboring subtrees. If the size of a subtree is divisible by K, it can form a path of length K. If it's not, store the remainder z (where z = subtreesize % K), and check if there is another subtree with a size that gives the complement remainder K - z. If such a subtree exists, you can pair the two subtrees to form a path of length K, which will sum to a multiple of K.