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

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

Amanda Lounges

면접 대비

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

요약
각 노선에 요구된 개수(0, 1, 2개)에 맞추어 라운지를 둘 공항을 최소 개수로 정합니다.
난이도

보통10점 중 6점

유형
그래프, BFS
정답자
아직 제출이 없습니다

문제

AMANDA AIR has routes between many different airports, and has asked their most important frequent flyers, members of the AA Frequent Flyer program, which routes they most often fly. Based on this survey, Amanda, the CEO and owner, has concluded that AMANDA AIR will place lounges at some of the airports at which they operate.

However, since there are so many routes going between a wide variety of airports, she has hired you to determine how many lounges she needs to build, if at all possible, given the constraints set by her. This calculation is to be provided by you, before any lounges are built. Her requirements specifies that for some routes, there must be lounges at both airports, for other routes, there must be lounges at exactly one of the airports, and for some routes, there will be no lounges at the airports.

She is very economically minded and is demanding the absolute minimum number of lounges to be built.

입력

The first line contains two non-negative integers 1 ≤ n, m ≤ 200 000, giving the number of airports and routes in the Amanda Catalog respectively. Thereafter follow m lines, each describing a route by three non-negative integers 1 ≤ a, b ≤ n and c ∈ {0, 1, 2}, where a and b are the airports the route connects and c is the number of lounges.

No route connects any airport with itself, and for any two airports at most one requirement for that route is given. As one would expect, 0 is a request for no lounge, 1 for a lounge at exactly one of the two airports and 2 for lounges at both airports.

출력

If it is possible to satisfy the requirements, give the minimum number of lounges necessary to do so. If it is not possible, output impossible.

예제4

  1. 예제 1

    입력
    4 4
    1 2 2
    2 3 1
    3 4 1
    4 1 2
    
    예상 출력
    3
    
  2. 예제 2

    입력
    5 5
    1 2 1
    2 3 1
    2 4 1
    2 5 1
    4 5 1
    
    예상 출력
    impossible
    
  3. 예제 3

    입력
    4 5
    1 2 1
    2 3 0
    2 4 1
    3 1 1
    3 4 1
    
    예상 출력
    2
    
  4. 예제 4

    입력
    2 1
    1 2 1
    
    예상 출력
    1