#P7157. 「dWoi R1」Physics Problem

「dWoi R1」Physics Problem

Description

There are nn states, numbered from 11 to nn. Among these nn states, there are kk kinds of transition relations. The ii-th transition relation is described as follows: state uiu_i and state viv_i can be converted into each other. When there is no direct transition relation between two states but there is an indirect transition relation, then there is a "升降华" relation between these two states.

Find how many "升降华" relations there are.

Wang Ma cannot solve very hard physics problems, so it is guaranteed that one state can be converted to any other state through direct or indirect transitions.

Input Format

The first line contains two integers n,kn,k, representing the number of states and the number of transition relations.
In the next kk lines, each line contains two integers ui,viu_i,v_i, meaning there is a transition relation between these two states.

Output Format

Output one integer in one line, representing how many "升降华" relations there are.

3 2
1 2
2 3
1

Hint

Explanation for Sample 1

There are 33 states in total, numbered 1,2,31,2,3. There is a transition relation between state 11 and state 22, and between state 22 and state 33. There is no direct transition relation between state 11 and state 33, but they can be converted using state 22 as a bridge, so there is a "升降华" relation between state 11 and state 33. There is only this one "升降华" relation, so output 11.

Constraints

This problem uses bundled tests.

  • Subtask 1 (5 pts): n2n \le 2.
  • Subtask 2 (10 pts): k=n1k=n-1, ui+1=viu_i+1=v_i.
  • Subtask 3 (10 pts): k=n1k=n-1, ui=1u_i=1.
  • Subtask 4 (25 pts): n,k1000n,k \le 1000.
  • Subtask 5 (50 pts): no special restrictions.

For 100%100\% of the testdata, 1n,k1071 \le n,k \le 10^7, 1ui,vin1 \le u_i,v_i \le n.

It is guaranteed that uiviu_i \ne v_i, and there do not exist ij(ui=ujvi=vj)i \ne j ∧(u_i =u_j∧v_i=v_j) and ij(ui=vjuj=vi)i \ne j∧(u_i=v_j∧u_j=v_i).

This sentence can also be understood as: no multiple edges and no self-loops.

Hint

Note that for the following case (a - b means aa can be converted into bb and bb can be converted into aa):

1 - 2
2 - 3
1 - 3

State 11 and state 33 are considered to have a direct transition relation, i.e. the transition relation uses the "shortest path".

Translated by ChatGPT 5