-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1012.cpp
More file actions
48 lines (42 loc) · 937 Bytes
/
1012.cpp
File metadata and controls
48 lines (42 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <cstdio>
#include <string.h>
using namespace std;
int n,m,k;
int adj[51][51];
bool v[51][51];
int dx[4]={-1, 0, 1, 0};
int dy[4]={0, 1, 0, -1};
void dfs(int y, int x){
v[y][x]=true;
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=0 && nx<m && ny>=0 && ny<n && adj[ny][nx]==1 && !v[ny][nx]){
dfs(ny, nx);
}
}
}
int main(){
int t;
scanf("%d", &t);
while(t--){
memset(adj,0,sizeof(adj));
memset(v,0,sizeof(v));
scanf("%d%d%d", &m, &n, &k);
int x, y;
for(int i=0;i<k;i++){
scanf("%d%d", &x, &y);
adj[y][x]=1;
}
int cnt=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!v[i][j] && adj[i][j]){
dfs(i, j);
cnt++;
}
}
}
printf("%d\n", cnt);
}
}