码迷,mamicode.com
首页 > 其他好文 > 详细

(水DFS) zoj 3583

时间:2015-04-16 23:12:07      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

Q - Simple Path
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

A path with no repeated vertices of an undirected graph is called a simple path. Given an undirected graph and two verteices S and D, return the number of vertics which don‘t lie on any simple paths between S and D.

Input

The input contains multiple test cases.

Each case starts with a line of four integers, N(1 < N ≤ 100), M(1 ≤ M ≤ N(N - 1) / 2), S(0 ≤ S < N), D(0 ≤ D < N). N is the number of vertices, M is the number of edges, S and D are two different vertices. Then M lines follow, each line contains two different integers A(0 ≤ A < N) and B(0 ≤ B < N), which represents an edge of the graph. It‘s ensure that there is at least one simple path between S and D.

Output

Output the number of such vertics, one line per case.

Sample Input

4 3 0 2
0 1
1 2
1 3
4 4 0 2
0 1
1 2
1 3
2 3

Sample Output

1
0

简单路上的点一定与s,d联通
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
int n,m,s,d,mp[105][105],vis[105],mark[105];
void dfs(int x)
{
    for(int i=0;i<n;i++)
    {
        if(!vis[i]&&mp[x][i])
        {
            vis[i]=1;
            dfs(i);
        }
    }
}
int main()
{
    int x,y;
    while(scanf("%d%d%d%d",&n,&m,&s,&d)!=EOF)
    {
        int ans=0;
        memset(mp,0,sizeof(mp));
        memset(mark,0,sizeof(mark));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            mp[x][y]=mp[y][x]=1;
        }
        for(int i=0;i<n;i++)
        {
            memset(vis,0,sizeof(vis));
            vis[i]=1;
            if(!vis[s]) dfs(s);
            if(!vis[d]) dfs(d);

            for(int j=0;j<n;j++)
                if(!vis[j]&&j!=s&&j!=d)
                    mark[j]=1;
        }
        for(int i=0;i<n;i++)
            if(mark[i])
                ans++;
        printf("%d\n",ans);
    }
    return 0;
}

  

(水DFS) zoj 3583

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4433352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!