Under construction

Still being built. Things may change

Cut Vertices and Bridges

Interview

Time limit1sMemory limit512 MB

Summary
Given a tree with N vertices and queries, report for each query whether a specified vertex is a cut vertex or a specified edge is a bridge.
Level

Medium4 of 10

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

Problem

In graph theory, a cut vertex and a bridge are defined as follows.

  • Cut vertex: if deleting a vertex splits the connected component that contained it into two or more components, that vertex is a cut vertex.
  • Bridge: if deleting an edge splits the connected component that contained it into two or more components, that edge is a bridge.

This problem asks for cut vertices and bridges of a tree. A tree is a graph with no cycle in which every vertex is connected.

Given a tree and a list of queries, answer each query.

Input

Input comes from standard input. The first line has the number of vertices NN of the tree. (2≤N≤1000002 \le N \le 100000) The vertices are numbered from 1 to NN. Each of the next N−1N-1 lines has an edge aa and bb, meaning vertex aa and vertex bb are connected. The given graph is always a tree. (1≤a,b≤N1 \le a, b \le N)

The next line has the number of queries qq. (1≤q≤1000001 \le q \le 100000) Each of the next qq lines has a query tt and kk. (1≤t≤21 \le t \le 2) If tt is 1, the query asks whether vertex kk is a cut vertex. If tt is 2, the query asks whether the kk-th edge of the input is a bridge. When tt is 1, 1≤k≤N1 \le k \le N. When tt is 2, 1≤k≤N−11 \le k \le N-1.

Output

Print to standard output. For the qq queries, print one answer per line. Print yes if the queried vertex is a cut vertex or the queried edge is a bridge, and no otherwise.

Examples7

  1. Example 1

    Input
    5
    1 2
    2 3
    3 4
    4 5
    4
    1 1
    1 2
    2 1
    2 2
    
    Expected output
    no
    yes
    yes
    yes
    
  2. Example 2

    Input
    2
    1 2
    3
    1 1
    1 2
    2 1
    
    Expected output
    no
    no
    yes
    
  3. Example 3

    Input
    6
    3 1
    3 2
    3 4
    3 5
    3 6
    7
    1 1
    1 2
    1 3
    1 4
    1 5
    1 6
    2 5
    
    Expected output
    no
    no
    yes
    no
    no
    no
    yes
    
  4. Example 4

    Input
    3
    2 1
    1 3
    5
    1 2
    1 1
    1 3
    2 1
    2 2
    
    Expected output
    no
    yes
    no
    yes
    yes
    
  5. Example 5

    Input
    15
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    4 8
    4 9
    5 10
    5 11
    6 12
    6 13
    7 14
    7 15
    15
    1 1
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    1 9
    1 10
    1 11
    1 12
    1 13
    1 14
    1 15
    
    Expected output
    yes
    yes
    yes
    yes
    yes
    yes
    yes
    no
    no
    no
    no
    no
    no
    no
    no
    
  6. Example 6

    Input
    7
    4 7
    7 2
    2 5
    5 1
    1 6
    6 3
    10
    2 1
    2 2
    2 3
    2 4
    2 5
    2 6
    1 4
    1 3
    1 7
    1 6
    
    Expected output
    yes
    yes
    yes
    yes
    yes
    yes
    no
    no
    yes
    yes
    
  7. Example 7

    Input
    9
    1 2
    2 3
    3 4
    2 5
    3 6
    4 7
    4 8
    8 9
    9
    1 8
    1 8
    1 9
    2 8
    1 5
    1 2
    2 1
    1 9
    1 4
    
    Expected output
    yes
    yes
    no
    yes
    no
    yes
    yes
    no
    yes