标签:const from sum nec cin 技术 else call 连接
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.
The i-th edge (1≤i≤M) connects Vertex ai and Vertex bi.
An edge whose removal disconnects the graph is called a bridge.
Find the number of the edges that are bridges among the M edges.
Input is given from Standard Input in the following format:
N M a1 b1 a2 b2 : aM bM
Print the number of the edges that are bridges among the M edges.
7 7 1 3 2 7 3 4 4 5 4 6 5 6 6 7
4
The figure below shows the given graph:
The edges shown in red are bridges. There are four of them.
3 3 1 2 1 3 2 3
0
It is possible that there is no bridge.
6 5 1 2 2 3 3 4 4 5 5 6
5
It is possible that every edge is a bridge.
题目意思:判断给定的图像中的不属于环的边有几条;
主要思路:1.遍历判断每一条边中的一点能否到达另一点
代码1:每次判断每条边时,都创建一个地图副本,把每次走过的边都消除,
比较容易理解,但是遇到多的数据的时候,会很慢;
#include<iostream> #include<string> #include<algorithm> #include <map> #include <string.h> using namespace std; struct S { int Map[55][55]; }; int N,M; int a[55],b[55],Map[55][55]; bool dfs(S s,int l,int r) { // cout<<" -> "<<l<<" "<<r<<endl; int falg = false; for(int i = 1;i<=N;i++){ if((l>i&&s.Map[i][l])||(l<i&&s.Map[l][i])){ if(l>i) s.Map[i][l] = 0; else s.Map[l][i] = 0; if(i==r) return true; if(dfs(s,i,r)) return true; } } return falg; } int main() { cin>>N>>M; for(int i = 1;i <=M; i++){ cin>>a[i]>>b[i]; if(a[i]>b[i]) Map[b[i]][a[i]] = 1; else Map[a[i]][b[i]] = 1; } int sum = 0; for(int i = 1;i<=M;i++){ S s; memcpy(s.Map,Map,sizeof(Map)); if(a[i]>b[i]) s.Map[b[i]][a[i]] = 0; else s.Map[a[i]][b[i]] = 0; if(!dfs(s,a[i],b[i])) sum++; // cout<<a[i]<<" "<<b[i]<<" <-> "<<sum<<endl; } cout<<sum<<endl; return 0; }
代码2;相当与上面代码的改进,用一个临时数组来储存每次经过的头
#include<iostream> #include<string> #include<algorithm> #include <string.h> #include <stdio.h> #include <math.h> #include <set> #include <queue> #include <stack> #include <map> #include <stdlib.h> using namespace std; int N,M; int a[55],b[55],Map[55][55]; int visit[55]; bool dfs(int l,int r,int num) { int j ; if(l==r||Map[l][r]==1) return true; visit[num] = l; for(int i = 1;i<=M;i++){ for(j=0;j<num;j++){ if(visit[j]==i) break; } if(j<num&&visit[j]==i) continue; if(Map[l][i]!=1) continue; if(dfs(i,r,num+1)) return true; } return false; } int main(){ cin>>N>>M; for(int i = 1;i<=M;i++){ cin>>a[i]>>b[i]; Map[a[i]][b[i]] = Map[b[i]][a[i]] = 1; } int sum = 0; for(int i = 1;i<=M;i++){ memset(visit,0,sizeof(visit)); Map[a[i]][b[i]] = 0; if(!dfs(a[i],b[i],0)) sum++; Map[a[i]][b[i]] = 1; } cout<<sum<<endl; return 0; }
代码3:使用了Floyd算法,来判断俩点之间的连通性;
代码4:储存每个结点连接的边的个数,每次都值为1的点出发,当前结点的值和相邻的点的值都减一;
当没有值为一的点时结束;过程中值为1的点的个数就是不循环的边的个数;
标签:const from sum nec cin 技术 else call 连接
原文地址:http://www.cnblogs.com/a2985812043/p/7671144.html