标签:
题意:给出n个点,m条有向边,问构造一个新图,最少几条边可以让任意两点间的连通性跟原图一样。#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]; pr[b]=a; } } for(int i=0;i<tol;i++) if(pr[i]==i) ans+=num[i]+(isok[i]?0:-1); cout<<ans; }
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.
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.
Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta‘s purpose.
4 5 1 2 1 3 1 4 2 3 2 4
3
4 6 1 2 1 4 2 3 2 4 3 2 3 4
4
For the first sample, one of the optimal ways to construct pipes is shown in the image below:
For the second sample, one of the optimal ways is shown below:
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 505 D Mr. Kitayuta's Technology
标签:
原文地址:http://blog.csdn.net/stl112514/article/details/47189561