This page is still under construction.

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

A Tree With More Root Nodes Is a Better Tree

Time limit2sMemory limit1024 MB

Summary
Maintain a mixed directed/undirected tree under edge-direction updates and after each update report how many nodes can reach all others.
Level

Hard8 of 10

Topics
Tree, DFS, Implementation, Graph
Solved
No attempts yet

Problem

There is a graph with NN nodes and N−1N-1 edges. The nodes are numbered from 11 to NN, and each edge may be directed or undirected. If the direction is removed from every edge, this graph becomes a tree.

A node is called a 'root node' if a path exists from it to every other node. The 'goodness' of this graph is defined as the number of root nodes. Perform the following queries.

  1. Change the edge connecting uu and vv into the directed edge u→vu \rightarrow v.
  2. Change the edge connecting uu and vv into the directed edge u←vu \leftarrow v.
  3. Change the edge connecting uu and vv into an undirected edge.

The results of the queries accumulate in the graph.

Input

The first line gives the number of nodes NN.

From the second line, N−1N-1 lines give information about the edges. Each edge is given as three space-separated tokens UiU_i, DiD_i, ViV_i, where UiU_i and ViV_i are integers denoting node numbers and DiD_i is a string denoting direction, one of ->, <-, --.

  • If DiD_i is ->, the ii-th edge is the directed edge Ui→ViU_i \rightarrow V_i.
  • If DiD_i is <-, the ii-th edge is the directed edge Ui←ViU_i \leftarrow V_i.
  • If DiD_i is --, the ii-th edge is the undirected edge Ui↔ViU_i \leftrightarrow V_i.

The next line gives the number of queries to perform, QQ.

From the next line, QQ lines give the queries in order. The ii-th line gives three tokens uiu_i did_i viv_i, where uiu_i and viv_i are integers denoting node numbers and did_i is a string denoting direction, one of ->, <-, --.

  • If did_i is ->, set the ii-th edge to the directed edge ui→viu_i \rightarrow v_i.
  • If did_i is <-, set the ii-th edge to the directed edge ui←viu_i \leftarrow v_i.
  • If did_i is --, set the ii-th edge to the undirected edge ui↔viu_i \leftrightarrow v_i.

Output

After each query, print the 'goodness' of the graph on its own line.

Constraints

  • 2≤N≤1052 \le N \le 10^5
  • 1≤Q≤1051 \le Q \le 10^5
  • 1≤Ui,Vi≤N1 \le U_i, V_i \le N (1≤i≤N−11 \le i \le N-1)
  • Ui≠ViU_i \ne V_i (1≤i≤N−11 \le i \le N-1)
  • The graph formed by connecting every UiU_i and ViV_i with an undirected edge is a tree.
  • The given graph contains an edge connecting uiu_i and viv_i.

Examples1

  1. Example 1

    Input
    5
    1 -- 2
    2 -> 3
    2 <- 4
    3 -- 5
    5
    2 -- 4
    2 -> 4
    5 -> 3
    2 -- 3
    3 -- 5
    
    Expected output
    3
    2
    0
    1
    4