关闭

zoj 3583 simple path

427人阅读 评论(0) 收藏 举报

Simple Path

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

Author: LIU, Yaoting
Contest: ZOJ 10th Anniversary Contest



枚举所有点,把这个点去掉,从source和dest各一次bfs,标记即不和source连通也不和dest连通的点,标记的点的总数就是answer


#include <cstdio>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

#ifdef _DEBUG
#define debug_printf(...) printf(__VA_ARGS__)
#else
#define debug_printf(...) 0
#endif

const int MAXN = 100;

std::vector<std::vector<int> > adj;

bool hidden[MAXN];

void bfs(int src, int d[])
{
    memset(d, -1, sizeof(*d) * MAXN);

    d[src] = 0;

    std::queue<int> que;
    que.push(src);

    while (! que.empty()) {

        int v = que.front();
        que.pop();

        if (hidden[v]) {
            continue;
        }

        assert(0 <= v && v < adj.size());

        for (int i=0; i<adj[v].size(); ++i) {
            int a = adj[v][i];
            if (d[a] == -1 && !hidden[a]) {
                d[a] = d[v] + 1;
                que.push(a);
            }
        }
    }
}

void solve()
{
    int vertices;
    int edges;
    int src;
    int dest;

    if (scanf("%d%d%d%d", &vertices, &edges, &src, &dest) == EOF) {
        exit(0);
    }

    adj.clear();
    adj.resize(vertices);

    for (int i=0; i<edges; ++i) {
        int a = 0, b = 0;
        scanf("%d%d", &a, &b);
        assert(0 <= a && a < adj.size());
        assert(0 <= b && b < adj.size());

        if (std::find(adj[a].begin(), adj[a].end(), b) == adj[a].end()) {
            adj[a].push_back(b);
        }

        if (std::find(adj[b].begin(), adj[b].end(), a) == adj[b].end()) {
            adj[b].push_back(a);
        }
    }

    bool onceUncovered[MAXN];
    memset(onceUncovered, 0, sizeof(onceUncovered));

    memset(hidden, 0, sizeof(hidden));

    int d[MAXN];
    int e[MAXN];

    for (int i=0; i<vertices; ++i) {
        if (i == dest || i == src) {
            //continue;
        }

        hidden[i] = true;

        bfs(src, d);
        bfs(dest, e);

        for (int v=0; v<vertices; ++v) {
            if (d[v] == -1 && e[v] == -1 && v != i && v != src && v != dest) {
                onceUncovered[v] = true;
            }
        }

        hidden[i] = false;
    }

    int answer = 0;
    for (int i=0; i<vertices; ++i) {
        if (onceUncovered[i] && i != src && i != dest) {
            ++answer;
        }
    }

    printf("%d\n", answer);
}

int main()
{
    while(true) {
        solve();
    }
}


0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:65987次
    • 积分:1564
    • 等级:
    • 排名:千里之外
    • 原创:94篇
    • 转载:0篇
    • 译文:0篇
    • 评论:15条
    最新评论