Codeforces 505 D-Mr Kitayuta ' s technology

Source: Internet
Author: User

Test instructions: Given n points, M-Bar has an edge, asked to construct a new diagram, at least a few sides can make any two points between the same connectivity as the original.
Practice: First to make strong connectivity components, it is obvious to the graph, if the point of the map is not the only bound to become a ring, of course, but also need to do is to connect these sub-graphs to become weakly connected components, if a weak connected component of the point is V, if there is a ring to contribute v edge, otherwise contribute V-1 edge.

#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace Std;stack<int>sk;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<tol;i++) Pr[i]=i;int ans=0;for (int. i=1;i<=n;i++) for ( int j=head[i];j!=-1;j=edge[j].next) {int A=belong[i],b=belong[edge[j].to];a=seek (a); B=seek (b); if (a!=b) {isok[a]|= ISOK[B];NUM[A]+=NUM[B];p r[b]=a;}} for (int i=0;i<tol;i++) if (pr[i]==i) ans+=num[i]+ (isok[i]?0:-1); Cout<<ans;}


Time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

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

Thanks to Mr Kitayuta's, it has the finally become possible to construct teleportation pipes between. A teleportation pipe would connect the cities unidirectionally, that's, a teleportation pipe from the cityxTo CityyCannot be usedyTo Cityx. The transportation within are extremely developed, therefore if a pipe from cityxTo Cityyand a pipe from the cityyTo CityZIs both constructed, people'll be ablexTo CityZInstantly.

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

Input

The First line contains 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 followingmLines describe the important pairs. TheI-th of them (1?≤? i? ≤? m ) contains space-separated integers ai and bi (1?≤? a i,? b i? ≤? n,? a i? ≠? b I ), denoting that it must be possible ai To City bi By using one or more teleportation pipes (and not necessarily from the city bi To City ai ). It is guaranteed this all pairs(ai,? bi)is distinct.

Output

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

Sample Test (s) input
4 51 21 31 42 32 4
Output
3
Input
4 61 21 42 32 43 23 4
Output
4
Note

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

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




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 505 D-Mr Kitayuta ' s technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.