POJ

来源:互联网 发布:淘宝店过户费用 编辑:程序博客网 时间:2024/05/22 07:07

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
7 71 22 33 42 54 55 65 7
Sample Output
2
Hint
Explanation of the sample: 

One visualization of the paths is: 
   1   2   3   +---+---+         |   |       |   | 6 +---+---+ 4      / 5     /     /  7 +
Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 
   1   2   3   +---+---+     :   |   |   :   |   | 6 +---+---+ 4      / 5  :     /     :    /      : 7 + - - - - 
Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7
 
Every pair of fields is, in fact, connected by two routes. 

It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

题意:

   为了保护放牧环境,避免牲畜过度啃咬同一个地方的草皮,牧场主决定利用不断迁移牲畜进行喂养的方法去保护牧草。然而牲畜在迁移过程中也会啃食路上的牧草,所以如果每次迁移都用同一条道路,那么该条道路同样会被啃咬过度而遭受破坏。

       现在牧场主拥有F个农场,已知这些农场至少有一条路径连接起来(不一定是直接相连),但从某些农场去另外一些农场,至少有一条路可通行。为了保护道路上的牧草,农场主希望再建造若干条道路,使得每次迁移牲畜时,至少有2种迁移途径,避免重复走上次迁移的道路。已知当前有的R条道路,问农场主至少要新建造几条道路,才能满足要求?

思路:先将给出的图进行缩点,找出叶子节点,至少添加的边数(leaf  + 1)/ 2;(自己找几个树画画就知道为什么是这个数了);还有就是一定要判重边;不判重边就是错的;

代码:

#include<stdio.h>#include<string.h>#include<vector>#include<stack>#include<algorithm>using namespace std;#define Max 5010int n,m;int dfn[Max];  //时间戳 int low[Max]; //为i或i的子树能够追溯到的最早的栈中节点的次序号int instack[Max]; //  int color[Max]; // 染色 强连通分量,为了进行缩点;int fathar[Max];  // 记录父亲节点,方便查找缩点后的图节点的出度和入度int dfn_num,color_num;vector<int > v[Max];stack<int > s;void init(){int i;//s.clear();for(i=0;i<=n;i++){v[i].clear();dfn[i] = 0;low[i] = 0;color[i] = 0;instack[i] = 0;fathar[i] = 0;}}void tarjan(int x,int fa)   // tarjan算法 {dfn[x] = low[x] = dfn_num++;fathar[x] = fa;        // 方便以后求再缩点之后的节点的度数 s.push(x);int i;for(i=0;i<v[x].size();i++){int k = v[x][i];if(!dfn[k]){tarjan(k,x);low[x] = min(low[x],low[k]);}else if(k!=fa){low[x] = min(low[x],dfn[k]);}}if(low[x]==dfn[x]){color[x] = ++color_num;int t;do{t = s.top();s.pop();color[t] = color_num;}while(t!=x);}}int fff(){int out[Max],i,j;memset(out,0,sizeof(out));for(i=1;i<=n;i++){int k = fathar[i];    // 用fathar[] 数组判断重边;  if(color[i]!=color[k]){out[color[i]]++;out[color[k]]++;}}int con = 0;    //叶子节点数 for(i=1;i<=color_num;i++)if(out[i]==1)con++;return (con+1)/2;  //至少增加的边数; }int main(){int i,j;while(~scanf("%d%d",&n,&m)){init();int x,y;for(i=0;i<m;i++){scanf("%d%d",&x,&y);v[x].push_back(y);v[y].push_back(x);}dfn_num = 1;color_num = 0;for(i=1;i<=n;i++){if(!dfn[i])tarjan(i,i);}printf("%d",fff());}return 0;}


不判重边的代码 wa 

#include<stdio.h>#include<string.h>#include<vector>#include<stack>#include<algorithm>using namespace std;#define Max 5010int n,m;int dfn[Max];int low[Max];int instack[Max];int color[Max];int dfn_num,color_num;vector<int > v[Max];stack<int > s;void init(){int i;//s.clear();for(i=0;i<=n;i++){v[i].clear();dfn[i] = 0;low[i] = 0;color[i] = 0;instack[i] = 0;}}void tarjan(int x,int fa){dfn[x] = low[x] = dfn_num++;s.push(x);int i;for(i=0;i<v[x].size();i++){int k = v[x][i];if(!dfn[k]){tarjan(k,x);low[x] = min(low[x],low[k]);}else if(k!=fa){low[x] = min(low[x],dfn[k]);}}if(low[x]==dfn[x]){color[x] = ++color_num;int t;do{t = s.top();s.pop();color[t] = color_num;}while(t!=x);}}int fff(){int out[Max],i,j;memset(out,0,sizeof(out));for(i=1;i<=n;i++){for(j=0;j<v[i].size();j++)  // 没有判断重复的边; {int k = v[i][j];if(color[i] != color[k]){out[color[i]]++; }}}int con = 0;for(i=1;i<=color_num;i++)if(out[i]==1)con++;return (con+1)/2;}int main(){int i,j;while(~scanf("%d%d",&n,&m)){init();int x,y;for(i=0;i<m;i++){scanf("%d%d",&x,&y);v[x].push_back(y);v[y].push_back(x);}dfn_num = 1;color_num = 0;for(i=1;i<=n;i++){if(!dfn[i])tarjan(i,i);}printf("%d",fff());}return 0;}


测试数据:

2 2

1 2

1 2

输出

1

但是不判重边的为 0 ;