POJ 2375 解题报告

这道题是先求强连通子图,然后压缩,选出度为0和入度为0的数目里面大的那个。思路是强连通子图内部的不用关心(已经两两互通了),强连通子图之间,在入度为0和出度为0的component之间连一条线(从出度为0到入度为0)。

类似的题我做过。也是USACO上的。但是。。。先是求neighbors的时候所有点共用一个(4元素的)数组,导致递归的时候覆盖了原来的邻居列表,导致错误。这个错误不是很容易发现。我最后还是找到了USACO的测试数据才发现的。

测试数据链接在这里:http://contest.usaco.org/DEC04_7.htm (曾经能打开)。

然后是RE。看了discuss上面说tarjan递归的层数可能太多了。但是我的程序C++也无法通过。于是想到可能是递归调用的时候传的参数太多了(为了避免全局变量,我之前程序所有变量都是作为函数参数传递的)。于是我走了极端,全部用全局函数,这样函数递归调用的时候不用压栈出栈,果然accept了。

thestoryofsnow2375Accepted13256K266MSC++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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值