소들이 줄을 선 순서대로 좋아하는 시리얼을 먼저, 없으면 두 번째로 좋아하는 시리얼을 가져간다고 할 때, 앞에서 i마리를 제거한 각 경우에 시리얼을 받는 소의 수를 구한다.
어려움8그래프유니온 파인드구현그리디아직 제출이 없습니다시간 제한1초메모리 제한512 MBFarmer John's cows like nothing more than cereal for breakfast! In fact, the cows have such large appetites that they will each eat an entire box of cereal for a single meal.
The farm has recently received a shipment with M different types of cereal (1≤M≤105) . Unfortunately, there is only one box of each cereal! Each of the N cows (1≤N≤105) has a favorite cereal and a second favorite cereal. When given a selection of cereals to choose from, a cow performs the following process:
The cows have lined up to get cereal. For each 0≤i≤N−1, determine how many cows would take a box of cereal if Farmer John removed the first i cows from the line.
The first line contains two space-separated integers N and M.
For each 1≤i≤N, the i-th line contains two space-separted integers f_i and s_i (1≤f_i,s_i≤M and f_i=s_i) denoting the favorite and second-favorite cereals of the i-th cow in line.
For each 0≤i≤N−1, print a line containing the answer for i.
If at least two cows remain, then exactly two of them get a box of cereal.