hdoj 3844 Mining Your Own Business 【在无向图选择尽量少的点涂黑,使得任意删除一个点后每个BCC里面至少有一个点涂黑】

Mining Your Own Business

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1701    Accepted Submission(s): 271


Problem Description
John Digger is the owner of a large illudium phosdex mine. The mine is made up of a series of tunnels that meet at various large junctions. Unlike some owners, Digger actually cares about the welfare of his workers and has a concern about the layout of the mine. Specifically, he worries that there may a junction which, in case of collapse, will cut off workers in one section of the mine from other workers (illudium phosdex, as you know, is highly unstable). To counter this, he wants to install special escape shafts from the junctions to the surface. He could install one escape shaft at each junction, but Digger doesn’t care about his workers that much. Instead, he wants to install the minimum number of escape shafts so that if any of the junctions collapses, all the workers who survive the junction collapse will have a path to the surface.

Write a program to calculate the minimum number of escape shafts and the total number of ways in which this minimum number of escape shafts can be installed.
 

Input
The input consists of several test cases. The first line of each case contains a positive integer N (N <= 5×10^4) indicating the number of mine tunnels. Following this are N lines each containing two distinct integers s and t, where s and t are junction numbers. Junctions are numbered consecutively starting at 1. Each pair of junctions is joined by at most a single tunnel. Each set of mine tunnels forms one connected unit (that is, you can get from any one junction to any other).

The last test case is followed by a line containing a single zero.
 

Output
For each test case, display its case number followed by the minimum number of escape shafts needed for the system of mine tunnels and the total number of ways these escape shafts can be installed. You may assume that the result fits in a signed 64-bit integer.

Follow the format of the sample output.
 

Sample Input
  
  
9 1 3 4 1 3 5 1 2 2 6 1 5 6 3 1 6 3 2 6 1 2 1 3 2 4 2 5 3 6 3 7 0
 

Sample Output
  
  
Case 1: 2 4 Case 2: 4 1
 


RE到死。。。。

 

题意就不说了吧

 

思路

转化模型:在一个无向图上选择尽量少的点涂黑,使得任意删除一个点后,每个BCC至少有一个点涂黑。

则有:当一个BCC只有一个割顶时才需要涂色,而且是任意选择一个非割顶涂色。特别地,当图是连通的,最少涂色两个点,且方案数为V * ( V - 1 ) / 2 其中V为点数。

 

AC代码:注意预处理栈大小 用C++提交

本题有10W个点。。。

 

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#define LL long long 
#define MAXN 100000+100
#define MAXM 200000+100
#define INF 10000000
using namespace std;
struct Edge
{
	int from, to;
};
stack<Edge> S;//存储BCC里面的边 
vector<int> G[MAXN], bcc[MAXN];//G存储图  bcc存储BCC里所有点 
int low[MAXN], dfn[MAXN];
int dfs_clock;
bool iscut[MAXN];//标记割点
int bccno[MAXN], bcc_cnt;//bccno[i]表示i属于哪个BCC    bcc_cnt是BCC计数器 
int k = 1;
int right;//最大连接点编号 
void init()
{
	for(int i = 1; i < MAXN; i++)
	G[i].clear();
}
void getMap(int M)
{
	int s, t;
	right = 0;
	while(M--)
	{
		scanf("%d%d", &s, &t);
		G[s].push_back(t);
		G[t].push_back(s);
		right = max(max(s, t), right);
	}
}
void tarjan(int u, int fa)
{
	low[u] = dfn[u] = ++dfs_clock;
	int child = 0;//计算子节点数目
	for(int i = 0; i < G[u].size(); i++)
	{
		int v = G[u][i];
		Edge E = {u, v};
		if(!dfn[v])
		{
			S.push(E);
			child++;
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
			if(low[v] >= dfn[u])
			{
				iscut[u] = true;//割点 
				bcc_cnt++;
				bcc[bcc_cnt].clear();
				for(;;)
				{ 
					Edge x = S.top();
					S.pop();
					if(bccno[x.from] != bcc_cnt)
					{
						bcc[bcc_cnt].push_back(x.from);
						bccno[x.from] = bcc_cnt;
					}
					if(bccno[x.to] != bcc_cnt)
					{
						bcc[bcc_cnt].push_back(x.to);
						bccno[x.to] = bcc_cnt;
					}
					if(x.from == u && x.to == v)  break;
				}
			}
		}
		else if(dfn[v] < dfn[u] && v != fa)
		{
			S.push(E);
			low[u] = min(low[u], dfn[v]);
		}
	}
	if(fa < 0 && child == 1) iscut[u] = false; 
}
void find_cut(int l, int r)
{
	memset(bccno, 0, sizeof(bccno));
	memset(iscut, false, sizeof(iscut));
	memset(low, 0, sizeof(low));
	memset(dfn, 0, sizeof(dfn));
	dfs_clock = bcc_cnt = 0;
	for(int i = l; i <= r; i++)
	if(!dfn[i])  tarjan(i, -1);
}
void solve()
{
	LL ans = 0, sum = 1;//ans 表示最少需要的太平井数目  sum表示方案数
	LL cut_cnt;//统计每个BCC里面的割点数目 
	for(int i = 1; i <= bcc_cnt; i++)
	{
		cut_cnt = 0; 
		for(int j = 0; j < bcc[i].size(); j++)
		if(iscut[bcc[i][j]]) cut_cnt++;
		if(cut_cnt == 1)
		{
			ans++; 
			sum *= (LL)bcc[i].size() - cut_cnt; 
		}
	} 
	if(bcc_cnt == 1)//图是连通的
	{
		LL t = bcc[1].size();//坑死啊 100000个点 还需要用LL 
		ans = 2;//至少需要两个太平井 
		sum = (t - 1) * t / 2;
	} 
	printf("Case %d: %lld %lld\n", k++, ans, sum);
}
int main()
{
	int m;
	while(scanf("%d", &m), m)
	{
		init();
		getMap(m);
		find_cut(1, right);//连接点是从1开始的 
		solve();
	}
	return 0;
} 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值