PAT1013Battle Over Cities (25)

来源:互联网 发布:usbwriter linux 编辑:程序博客网 时间:2024/05/17 08:55

It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.

Input

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.

Output

For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input3 2 3
1 2
1 3
1 2 3

Sample Output
1
0
0

方法一:深度优先

#define N 1005int matrix[N][N];int tmp[N][N];int vis[N];int partNum;void dfs(int x, int n){    if (!vis[x]){        vis[x] = 1;        for (int i = 1; i <= n; ++i){            if (tmp[x][i] && !vis[i])                dfs(i, n);        }    }}void f(int x, int n){    int j, i;    for (i = 1; i <= n; ++i){        for (j = 1; j <= n; ++j){            if (i == x || j == x)tmp[i][j] = 0;            else tmp[i][j] = matrix[i][j];        }    }    partNum = 0;    memset(vis, 0, sizeof(vis));    for (i = 1; i <= n; ++i){        if (!vis[i])        {            dfs(i, n);            ++partNum;        }    }    printf("%d", partNum - 2);}void PAT1013Apro(){    int n, m, k, i, j, a, b, c;    while (cin >> n >> m >> k)    {        for (i = 1; i <= n; i++, matrix[i][i] = 1)            for (j = 1; j <= n; j++)            {                matrix[i][j] = 0;            }        for (i = 0; i < m; i++)        {            cin >> a >> b;            matrix[a][b] = matrix[b][a] = 1;        }        for (i = 1; i <= k; i++)        {            cin >> c;            f(c, n);        }    }}

方法二:并查集

int fa[1005] = { 0 };//存储城市,下标是城市编号,单元格中存储城市公路通往的“源头”城市的编号int ans[1005] = { 0 };//下标是被入侵的城市的编号,单元格中存储该城市被入侵后需要修补的公路数struct point{    //城市结构体,xy表示有进有出    int x, y;    void read(){ scanf("%d%d", &x, &y); }};int getf(int x){    //路径压缩,    if (fa[x] == x)return x;    else        return fa[x] = getf(fa[x]);}void PAT1013A(){    int n, m, k;    scanf("%d%d%d", &n, &m, &k);    point *a = new point[m + 1];    for (int i = 0; i < m; ++i)a[i].read();    for (int i = 1; i <= n; ++i)    {        for (int j = 1; j <= n; ++j)fa[j] = j;//初始化        //合并开始        ans[i] = n - 2;//需要修补的公路数的最大值是城市数减二        for (int j = 0; j < m&&ans[i]>0; j++){            if (a[j].x == i || a[j].y == i)continue;            int fx = getf(a[j].x), fy = getf(a[j].y);            if (fx == fy)continue;            else fa[fx] = fy, ans[i]--;        }    }    int x = 0;    while (k--){        scanf("%d", &x);        printf("%d\n", max(ans[x], 0));    }}
0 0