Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
The first line of the input contains two positive integers, n and m — the number of the cities and the number of roads in Berland (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers xi, yi (1 ≤ xi, yi ≤ n, xi ≠ yi), wherexi and yi are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
Print a single integer — the minimum number of separated cities after the reform.
4 3 2 1 1 3 4 3
1
5 5 2 1 1 3 2 3 2 5 4 3
0
6 5 1 2 2 3 4 5 4 6 5 6
1
In the first sample the following road orientation is allowed:
,
,
.
The second sample:
,
,
,
,
.
The third sample:
,
,
,
,
.
题意:给你n个点,m条边,(2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000).刚开始每条边都是无向的,然后把所有的边都改成有向,问最少有多少个点入度为0,
出度不为0的。
思路:如果一个联通图里含有边双连通,那么这个联通图的入度为0,出度不为0的点最少为0个,
如果一个联通图中不含边双连通分量,那么这个联通图的入度为0,出度不为0的点最少为1个。
#include<bits/stdc++.h>
using namespace std;
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
const int maxn=100100;
struct Edge{
int from,next,to;
}e[maxn*2];
int tot,head[maxn];
int Index,top,DFN[maxn],Low[maxn],Stack[maxn],Belong[maxn],num[maxn];
int block;
bool Instack[maxn];
void init(){
tot=0;
mem1(head);
}
void addedge(int from,int to){
e[tot].from=from;
e[tot].to=to;
e[tot].next=head[from];
head[from]=tot++;
}
void Tarjan(int u,int pre){
int v;
Low[u]=DFN[u]=++Index;
Stack[top++]=u;
Instack[u]=true;
int pre_cnt=0;
for(int i=head[u];i!=-1;i=e[i].next){
v=e[i].to;
if(v==pre&&pre_cnt==0){
pre_cnt++;
continue;
}
if(!DFN[v]){
Tarjan(v,u);
if(Low[u]>Low[v])
Low[u]=Low[v];
}
else if(Instack[v]&&Low[u]>DFN[v])
Low[u]=DFN[v];
}
if(Low[u]==DFN[u]){
block++;
do{
v=Stack[--top];
num[block]++;
Instack[v]=false;
Belong[v]=block;
}while(v!=u);
}
}
int vis[maxn];
void bfs(int st){
queue<int>Q;
if(st==0){
for(int i=1;i<=block;i++)
if(num[i]>1&&vis[i]==0){
Q.push(i);
vis[i]=1;
}
}
else
Q.push(st),vis[st]=1;
while(!Q.empty()){
int u=Q.front();
Q.pop();
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].to;
if(vis[v]==1)
continue;
vis[v]=1;
Q.push(v);
}
}
}
void solve(int n){
mem0(DFN);
mem0(vis);
mem0(num);
memset(Instack,false,sizeof(Instack));
Index=top=block=0;
for(int i=1;i<=n;i++)
if(!DFN[i])
Tarjan(i,0);
int tot1=tot;
init();
for(int i=0;i<tot1;i++){
int u=Belong[e[i].from],v=Belong[e[i].to];
addedge(u,v);
}
mem0(vis);
bfs(0);
int ans=0;
for(int i=1;i<=block;i++)
if(vis[i]==0){
ans++;
bfs(i);
}
printf("%d\n",ans);
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
int u,v;
init();
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
solve(n);
return 0;
}
理解复杂网络结构与道路改革

本文探讨了在Berland城市网络中进行道路方向性调整的策略,目标是最小化独立城市的数量。通过分析输入的城市数量、道路数量及具体道路连接情况,实现路径优化与城市连通性的最大化。
1566

被折叠的 条评论
为什么被折叠?



