Underspecified Ultrametrics

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

요약
일부 점 쌍의 거리만 주어졌을 때, 나머지 거리를 채워 전체 집합이 초거리 공간이 되도록 만들 수 있는지 판정한다.
난이도

어려움10점 중 8점

유형
유니온 파인드, 정렬, 그래프, 그리디
정답자
아직 제출이 없습니다

문제

Given a set of points XX with distances d(u,v)d(u,v) between any u,v∈Xu,v \in X, we say that (X,d)(X,d) is an ultrametric if the following properties are satisfied:

  • d(u,v)≥0d(u,v)≥0 for any two points uu, vv with d(u,v)=0d(u,v)=0 if and only if u=vu=v
  • d(u,v)=d(v,u)d(u,v)=d(v,u) for any two points uu, vv
  • d(u,v)≤max⁡d(u,w),d(w,v)d(u,v)≤\max\\{d(u,w),d(w,v)\\} for any three points uu, vv, ww

That is, distances in an ultrametric satisfy a slightly stronger property than the usual triangle inequality d(u,v)≤d(u,w)+d(w,v)d(u,v)≤d(u,w)+d(w,v).

You have measured distances between some pair of points from some set XX and start to wonder if you might be looking at an ultrametric. Write a program to help you determine if this is the case!

입력

The first line of input contains two integers NN (3≤N≤100,0003≤N≤100\\,000) and MM (0≤M≤400,0000≤M≤400\\,000) where NN is the number of points in the set XX and MM is the number of distances you have determined so far. The points are numbered from 00 to N−1N-1.

Then MM lines follow, each containing three integers uu, vv, ℓℓ (0≤u\<v\<N0≤u\<v\<N and 1≤ℓ≤1091≤ℓ≤10^9) where uu, vv are two points in XX and ℓℓ is the distance d(u,v)d(u,v) you have determined between these points. No pair of points will have their distance specified on more than on line.

출력

Output possibly ultrametric if there is an ultrametric (X,d′)(X,d') where the distances satisfy d′(u,v)=d(u,v)d'(u,v)=d(u,v) for any u,v∈Xu,v \in X such that one of the MM given distances you have already determined is for the pair (u,v)(u,v). Otherwise, output the message not ultrametric.

예제5

  1. 예제 1

    입력
    3 3
    0 1 5
    0 2 7
    1 2 7
    
    예상 출력
    possibly ultrametric
    
  2. 예제 2

    입력
    3 3
    0 1 5
    0 2 7
    1 2 5
    
    예상 출력
    not ultrametric
    
  3. 예제 3

    입력
    5 5
    0 1 10
    1 2 11
    2 3 12
    3 4 13
    0 4 12
    
    예상 출력
    not ultrametric
    
  4. 예제 4

    입력
    5 5
    0 1 10
    1 2 11
    2 3 12
    3 4 13
    0 4 13
    
    예상 출력
    possibly ultrametric
    
  5. 예제 5

    입력
    7 5
    0 1 3
    4 5 1
    0 2 1
    1 2 3
    4 6 10
    
    예상 출력
    possibly ultrametric