POJ2230 Watchcow

原题链接

类欧拉回路,要求每条边被正反各经过一次,且从(1)出发并回到(1)
只需每次搜索该点的边时,将该点的边对应的邻接表头及时修改为下一条即可,因为邻接表恰好储存了正反方向的边,所以及时更新表头就能保证每条边被正反各经过一次。

#include<cstdio>
using namespace std;
const int N = 1e4 + 10;
const int M = 5e4 + 10;
int fi[N], di[M << 1], ne[M << 1], an[M << 1], l, k;
inline int re()
{
	int x = 0;
	char c = getchar();
	bool p = 0;
	for (; c < '0' || c > '9'; c = getchar())
		p |= c == '-';
	for (; c >= '0' && c <= '9'; c = getchar())
		x = x * 10 + c - '0';
	return p ? -x : x;
}
inline void add(int x, int y)
{
	di[++l] = y;
	ne[l] = fi[x];
	fi[x] = l;
}
void dfs(int x)
{
	int i;
	for (i = fi[x]; i; i = fi[x])
	{
		fi[x] = ne[i];
		dfs(di[i]);
	}
	an[++k] = x;
}
int main()
{
	int i, n, m, x, y;
	n = re();
	m = re();
	for (i = 1; i <= m; i++)
	{
		x = re();
		y = re();
		add(x, y);
		add(y, x);
	}
	dfs(1);
	for (i = k; i; i--)
		printf("%d
", an[i]);
	return 0;
}
原文地址:https://www.cnblogs.com/Iowa-Battleship/p/9627474.html