oj mrJudge
Toggle navigation
  • Login
    • Forget Password
      Login
User Image

Hello, Stranger

Guest
  • Analysis Mode
  • Problems
    • All Problems
    • Latest Problems
  • Join Us Now
  • Registration
  • Contact Us
  • Infomation
  • About
    • Terms of Use
    • Technical Specifications
    • Credits

harmony Batch , stdin/stdout

Registered Users Only

Please login to utilize this feature.

Do note that this website only supports submissions in C++.

statement.html

Problem Statement

There are N students in 302, all seated in a row. They are numbered in order of their seats, so the leftmost person is 1 and the rightmost is N.

There are currently M friendships among the students of 302. Each friendship involves two people and is mutual. Two people are considered to know each other if they can describe each other as "a friend", "a friend of a friend", or using any such chains of friendships.

Recently, fights have been breaking out in 302. The teacher thinks that it is because people who know each other talk a lot in class, and thus annoy the people in between them who do not know them. Thus, the teacher has ordered the class to make friends with more people such that for any two people L and R who know each other, everyone in between them must also know them. This will surely return harmony to the class, thought the teacher.

However, different people in 302 don't really get along very well. Thus, they do not like to make friends. Help them find the minimum number of friendships they must form to create the "harmonious class" that the teacher wants.

Input

The first line of input contains two integers, N and M.

The next M lines of input contain two integers each, Ai and Bi, denoting a friendship between the people numbered as such.

Output

The first and only line of output should contain a single integer, denoting the minimum number of new friendships that must be formed in 302.

Limits

For all subtasks, 3 ≤ N ≤ 200000 and 1 ≤ M ≤ 200000. It is guaranteed that each pair (Ai, Bi) only appears once and Ai ≠ Bi for all i.

Subtask 1 (20%): M = 1

Subtask 2 (80%): There are no more constraints.

Subtask 3 (0%): Sample test cases.

Sample Input 1

6 5
1 3
1 4
1 5
1 6
3 6

Sample Output 1

1

Sample Input 2

5 3
2 1
3 5
4 5

Sample Output 2

0

Explanation

For Sample 1, the class is not harmonious as 2 is in between 1 and 3 (who know each other), but does not know either 1 or 3. However, if 2 makes friends with 4, the class becomes harmonious.

For Sample 2, the class is already harmonious, thus no more friends need to be made.

Tags

Graph Theory, Data Structure

Subtasks and Limits

Subtask Score #TC Time Memory Scoring
120101s256MBMinimum
280221s256MBMinimum
3021s256MBMinimum

Judge Compile Command

g++-8 ans.cpp -o harmony -Wall -Wshadow -static -O2 -lm -m64 -s -w -std=gnu++17 -fmax-errors=512

Accepted Submissions

subIDUserTimeMax Time

Past Submissions

subIDUserTimeScore
mrJudge 09.05.20
Copyright © 2020 mrJudge. All rights reserved.