(intermediate) DFS (高精度+找环) UVA 1361 Cactus

Cactus is a connected undirected graph in which every edge lies on at most one simple cycle. Intuitively cactus is a generalization of a tree where some cycles are allowed. Your task first is to verify if the given graph is a cactus or not. Important difference between a cactus and a tree is that a cactus can have a number of spanning subgraphs that are also cactuses. The number of such subgraphs (including the graph itself ) determines cactusness of a graph (this number is one for a cactus that is just a tree). The cactusness of a graph that is not a cactus is considered to be zero.

\epsfbox{p3514.eps}

The first graph on the picture is a cactus with cactusness 35. The second graph is not a cactus because edge (2, 3) lies on two cycles. The third graph is not a cactus because it is not connected.

Input 

The input will contain several test cases, each of them as described below. Consecutive test cases are separated by a single blank line.


The first line of the input contains two integer numbers n and m (1$ \le$n$ \le$20000, 0$ \le$m$ \le$1000) . Here n is the number of vertices in the graph. Vertices are numbered from 1 to n . Edges of the graph are represented by a set of edge-distinct paths, where m is the number of such paths.

Each of the following m lines contains a path in the graph. A path starts with an integer number ki (2$ \le$ki$ \le$1000) followed by ki integers from 1 to n . These ki integers represent vertices of a path. Path can go to the same vertex multiple times, but every edge is traversed exactly once in the whole input file. There are no multiedges in the graph (there is at most one edge between any two vertices).

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.


Write to the output file a single integer number - the cactusness of the given graph. Note that cactusness can be quite a large number.

Sample Input 

14 3 
9 1 2 3 4 5 6 7 8 3 
7 2 9 10 11 12 13 10 
2 2 14

10 2 
7 1 2 3 4 5 6 1 
6 3 7 8 9 10 2

5 1 
4 1 2 3 4

Sample Output 

35

0

0

题意:首先仙人掌图就是任意一条边都最多在一个简单环里面,然后求一个仙人掌图里面有多少个生成子图也是仙人掌图,注意生成子图的图中包含原图的所有的顶点哦,。
思路:其实理解了题意,怎么做是很好想的,对于一个仙人掌图里面,能删掉的边就只有环里面的边,如果他是一个n边形,那么有n+1种可能性,删掉任意一条边,或者不删除,那么找出所有的简单环,乘一下就行了。这道题就麻烦的地方就是一条边只能位于一个环。感觉好难判断啊。。。我用一个set装下所有环的边,看会不会重复,我知道会很慢,但是过了就行了,唉。
这题好麻烦,又要高精度,又要栈模拟递归,不然溢出。。。发现递归只能递归到5000层左右啊。。。是这么小的吗?

代码:
#include<iostream>
#include<vector>
#include<stack>
#include<cstring>
#include<string.h>
#include<cstdio>
#include<set>
using namespace std;
#define mod 10000
const int maxn = 20000+5;
vector<int> G[maxn];
bool vis[maxn];
int step[maxn];
int pos[maxn];
//bool g[maxn][maxn];
int S[maxn] , c;
int n , m , k , u , v;

struct Edge
{
	int u , v;
	Edge(int uu = -1 ,int vv = -1) : u(uu) , v(vv) { }
}tmp;

inline bool operator < (const Edge & e1 , const Edge & e2)
{
	if (e1.u==e2.u) return e1.v < e2.v;
	return e1.u < e2.u;
}

inline bool operator == (const Edge & e1 , const Edge & e2)
{
	return e1.u==e2.u && e1.v==e2.v;
}

class Bignum
{
private:
	int val[5000];
	int sz;
public:
	Bignum() { memset(val,0,sizeof(val)); sz = 0; }
	Bignum(int x) {
		memset(val,0,sizeof(val));
		sz = 0;
		while (x) { val[sz++] = x % mod ; x /= mod; }
	}
	Bignum(const Bignum & bn)
	{
		memset(val,0,sizeof(val));
		sz = bn.sz;
		for (int i = 0 ; i < sz ; ++i) val[i] = bn.val[i];
	}

	Bignum & operator = (const Bignum & bn)
	{
		memset(val,0,sizeof(val));
		sz = bn.sz;
		for (int i = 0 ; i < sz ; ++i) val[i] = bn.val[i];
		return *this;
	}
	Bignum operator * (int x)
	{
		Bignum ret;
		Bignum tmp(x);
		for (int i = 0 ; i < sz ; ++i)
		{
			for (int j = 0 ; j < tmp.sz ; ++j)
			{
				ret.val[i+j] += tmp.val[j]*val[i];
				ret.val[i+j+1] += ret.val[i+j] / mod;
				ret.val[i+j] %= mod;
			}
		}
		ret.sz = tmp.sz + sz;
		while (ret.val[ret.sz]) { 
			ret.val[ret.sz+1] += ret.val[ret.sz] / mod;
			ret.val[ret.sz] %= mod;
			++ret.sz;
		}
		while (ret.sz > 0 && ret.val[ret.sz-1]==0) --ret.sz;
		return ret;
	}

	void print()
	{
		printf("%d",val[--sz]);
		--sz;
		while (sz >= 0) {
			if (val[sz] < 10) printf("000%d",val[sz]);
			else if (val[sz] < 100) printf("00%d",val[sz]);
			else if (val[sz] < 1000) printf("0%d",val[sz]);
			else printf("%d",val[sz]);
			--sz;
		}
		cout << endl;
	}
}ans;

set<Edge> edge;
stack<Edge> DFS;
void init()
{
	c = 0;
	ans = 1;
	while (DFS.size()) DFS.pop();
	for (int i = 1 ; i <= n ; ++i) { vis[i] = false; G[i].clear(); } 
	edge.clear();
}

void input()
{
	while (m--)
	{
		scanf("%d%d",&k,&u);
		while (--k)
		{
			scanf("%d",&v);
			G[u].push_back(v);
			G[v].push_back(u);
			pos[u] = pos[v] = step[u] = step[v] = -1;
			//g[u][v] = g[v][u] = false;
			u = v;
		}
	}
}

bool dfs(int rt)
{
	DFS.push(Edge(-1,rt));
	step[rt] = 0;
	int edge_cnt = 0;
	while (DFS.size())
	{
		tmp = DFS.top(); 
		u = tmp.u , v = tmp.v;
		if (u==-1 && vis[v]) break;
		if (!vis[v])
		{
			if (u > 0) step[v] = step[u]+1;
			vis[v] = true;
			S[c++] = v;
			bool isleaf = true;
			for (int i = 0 ; i < G[v].size() ; ++i)
			{
				int y = G[v][i];
				if (step[y]==-1 || abs(step[y]-step[v])!=1) {
					isleaf = false;
					if (pos[v]==-1) pos[v] = DFS.size();
					DFS.push(Edge(v,y));
				}
			}
			if (isleaf) { --c; DFS.pop(); }
		} else {
			if (step[u] <= step[v]) {
				DFS.pop();
				while (c > 0 && pos[S[c-1]] > DFS.size()) --c;
				continue;
			}
			ans = ans*(step[u]-step[v]+2);
			int x = v , y;
			for (int i = c-1 ; i >= 0 ; --i) {
				y = S[i];
				//	if (g[x][y]) return false;
				//g[x][y] = g[y][x] = true;
				edge.insert(Edge(min(x,y),max(x,y)));
				++edge_cnt;
				if (edge.size()!=edge_cnt) return false;
				x = y;
				if (S[i]==v) break;
			}
			DFS.pop();
			while (c > 0 && pos[S[c-1]] > DFS.size()) --c;
		}
	}
	for (int i = 1;  i <= n ; ++i)
		if (!vis[i]) return false;
	return true;
}

void solve()
{
	if (!dfs(1)) cout << 0 << endl;
	else ans.print();
}

int main()
{
	//freopen("input.in","r",stdin);
	while (scanf("%d%d",&n,&m)==2)
	{
		init();
		input();
		solve();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值