BZOJ1718:[USACO]Redundant Paths 分离的路径(双连通分量)
Description
In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another. Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way. There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.
Input
* Line 1: Two space-separated integers: F and R * Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
* Line 1: A single integer that is the number of new paths that must be built.
Sample Input
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
HINT
Solution
首先可以发现,对于一个双连通分量,我们是不用处理它的
那么如果将所有边双缩成一个点的话,很显然我们可以得到一颗树
那么我们只需要处理叶子节点,在叶子节点间两两连边就好了
答案是(叶子节点数+1)/2
Code
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define N (5000+100) 5 using namespace std; 6 7 struct Edge{int to,next;} edge[N<<3]; 8 int n,m,u,v,head[N],num_edge; 9 int Dfn[N],Low[N],dfs_num; 10 int bridge_num,ans; 11 bool Bridge[N],vis[N],dis[N][N]; 12 13 void add(int u,int v) 14 { 15 edge[++num_edge].to=v; 16 edge[num_edge].next=head[u]; 17 head[u]=num_edge; 18 } 19 20 void Tarjan(int x,int fa) 21 { 22 Dfn[x]=Low[x]=++dfs_num; 23 for (int i=head[x]; i; i=edge[i].next) 24 if (!Dfn[edge[i].to]) 25 { 26 Tarjan(edge[i].to,x); 27 Low[x]=min(Low[x],Low[edge[i].to]); 28 if (Low[edge[i].to]>Dfn[x]) 29 Bridge[i]=Bridge[(i-1^1)+1]=true; 30 } 31 else if (Dfn[edge[i].to]<Dfn[x] && edge[i].to!=fa) 32 Low[x]=min(Low[x],Dfn[edge[i].to]); 33 } 34 35 void Dfs(int x) 36 { 37 vis[x]=true; 38 for (int i=head[x]; i; i=edge[i].next) 39 { 40 if(Bridge[i]){bridge_num++; continue;} 41 if (!vis[edge[i].to]) Dfs(edge[i].to); 42 } 43 } 44 45 int main() 46 { 47 scanf("%d%d",&n,&m); 48 for (int i=1; i<=m; ++i) 49 scanf("%d%d",&u,&v),dis[u][v]=dis[v][u]=true; 50 for (int i=1; i<=n; ++i) 51 for (int j=i+1; j<=n; ++j) 52 if (dis[i][j]) 53 add(i,j),add(j,i); 54 for (int i=1; i<=n; ++i) 55 if (!Dfn[i]) 56 Tarjan(i,0); 57 for (int i=1; i<=n; ++i) 58 if (!vis[i]) 59 { 60 bridge_num=0; 61 Dfs(i); 62 if (bridge_num==1) 63 ans++; 64 } 65 printf("%d",(ans+1)/2); 66 }