#include<iostream>
#include<string >
using namespace std;
const int SIZE = 100;
int n, m, p, a[SIZE] [SIZE], count;
void colour (int x, int y)
{
Count++;
a[x][y] = 1;
if ((x > 1)&& (a[x-1][y] == 0))
colour( x - 1, y);
if ((y> 1)&& (a[x][y-1] == 0))
colour( x, y- 1);
if ((x < n)&& (a[x+1][y] == 0))
colour( x +1, y);
if ((y < m)&& (a[x][y+1] == 0))
colour( x, y+1);
}
int main( )
{
int i, j, x, y, ans;
memset(a, 0, sizeof(a));
cin >>n>>m>>p;
for(i =1 ; I <=p; i++) {
cin>>x>>y;
a[x][y] = 1;
}
ans = 0;
for (i =1; i <=n; i++)
for (j =1; j <=m;j++)
if (a[i][j] == 0)
{count = 0;
colour (i , j);
if (ans <count)
ans <count;
}
count<<ans<<endl;
return 0;
}输入:
6 5 9
1 4
2 3
2 4
3 2
4 1
4 3
4 5
5 4
6 4
输出:_________
相似题推荐
如下图所示,A到B是连通的。假设删除一条细的边的代价是1,删除一条粗的边的代价是2,要让A、B不连通,最小代价是_____(2分),最小代价的不同方案数是_______(3分)。(只要有一条删除的边不同,就是不同的方案)

如右图所示,共有13个格子。对任何一个格子进行一次操作,会使得它自己以及与它上下左右相邻的格子中的数字改变(由1变0,或由0变1)。现在要使得所有的格子中的数字都变为0,至少需要 次操作。

(最长路径)给定一个有向无环图,每条边长度为1,求图中的最长路径长度。(第五空2分,其余3分)
输入:第一行是结点数n(不超过100)和边数m,接下来m行,每行两个整数a,b,表示从结点a到结点b有一条有向边。结点标号从0到(n-1)。
输出:最长路径长度。
提示:先进行拓扑排序,然后按照拓扑序计算最长路径。
#include <iostream>
using namespace std;
int n, m, i, j, a, b, head, tail, ans;
int graph[100][100]; // 用邻接矩阵存储图
int degree[100]; // 记录每个结点的入度
int len[100]; // 记录以各结点为终点的最长路径长度
int queue[100]; // 存放拓扑排序结果
int main() {
cin >> n >> m;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
graph[i][j] = 0;
for (i = 0; i < n; i++)
degree[i] = 0;
for (i = 0; i < m; i++) {
cin >> a >> b;
graph[a][b] = 1;
(1) ;
}
tail = 0;
for (i = 0; i < n; i++)
if ( (2) ) {
queue[tail] = i;
tail++;
}
head = 0;
while (tail < n - 1) {
for (i = 0; i < n; i++)
if (graph[queue[head] ][i] == 1) {
(3) ;
if (degree[i] == 0) {
queue[tail] = i;
tail++;
}
}
(4) ;
}
ans = 0;
for (i = 0; i < n; i++) {
a = queue[i];
len[a] = 1;
for (j = 0; j < n; j++)
if (graph[j][a] == 1 && len[j] + 1 > len[a])
len[a] = len[j] + 1;
if ( (5) )
ans = len[a];
}
cout << ans << endl;
return 0;
}
设A和B是两个长为n的有序数组,现在需要将A和B合并成一个排好序的数组,请问任何以元素比较作为基本运算的归并算法最坏情况下至少要做( )次比较。
| A. n2 |
B. n logn |
| C. 2n |
D. 2n-1 |
