这道题是先求强连通子图,然后压缩,选出度为0和入度为0的数目里面大的那个。思路是强连通子图内部的不用关心(已经两两互通了),强连通子图之间,在入度为0和出度为0的component之间连一条线(从出度为0到入度为0)。
类似的题我做过。也是USACO上的。但是。。。先是求neighbors的时候所有点共用一个(4元素的)数组,导致递归的时候覆盖了原来的邻居列表,导致错误。这个错误不是很容易发现。我最后还是找到了USACO的测试数据才发现的。
测试数据链接在这里:http://contest.usaco.org/DEC04_7.htm (曾经能打开)。
然后是RE。看了discuss上面说tarjan递归的层数可能太多了。但是我的程序C++也无法通过。于是想到可能是递归调用的时候传的参数太多了(为了避免全局变量,我之前程序所有变量都是作为函数参数传递的)。于是我走了极端,全部用全局函数,这样函数递归调用的时候不用压栈出栈,果然accept了。
thestoryofsnow | 2375 | Accepted | 13256K | 266MS | C++ | 3509B |
/*
ID: thestor1
LANG: C++
TASK: poj2375
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cassert>
using namespace std;
const int MAXL = 500 + 10;
const int MAXW = 500 + 10;
int heights[MAXL][MAXW];
int discovery[MAXL][MAXW], lowlink[MAXL][MAXW];
int stack[MAXL * MAXW][2];
bool onStack[MAXL][MAXW];
int sccs[MAXL][MAXW];
int directions[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int neighbors[4][2];
int W, L;
int t = 0, top = 0;
int nscc = 0;
void getNeighbors(int l, int w, int &nn)
{
nn = 0;
for (int i = 0; i < 4; ++i)
{
int nl = l + directions[i][0], nw = w + directions[i][1];
if (nl >= 0 && nl < L && nw >= 0 && nw < W)
{
neighbors[nn][0] = nl;
neighbors[nn][1] = nw;
nn++;
}
}
}
void strongconnect(int l, int w)
{
// set timestamp
discovery[l][w] = t;
lowlink[l][w] = t;
t++;
stack[top][0] = l;
stack[top][1] = w;
top++;
onStack[l][w] = true;
// int nn = 0;
// int neighbors[4][2];
// getNeighbors(l, w, neighbors, nn, L, W);
for (int d = 0; d < 4; ++d)
{
int nl = l + directions[d][0], nw = w + directions[d][1];
if (nl >= 0 && nl < L && nw >= 0 && nw < W)
{
if (heights[l][w] >= heights[nl][nw])
{
if (discovery[nl][nw] < 0)
{
strongconnect(nl, nw);
lowlink[l][w] = min(lowlink[l][w], lowlink[nl][nw]);
}
else if (onStack[nl][nw])
{
lowlink[l][w] = min(lowlink[l][w], discovery[nl][nw]);
}
}
}
}
if (lowlink[l][w] == discovery[l][w])
{
while (top > 0)
{
int nl = stack[top - 1][0];
int nw = stack[top - 1][1];
top--;
onStack[nl][nw] = false;
sccs[nl][nw] = nscc;
if (nl == l && nw == w)
{
break;
}
}
nscc++;
}
}
void tarjan()
{
nscc = 0;
for (int l = 0; l < L; ++l)
{
for (int w = 0; w < W; ++w)
{
if (discovery[l][w] < 0)
{
strongconnect(l, w);
}
}
}
}
int main()
{
scanf("%d%d", &W, &L);
for (int l = 0; l < L; ++l)
{
for (int w = 0; w < W; ++w)
{
scanf("%d", &heights[l][w]);
}
}
// printf("heights[%d][%d]\n", L, W);
// for (int l = 0; l < L; ++l)
// {
// for (int w = 0; w < W; ++w)
// {
// printf("%d ", heights[l][w]);
// }
// printf("\n");
// }
// initialize discovery[][] with negative value
for (int l = 0; l < L; ++l)
{
for (int w = 0; w < W; ++w)
{
discovery[l][w] = -1;
onStack[l][w] = false;
}
}
tarjan();
// printf("nscc: %d\n", nscc);
// if the whole graph is strongly connected
if (nscc == 1)
{
printf("0\n");
}
else
{
int nn = 0;
std::vector<int> in(nscc, 0), out(nscc, 0);
for (int l = 0; l < L; ++l)
{
for (int w = 0; w < W; ++w)
{
nn = 0;
getNeighbors(l, w, nn);
for (int i = 0; i < nn; ++i)
{
int nl = neighbors[i][0];
int nw = neighbors[i][1];
if (sccs[l][w] != sccs[nl][nw] && heights[l][w] >= heights[nl][nw])
{
out[sccs[l][w]]++;
in[sccs[nl][nw]]++;
}
}
}
}
// printf("sccs(%d):\n", nscc);
// for (int l = 0; l < L; ++l)
// {
// for (int w = 0; w < W; ++w)
// {
// printf("%d ", sccs[l][w]);
// }
// printf("\n");
// }
int zeroIns = 0, zeroOuts = 0;
for (int i = 0; i < nscc; ++i)
{
if (in[i] == 0)
{
zeroIns++;
}
if (out[i] == 0)
{
zeroOuts++;
}
}
printf("%d\n", max(zeroIns, zeroOuts));
}
return 0;
}