This page is still under construction.

Parts of this page are still being built. What you see may change.

Failing Roads

Time limit1sMemory limit128 MB

Summary
Given an expression tree of merge and complement operations, compute the maximum independent set of the resulting graph.
Level

Hard8 of 10

Topics
Tree, Dynamic programming, Divide and conquer, Implementation
Solved
No attempts yet

Problem

There are nn towns in the kingdom of Failland. In the past, Failland had an excellent road system that connected every pair of towns directly, with no two roads crossing (a complicated system of bridges kept them apart). Recently, however, the Road-workers Union split into nn independent divisions, one per town. Because of a fierce rivalry between the divisions, each division flatly refuses to maintain any road leading to a town controlled by another division. Since each division initially controlled a single town, all of the roads soon fell completely into ruin.

The wise king of Failland decided to improve matters by decree. In a series of decrees he ordered certain divisions to reunite. Whenever the road-workers received such a decree they obeyed (wouldn't you obey, when the only alternative was losing your head?), and the divisions in question were immediately merged into one. But since the decree did not say otherwise and the workers were lazy, merging did not change which roads were maintained: the merged division kept maintaining exactly the roads it had maintained before.

The king then began issuing a second kind of decree, ordering the road-workers of some division to immediately repair every destroyed road between the towns that division controls. He repeated this process of merging divisions and ordering repairs several times, and finally, once a single division remained, he considered the matter solved and left on vacation.

The citizens of Failland soon noticed that something was still wrong: there were far too few roads. On investigation they discovered that whenever a division was ordered to repair all destroyed roads, the workers also assumed they could stop maintaining the roads they had maintained before, and those roads quickly decayed.

To persuade the king to return and fix things, the citizens decided to find as many towns as possible such that no two of them are joined by a maintained road, and to send this list to the king. This turned out to be too hard, so they have asked you for help: report the largest possible number of such towns.

Input

The input consists of several scenarios. Each scenario is a single line containing one expression that describes the sequence of the king's decrees, and hence the current state of the roads in Failland. The expression is one of the following:

  • V stands for a single town controlled by a single division.
  • U e1e_1 e2e_2, where e1e_1 and e2e_2 are expressions describing disjoint sets of towns, each set controlled by a different division. U e1e_1 e2e_2 describes the situation after the king ordered these two divisions to unite: the new division now controls all towns of e1e_1 and e2e_2 and maintains the same roads as before (the union of the roads maintained in e1e_1 and e2e_2).
  • C ee, where ee is an expression, describes the situation after the division described by ee was ordered to take care of the roads it had neglected. The division still controls the same towns, but now maintains exactly those roads it did not maintain before (only roads between two towns both controlled by this division are affected). The roads it used to maintain are no longer maintained and are considered destroyed immediately.

For example, C U U V V V describes a land of three towns controlled by a single division, with all three roads between them in perfect condition, forming a triangle. U C U U V V V C U U V V V describes a land of six towns formed as the union of two such triangles. C U C U U V V V C U U V V V describes the same land after the decree ordering the workers to repair the neglected roads: it now has six towns and 9 roads -- every road between every pair of towns except the six that belonged to the triangles.

Each line of the input contains at most 200 000 characters.

Output

For each scenario, output a single line containing one integer: the maximum number of towns such that no two of them are connected by a maintained road.

Examples1

  1. Example 1

    Input
    U V V
    U C U V V V
    C U C U U V V V C U U V V V
    
    Expected output
    2
    2
    3