赞
踩
收获:
1.判断是否连通,用并查集
2.有多少层数 用bfs
代码:
- #include <iostream>
- #include <queue>
- #include <cstring>
- using namespace std;
-
- #define N 10005
- int map[N][N];
- int visited[N] = {0};
- int pre[N];
- int n, m;
-
- int find(int x) {
- if(x != pre[x])
- return pre[x] = find(pre[x]);
- return x;
- }
-
- void Union(int x, int y) {
- int a = find(x);
- int b = find(y);
- if(a != b)
- pre[a] = b;
- }
-
- int BFS(int s) {
- memset(visited, 0, sizeof(visited));
- visited[s] = 1;
- queue<int> q;
- q.push(s);
- int last = s;
- int tail;
- int level = 0;
- int cnt = 0;
- int sum = 0;
- while(!q.empty()) {
- int l = q.front();
- q.pop();
- for(int i = 1; i <= n; i++)
- if(!visited[i] && map[l][i]) {
- visited[i] = 1;
- q.push(i);
- c
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。