A Tree With More Root Nodes Is a Better Tree
Time limit2sMemory limit1024 MB
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 nodes and edges. The nodes are numbered from to , 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.
- Change the edge connecting and into the directed edge .
- Change the edge connecting and into the directed edge .
- Change the edge connecting and into an undirected edge.
The results of the queries accumulate in the graph.
Input
The first line gives the number of nodes .
From the second line, lines give information about the edges. Each edge is given as three space-separated tokens , , , where and are integers denoting node numbers and is a string denoting direction, one of ->, <-, --.
- If is
->, the -th edge is the directed edge . - If is
<-, the -th edge is the directed edge . - If is
--, the -th edge is the undirected edge .
The next line gives the number of queries to perform, .
From the next line, lines give the queries in order. The -th line gives three tokens , where and are integers denoting node numbers and is a string denoting direction, one of ->, <-, --.
- If is
->, set the -th edge to the directed edge . - If is
<-, set the -th edge to the directed edge . - If is
--, set the -th edge to the undirected edge .
Output
After each query, print the 'goodness' of the graph on its own line.
Constraints
- ()
- ()
- The graph formed by connecting every and with an undirected edge is a tree.
- The given graph contains an edge connecting and .