为什么我这个代码在oj系统上显示runtime error?

代码与无向图的双联通分量有关,求一个无向图中至少添加几条边使从一个地点到另一个地点至少有两条路,我的理解是每个点至少都存在于一个回路中
#include<stdio.h>
#include<string.h>
#define min(a,b) (a>b?b:a)
#define Max 5555
int matrix[Max][Max];
int vis[Max],low[Max],dfn[Max],dfs_clock;
int cnt[111111],count;
int Stack[Max],top=-1;
int n,e;
void init()
{
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(matrix,0,sizeof(matrix));
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    count=0;
    dfs_clock=1;
}
void dfs(int u)//非递归深度遍历生产low值
{
    int v,i;

    low[u]=dfn[u]=dfs_clock++;
    vis[u]=1;
    top++;
    Stack[top]=u;

    while(top!=-1){
        v=Stack[top];
        if(!vis[v]){
            low[v]=dfn[v]=dfs_clock++;
            vis[v]=1;
        }

        for(i=1;i<=n;i++){
            if(matrix[v][i]==1){
                if(!vis[i]){
                    top++;
                    Stack[top]=i;
                    break;
                }
                else{
                    if(vis[v+1])//与前一个比较,判断是否在同一个回路中
                       low[v]=min(low[v],low[v+1]);
                    if(i!=Stack[top-1])//形成回路
                       low[v]=min(low[v],dfn[i]);
                }
        }
        if(i==n)
            top--;
        }
    }
}
int main()
{
    int i,j;
    int u,v;
    
    while(scanf("%d%d",&n,&e)!=EOF){
        init();

        for(i=0;i<e;i++){
            scanf("%d%d",&u,&v);
            matrix[u][v] = 1;
            matrix[v][u] = 1;
        }
        
        dfs(1);
        
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++){
                if(matrix[i][j]==1){
                    if(low[i]!=low[j])//如果不等,则不连通 
                       cnt[low[j]]++;
                }
            }
        
        for(i=1;i<=n;i++){
            if(cnt[i]==1)
               count++;
        }
        
        printf("%d\n",(count+1)/2);
    }
    return 0;
}

下面是原题(大概意思就是在各个农场中间至少添加多少条路使从一个农场到另一个农场至少存在2条路)
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
Multiple test case. For each case :
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
For each case, output one line: A single integer that is the number of new paths that must be built.

阅读 2.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题