Island Memories

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

요약
모르는 트리에서 간선 하나를 제거해 만들어질 수 있는 연결 구역 후보들이 주어질 때, 모든 기억을 만족하는 트리가 존재하는지 판정한다.
난이도

보통10점 중 7점

유형
트리, 그래프, 구현, 완전 탐색
정답자
아직 제출이 없습니다

문제

There is an island country that has nn islands numbered 11 to nn. The islands are connected by n−1n - 1 bidirectional drawbridges, such that it is possible travel from each island to every other island. Every drawbridge can be lifted to provide clearance for boat traffic. Sometimes a drawbridge can be lifted for a prolonged period of time, during which the country operates as two zones of road traffic. Each zone is a maximal set of islands connected by the un-lifted drawbridges. It is impossible to travel by road from one zone to the other zone. The country never lifts more than one drawbridge at any time in order to mitigate the inconvenience of road travel.

You are writing a tourist guide for people who would like to visit this island country. You found that the country does not yet have a map that describes how their islands are connected by drawbridges. You thus interviewed mm islanders who live in this country to gain some information. Each islander told you a memory that describes which islands were in their zone sometime in the past when there was a drawbridge lifted. However, some islanders might have remembered things wrong. You would like to check if all the islanders’ memories are consistent, which means that there exists a way to connect the islands by drawbridges so that all the zones described by the mm islanders can actually be formed after lifting exactly one drawbridge at a time.

입력

The first line has two integers nn, mm (2≤n,m≤1,0002 ≤ n, m ≤ 1\\, 000), the number of islands in the country and the number of islanders you interviewed.

This is followed by mm islanders’ memories. Each islander’s memory starts with a single integer kk (1≤k<n1 ≤ k < n) on the first line, the number of islands that are in this islander’s zone. The next line has kk integers in increasing order giving those islands in the zone. You may assume that the islanders never left out any islands in their zone when describing their memories (i.e. each set of islands in a memory is maximal as connected by the un-lifted drawbridges).

출력

Output 11 if all the islanders’ memories are consistent, or 00 otherwise.

예제3

  1. 예제 1

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

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

    입력
    5 2
    2
    1 2
    2
    1 3
    
    예상 출력
    0