POJ 2793 Cactus

题意:

给你一幅无向图  计算它有多少生成子图是仙人掌  如果它本身不是仙人掌输出0

思路:

无向图的仙人掌是一个连通图且一条边最多在一个环上

对于这道题  需要区分“生成子图”和“导出子图”的概念

生成子图:包含G的所有顶点V和其中一些边的子图

导出子图:选择G中一些点组成集合V‘,将E中所有两端点在V‘中的边全部找出形成的子图叫点导出子图;选择G中一些边组成集合E‘,将V中所有与E‘中的边有关系的点全部找出形成的子图叫边导出子图。

那么这道题就是说你要扔掉一些边  使图还是仙人掌  问方案数

易知扔掉的边必定是环上的边  而且由于原图是仙人掌  所以每个环只能扔1条边或不扔

那么总方案数其实就是所有的环中的边数加一后的乘积  由于最后数字很大所以要用高精度

PS:

其实我不会java…  所以代码很丑…  有能改进的地方还望各位指出  感激不尽

代码:

import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
	class edge {
		int v, next;
		boolean flag;

		edge(int v, int next) {
			this.v = v;
			this.next = next;
			this.flag = false;
		}
	}

	edge[] ed = new edge[2000001];
	int[] head = new int[20001];
	int[] dfn = new int[20001];
	int[] low = new int[20001];
	int[] st = new int[2000001];
	int tot, idx, top;
	BigInteger res;

	public void tarjan(int u, int fa) {
		int i, j, v, f = 0;
		dfn[u] = low[u] = ++idx;
		for (i = head[u]; i != -1; i = ed[i].next) {
			v = ed[i].v;
			if (ed[i].flag)
				continue;
			ed[i].flag = ed[i ^ 1].flag = true;
			st[++top] = i;
			if (dfn[v] == -1) {
				tarjan(v, u);
				low[u] = min(low[u], low[v]);
				if (dfn[u] <= low[v]) {
					int num = 0;
					do {
						j = st[top--];
						num++;
					} while (i != j);
					if (num > 1)
						num++;
					res = res.multiply(new BigInteger(num + ""));
				}
			} else
				low[u] = min(low[u], dfn[v]);
			if (low[v] < dfn[u])
				f++;
		}
		if (f > 1)
			res = BigInteger.ZERO;
	}

	public static int min(int i, int j) {
		if (i < j)
			return i;
		return j;
	}

	public void solve(int n) {
		idx = top = 0;
		res = BigInteger.ONE;
		tarjan(1, 1);
		for (int i = 1; i <= n; i++) {
			if (dfn[i] == -1) {
				res = BigInteger.ZERO;
				break;
			}
		}
	}

	public void run() {
		Scanner cin = new Scanner(System.in);
		int n, m, i, j, k, u, v;
		while (cin.hasNext()) {
			n = cin.nextInt();
			m = cin.nextInt();
			tot = 0;
			for (i = 1; i <= n; i++) {
				dfn[i] = -1;
				head[i] = -1;
			}
			for (i = 1; i <= m; i++) {
				k = cin.nextInt();
				u = cin.nextInt();
				for (j = 2; j <= k; j++) {
					v = cin.nextInt();
					ed[tot] = new edge(v, head[u]);
					head[u] = tot++;
					ed[tot] = new edge(u, head[v]);
					head[v] = tot++;
					u = v;
				}
			}
			solve(n);
			System.out.println(res);
		}
		cin.close();
	}

	public static void main(String[] args) {
		new Main().run();
	}
}
时间: 2024-08-28 18:58:53

POJ 2793 Cactus的相关文章

bzoj 1023 [SHOI2008]cactus仙人掌图 ( poj 3567 Cactus Reloaded )——仙人掌直径模板

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1023 http://poj.org/problem?id=3567 因为lyd在讲课,所以有了lyd的模板.感觉人家写得好好呀!于是学习(抄)了一下.可以记一记. 反正各种优美.那个dp断环成链的地方那么流畅自然!tarjan里的那些 if 条件那么美! 不过十分不明白为什么边要开成4倍的.开成2倍的真的会RE.怎么分析仙人掌的边数? #include<iostream> #includ

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

POJ - 3186 Treats for the Cows (区间DP)

题目链接:http://poj.org/problem?id=3186 题意:给定一组序列,取n次,每次可以取序列最前面的数或最后面的数,第n次出来就乘n,然后求和的最大值. 题解:用dp[i][j]表示i~j区间和的最大值,然后根据这个状态可以从删前和删后转移过来,推出状态转移方程: dp[i][j]=max(dp[i+1][j]+value[i]*k,dp[i][j-1]+value[j]*k) 1 #include <iostream> 2 #include <algorithm&

POJ 2533 - Longest Ordered Subsequence(最长上升子序列) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ..., aN) be any sequence (ai1, ai2, ..., aiK)

POJ——T2271 Guardian of Decency

http://poj.org/problem?id=2771 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5932   Accepted: 2463 Description Frank N. Stein is a very conservative high-school teacher. He wants to take some of his students on an excursion, but he is

POJ——T2446 Chessboard

http://poj.org/problem?id=2446 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18560   Accepted: 5857 Description Alice and Bob often play games on chessboard. One day, Alice draws a board with size M * N. She wants Bob to use a lot of c

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+

POJ 1385 计算几何 多边形重心

链接: http://poj.org/problem?id=1385 题意: 给你一个多边形,求它的重心 题解: 模板题,但是不知道为啥我的结果输出的确是-0.00 -0.00 所以我又写了个 if (ans.x == 0) ans.x = 0 感觉好傻逼 代码: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <stack> 6

POJ 1741 Tree(树的点分治,入门题)

Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21357   Accepted: 7006 Description Give a tree with n vertices,each edge has a length(positive integer less than 1001).Define dist(u,v)=The min distance between node u and v.Give an in