PAT (Advanced Level) Practice - 1013 Battle Over Cities(25 分)

简介: PAT (Advanced Level) Practice - 1013 Battle Over Cities(25 分)

题目链接:点击打开链接


题目大意:给出一张无向图,以及几条边。要求当去掉其中的一个顶点后为了使剩下的顶点可以连通需要增加多少条边。


解题思路:其实只要考虑去掉这个顶点后,剩余的顶点可以组成几个独立的区域,假设该区域数为t,则需要增加的边即为t-1。(即:求剩余的连通分量个数-1)。


AC 代码


/

#include<bits/stdc++.h>
#include<cmath>
#define mem(a,b) memset(a,b,sizeof a);
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1010;
int n,m,k;
int vis[maxn];
int g[maxn][maxn];
void dfs(int s)
{
    vis[s]=1;
    for(int i=1;i<=n;i++)
        if(!vis[i] && g[s][i]==1) dfs(i);
}
int main()
{
    int u,v;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            g[u][v]=g[v][u]=1;
        }
        while(k--)
        {
            scanf("%d",&u);
            mem(vis,0);
            vis[u]=1;
            int rs=0;
            for(int i=1;i<=n;i++)
            {
                if(!vis[i])
                {
                    dfs(i);
                    rs++;
                }
            }
            printf("%d\n",rs-1);
        }
    }
    return 0;
}
目录
相关文章
|
移动开发 C语言
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分)
98 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
134 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
121 0
|
索引
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
119 0
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
143 0
PAT (Advanced Level) Practice - 1076 Forwards on Weibo(30 分)
PAT (Advanced Level) Practice - 1076 Forwards on Weibo(30 分)
111 0
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
PAT (Advanced Level) Practice - 1016 Phone Bills(25 分)
115 0
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
PAT (Advanced Level) Practice - 1118 Birds in Forest(25 分)
112 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
121 0
PAT (Advanced Level) Practice - 1112 Stucked Keyboard(20 分)
PAT (Advanced Level) Practice - 1112 Stucked Keyboard(20 分)
133 0