Advertisement
InterviewTime limit1sMemory limit1024 MB
Given a directed graph of who knows whose contact, find the minimum number of people to seed so the news reaches everyone.
- Level
Medium4 of 10
- Topics
- Graph, DFS, Implementation, Greedy
- Solved
- No attempts yet
Problem
JOI has finally finished developing its new product, the "Incredibly Odd Instrument". The president obtained the contact details of everyone expected to buy it and intends to send them an announcement about the new product, but realized that sending the announcement to too many people could make the company look like a shady operator.
So the company decided to send the announcement to as few people as possible and let word of mouth spread the news of the new product to everyone. The product is so innovative that anyone who learns about it is expected to immediately pass the information about the new product to everyone whose contact details they know.
Write a program that finds how many people the announcement must be sent to so that information about the new product reaches everyone expected to buy it.
Input
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000) separated by a space. n is the number of people expected to buy the new product.
The following m lines (the second line through the (m + 1)-th line) describe who knows whose contact details. The (j + 1)-th line (1 ≤ j ≤ m) contains two integers a_j and b_j (1 ≤ a_j, b_j ≤ n, a_j ≠ b_j) separated by a space. This means person a_j knows the contact details of person b_j. People are numbered with the integers from 1 to n. If j ≠ k, then a_j = a_k and b_j = b_k never holds.
Output
Output to standard output. Print one integer, the minimum number of people to whom the announcement must be sent.