题意:求最大节点的连通块的节点数量
用一个实际参数记录当前连通块里的总数,然后遍历完所有的连通块,找到节点最多的
#include<cstdio> #include<cmath> #include<cstring> #include<queue> #include<vector> #include<functional> #include<algorithm> using namespace std; typedef long long LL; typedef pair<int, int> PII; const int MX = 100 + 5; const int INF = 0x3f3f3f3f; int m, n; bool S[MX][MX]; int dist[][2] = {{ -1, 0}, {1, 0}, {0, 1}, {0, -1}}; void DFS(int x, int y, int &sum) { S[x][y] = 0; sum++; for(int k = 0; k < 4; k++) { int nx = x + dist[k][0]; int ny = y + dist[k][1]; if(nx < 1 || nx > m || ny < 1 || ny > n) continue; if(!S[nx][ny]) continue; DFS(nx, ny, sum); } } int main() { int k; while(~scanf("%d%d%d", &m, &n, &k)) { memset(S, 0, sizeof(S)); for(int i = 1; i <= k; i++) { int x, y; scanf("%d%d", &x, &y); S[x][y] = 1; } int ans = 0, sum; for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) { sum = 0; if(S[i][j]) { DFS(i, j, sum); ans = max(ans, sum); } } } printf("%d\n", ans); } return 0; }