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 分)
96 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
120 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
133 0
|
索引
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
PAT (Advanced Level) Practice - 1056 Mice and Rice(25 分)
118 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 - 1062 Talent and Virtue(25 分)
PAT (Advanced Level) Practice - 1062 Talent and Virtue(25 分)
142 0
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
PAT (Advanced Level) Practice - 1129 Recommendation System(25 分)
106 0
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
PAT (Advanced Level) Practice - 1122 Hamiltonian Cycle(25 分)
119 0
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
PAT (Advanced Level) Practice - 1087 All Roads Lead to Rome(30 分)
103 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
140 0