codeforces 505 D Mr. Kitayuta's Technology

题意:给出n个点,m条有向边,问构造一个新图,最少几条边可以让任意两点间的连通性跟原图一样。
做法:首先做出强连通分量,很显然对于有向图而言,若分图的点不唯一必定成环,当然啦,还需要做的是把这些分图再连起来变成弱连通分量,若某个弱连通分量的点数为v,若有环则贡献v条边,否则贡献v-1条边。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
stacksk;
bool insk[100010],isok[100010];
int step,dfn[100010],low[100010],belong[100010],num[100010],tol;
struct Edge
{
	int to,next;
}edge[200010];
int head[100010],tail;
void add(int from,int to)
{
	edge[tail].to=to;
	edge[tail].next=head[from];
	head[from]=tail++;
}
void dfs(int from)
{
	dfn[from]=low[from]=++step;
	sk.push(from);
	insk[from]=1;
	for(int i=head[from];i!=-1;i=edge[i].next)
	{
		int to=edge[i].to;
		if(dfn[to]==0)
		{
			dfs(to);
			low[from]=min(low[from],low[to]);
		}
		else if(insk[to])
			low[from]=min(low[from],dfn[to]);
	}
	if(dfn[from]==low[from])
	{
		int v;
		do
		{
			v=sk.top();
			sk.pop();
			insk[v]=0;
			belong[v]=tol;
			num[tol]++;
		}while(v!=from);
		if(num[tol]>1)
			isok[tol]=1;
		tol++;
	}
}
int pr[100010];
int seek(int v)
{
	return pr[v]=v==pr[v]?v:seek(pr[v]);
}
int main()
{
	int n,m;
	cin>>n>>m;
	memset(head,-1,sizeof(head));
	while(m--)
	{
		int a,b;
		cin>>a>>b;
		add(a,b);
	}
	for(int i=1;i<=n;i++)
		if(!dfn[i])
			dfs(i);
	for(int i=0;i


time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Shuseki Kingdom is the world's leading nation for innovation and technology. There are n cities in the kingdom, numbered from 1 to n.

Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city x to city y cannot be used to travel from city y to city x. The transportation within each city is extremely developed, therefore if a pipe from city x to city y and a pipe from city y to city z are both constructed, people will be able to travel from city x to city z instantly.

Mr. Kitayuta is also involved in national politics. He considers that the transportation between the m pairs of city (ai, bi) (1 ≤ i ≤ m) is important. He is planning to construct teleportation pipes so that for each important pair (ai, bi), it will be possible to travel from city ai to city bi by using one or more teleportation pipes (but not necessarily from city bi to city ai). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities.

Input

The first line contains two space-separated integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively.

The following m lines describe the important pairs. The i-th of them (1 ≤ i ≤ m) contains two space-separated integers ai and bi(1 ≤ ai, bi ≤ n, ai ≠ bi), denoting that it must be possible to travel from city ai to city bi by using one or more teleportation pipes (but not necessarily from city bi to city ai). It is guaranteed that all pairs (ai, bi) are distinct.

Output

Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose.

Sample test(s)
input
4 5
1 2
1 3
1 4
2 3
2 4
output
3
input
4 6
1 2
1 4
2 3
2 4
3 2
3 4
output
4
Note

For the first sample, one of the optimal ways to construct pipes is shown in the image below:

codeforces 505 D Mr. Kitayuta's Technology_第1张图片

For the second sample, one of the optimal ways is shown below:

codeforces 505 D Mr. Kitayuta's Technology_第2张图片



你可能感兴趣的:(强连通分量)