【PAT Advanced Level】1013. Battle Over Cities (25)
2013-10-31 22:15
330 查看
这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图)。
在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问。一共进行了多少次这样的搜索,
就是我们要求的独立区域的个数。
在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问。一共进行了多少次这样的搜索,
就是我们要求的独立区域的个数。
#include <iostream> #include <fstream> #include <memory.h> using namespace std; const int maxNum = 1001; bool visited[maxNum]; int edge[maxNum][maxNum]; int N, M, K; void DFS(int begin) { for(int i = 1; i <= N; i++) { if(edge[begin][i] && !visited[i]) { visited[i] = true; DFS(i); } } } int main() { //fstream cin("a.txt"); cin>>N>>M>>K; for(int i = 1; i <= M; i++) { int tmp1, tmp2; cin>>tmp1>>tmp2; edge[tmp1][tmp2] = edge[tmp2][tmp1] = 1; } memset(visited, 0, maxNum); int result = 0; for(int i = 0; i < K; i++) { int k; cin>>k; visited[k] = true; for(int j = 1; j <= N; j++) { if(!visited[j]) { DFS(j); result++; } } cout<<result - 1<<endl; memset(visited, 0, maxNum); result = 0; } }
相关文章推荐
- PAT (Advanced Level) Practise 1012. The Best Rank (25)
- PAT (Advanced Level) 1126. Eulerian Path (25)
- PAT (Advanced Level) Practise 1009 Product of Polynomials (25)
- 【PAT】【Advanced Level】1037. Magic Coupon (25)
- 【PAT】【Advanced Level】1059. Prime Factors (25)
- PAT (Advanced Level) Practise 1048 Find Coins (25)
- 1085. Perfect Sequence (25)【二分查找】——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1071 Speech Patterns (25)
- PAT (Advanced Level) Practise 1078 Hashing (25)
- PAT (Advanced Level) Practise 1089 Insert or Merge (25)
- PAT (Advanced Level) 1012. The Best Rank (25) 最佳排名,结构体排序,哈希查询
- PAT (Advanced Level) Practise 1109. Group Photo (25) 结构体排序
- 浙大 PAT Advanced level 1024. Palindromic Number (25)
- PAT(Advanced Level)1006. Sign In and Sign Out (25)
- PAT (Advanced Level)1013. Battle Over Cities (25) DFS
- 浙大PAT (Advanced Level) Practise 1020 Tree Traversals (25)
- PAT (Advanced Level) Practise 1126 Eulerian Path (25)
- PAT (Advanced Level) 1089. Insert or Merge (25)
- 【PAT Advanced Level】1010. Radix (25)
- 【c++】PAT (Advanced Level)1010. Radix (25)