(水DFS) zoj 3583

Q - Simple Path

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status Practice ZOJ 3583

Appoint description: 
System Crawler  (2015-04-16)

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;
}

  

时间: 2024-10-17 11:52:09

(水DFS) zoj 3583的相关文章

[dfs] zoj 3736 Pocket Cube

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3736 Pocket Cube Time Limit: 2 Seconds      Memory Limit: 65536 KB Pocket Cube is a 3-D combination puzzle. It is a 2 × 2 × 2 cube, which means it is constructed by 8 mini-cubes. For

[迭代加深dfs] zoj 3768 Continuous Login

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3768 Continuous Login Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge Pierre is recently obsessed with an online game. To encourage users to log in, this game wi

[数学+dfs] ZOJ 3753 Simple Equation

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5176 Simple Equation Time Limit: 2 Seconds      Memory Limit: 65536 KB There are many Equations. Some are difficult to solve, for example: an xn + an-1 xn-1 + .. + a0 = 0. In this probl

[水题+dfs] zoj 3861 Valid Pattern Lock

题意: 给n个不同的整数(3<=n<=9),问你能绘制出多少种解锁的方案. 输出方案数,以及按字典序输出每种方案. 思路: 一个很水的dfs全排列,加上特判就好了. 特判就是1->9的话就必定经过5等. 这里要注意的是. 中间所经过的数字是必须存在的. 比如要想1->9就必须有5. 5要么被用过,要么就经过5 例子就是 1 3 5 9这四个数. 实际的方案是只有2种 3 5 1 9 和 3 5 9 1 然后就是输入完排下序,保证字典序. 最后就是弱太弱了,写了2个dfs一个算个数,

stack+DFS ZOJ 1004 Anagrams by Stack

题目传送门 1 /* 2 stack 容器的应用: 要求字典序升序输出,所以先搜索入栈的 3 然后逐个判断是否满足答案,若不满足,回溯继续搜索,输出所有符合的结果 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <stack> 9 #include <cmath> 10 #include <cstring> 11 #inc

HDU 4707 水DFS

所有房子组成一颗树,求出离根节点0的距离大于d的节点数目 DFS+vector存边 水过 #include "stdio.h" #include "string.h" #include "vector" using namespace std; vector<int>mapp[100010]; int ans,d; void dfs(int cur,int pre,int op) { int i; for (i=0;i<mapp

zzuli1985(dp/水dfs郑轻比赛)

再一次感受到dp的威力 1985: 即将到来的新生赛 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 112  Solved: 28 SubmitStatusWeb Board Description 新生赛马上就要到来了.为了举办这次比赛,学校也是大费苦心.由于时间紧迫,要准备的事情太多,人员安排也是很伤脑子.身为一个聪明的acmer,这点小事对你来说应该是So easy! 距离新生赛开始剩余100个小时,现在还剩余m项任务.每个任务都有开始时间,

水题 ZOJ 3875 Lunch Time

题目传送门 1 /* 2 水题:找排序找中间的价格,若有两个,选价格大的: 3 写的是有点搓:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cmath> 9 #include <cstring> 10 #include <string> 11 #include <map> 12 #include <

水题 ZOJ 3876 May Day Holiday

题目传送门 1 /* 2 水题:已知1928年1月1日是星期日,若是闰年加1,总天数对7取余判断就好了: 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <map> 11 #include