아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

XOR Tree

시간 제한2초메모리 제한256 MB

요약
간선에 중복 개수가 있는 트리에서 각 질의 쌍 S, T에 대해 간선 토글 게임의 승자를 판정합니다.
난이도

보통10점 중 6점

유형
게임 이론, 트리, 수학, DFS
정답자
아직 제출이 없습니다

문제

There is an undirected graph GG with nn vertices. It has an interesting property: if we treat multiple edges as one, the graph GG will become a tree. Alice and Bob invented the following game:

  • At the beginning, Alice and Bob choose two vertices SS and TT, S≠TS \ne T.
  • There is another graph HH with nn vertices and, initially, no edges.
  • On every turn, the current player chooses any edge u−vu-v in graph GG, deletes it and changes the state of edge u−vu-v in graph HH: if there was no such edge in graph HH, it appears, and if there was such an edge, it disappears.
  • Alice wins if there exists a moment of time when there is a path between SS and TT in graph HH.
  • If there is no edge in graph GG and Alice hasn't won yet, Bob wins.
  • Players take turns, Alice moves first.

Children like the game very much so they have played it qq times. Now they are wondering who would win in each game if they were playing optimally. Help them find it out!

입력

The first line of input contains two integers nn and qq: the number of vertices in graph GG and the number of games played by Alice and Bob (2≤n≤1052 \le n \le 10^5, 1≤q≤1051 \le q \le 10^5).

The next n−1n-1 lines contain the description of graph GG. Each line consists of three integers aa, bb and cc (1≤a,b≤n1 \le a, b \le n, 1≤c≤1091 \le c \le 10^9) which means there are exactly cc edges between vertices aa and bb. It is guaranteed that, if we change all cc to 11, the graph GG will become a tree with nn vertices.

The next qq lines describe the games. Each of these lines contains two integers SS and TT: the parameters of the game (1≤S,T≤n1 \le S, T \le n, S≠TS \ne T).

출력

For each game, print 1 if Alice wins, and print 2 otherwise. Separate the answers with line breaks.

예제1

  1. 예제 1

    입력
    6 5
    1 2 3
    1 3 2
    2 4 1
    2 5 2
    3 6 2
    1 4
    5 4
    5 6
    2 6
    4 6
    
    예상 출력
    1
    1
    2
    2
    2