zoj 3583 simple path

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章