【题解】洛谷P3225 矿场搭建(割点 tarjan)

本文介绍了一种用于寻找图中的割点和割边的算法,并通过具体代码实现了基于Tarjan算法的割点和割边检测。文章还讨论了如何通过深度优先搜索(DFS)来确定图中割点和割边的数量,以及如何基于这些信息进一步计算特定类型的子图数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.luogu.org/blog/cjyyb/solution-p3225

思路还是比较好理解的,不过实现起来挺复杂。。。

虽然数据水 但还是注意初始化别搞错了。。我的Cut数组初始化为0时sizeof写的是cut,看了半天没找出来。。。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define ll long long
using namespace std;
const int maxn=610;
int dfn[maxn],low[maxn],color[maxn];
bool Cut[maxn];
ll head[maxn],nnext[maxn*4],to[maxn*4];
int tot;
ll num,cut,id,n,m,rs,root,ans1,ans2,group,Case;
void add(ll x,ll y)
{
	tot++;
	nnext[tot]=head[x];
	head[x]=tot;
	to[tot]=y;
}
void ycl()
{
	memset(Cut,0,sizeof(Cut));
	memset(head,0,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(color,0,sizeof(color));
	ans1=group=n=tot=id=0;
	ans2=1;
}
void tarjan(int x,int fa)
{
	dfn[x]=low[x]=++id;
	for(int i=head[x];i;i=nnext[i])
	{
		int y=to[i];
		if(dfn[y]==0)
		{
			tarjan(y,x);
			if(y==fa) continue;
			low[x]=min(low[x],low[y]);
			if(low[y]>=dfn[x])
			{
				if(x!=root) Cut[x]=true;
				else rs++;
			}
		}
		else
		{
			low[x]=min(low[x],low[y]);
		}
	}
}
void dfs(int x)
{
	color[x]=group;
	num++;
	for(int i=head[x];i;i=nnext[i])
	{
		int y=to[i];
		if(Cut[y]&&color[y]!=group)
		{
			cut++;
			color[y]=group;
		}
		if(!color[y]) dfs(y);
	}
}
int main()
{
	Case=1;
	ll x,y;
	while(cin>>m&&m)
	{
		
		ycl();
		for(int i=1;i<=m;i++)
		{
			
			cin>>x>>y;
			add(x,y);
			add(y,x);
			n=max(n,x);
			n=max(n,y);
		}
		for(int i=1;i<=n;i++)
		{
			if(!dfn[i])
			{
				root=i;
				rs=0;
				tarjan(i,i);
				if(rs>=2) Cut[i]=true;
			}
		}
		for(int i=1;i<=n;i++)
		{
			if(!color[i]&&!Cut[i])
			{
				++group;
				num=cut=0;
				dfs(i);
				
				if(cut==0)
				{
					ans1+=2;
					ans2*=(num-1)*num/2; 
				}
				if(cut==1)
				{
					ans1+=1;
					ans2*=num;
				}
				if(cut>=2)
				{
					;
				}
			}
		}
		cout<<"Case "<<Case++<<": "<<ans1<<" "<<ans2<<endl;
	}
	return 0;
}

 

洛谷 P2516 题目涉及的是一个与字符串处理和动态规划相关的挑战。题目要求对一个由数字组成的字符串进行分,使得每个分出的数字子串能构成一个递增序列,并且每部分对应的数值都比前一部分大。以下是解题思路及实现方法。 ### 问题解析 - 输入是一个长度不超过 **40** 的纯数字字符串。 - 目标是将该字符串分成若干个非空数字子串,这些子串所表示的数值形成一个严格递增序列。 - 每个分出来的子串必须满足其数值大于前一个子串的数值。 - 最终输出所有可能的合法分方案的数量。 ### 解法概述 此问题可以通过 **深度优先搜索 (DFS)** 或 **回溯法** 来解决: - 使用递归的方式尝试在每一个位置进行分。 - 对于每一次分,提取当前子串并转换为整数,然后判断它是否大于上一次分得到的值。 - 如果符合递增条件,则继续递归处理剩余的字符串部分。 - 当遍历完整个字符串并且满足所有分条件时,计数器加一。 ### 实现细节 - 因为输入字符串长度最大为 **40**,所以需要考虑大数问题(超过 `int` 范围),建议使用 `long long` 类型或 Python 中的 `int` 类型自动处理大整数。 - 在分过程中,确保没有前导零(除非子串长度为 1)。 - 递归终止条件是字符串已经被完全分。 ### 示例代码 (C++) ```cpp #include <iostream> #include <string> using namespace std; int count = 0; // 将字符串 s 的 [start, end) 子串转换为整数 long long to_number(const string &s, int start, int end) { long long num = 0; for (int i = start; i < end; ++i) { num = num * 10 + (s[i] - '0'); } return num; } // DFS 函数:从 pos 位置开始分,last_num 表示上一次分出的数 void dfs(const string &s, int pos, long long last_num) { if (pos == s.size()) { count++; return; } for (int i = pos + 1; i <= s.size(); ++i) { // 剪枝:如果子串长度大于1且以0开头,则跳过 if (i - pos > 1 && s[pos] == '0') break; long long current = to_number(s, pos, i); if (current > last_num) { dfs(s, i, current); } } } int main() { string s; cin >> s; dfs(s, 0, -1); // 初始时 last_num 设置为 -1,保证第一个数可以任意选择 cout << count << endl; return 0; } ``` ### 算法复杂度分析 - 时间复杂度:最坏情况下为指数级 $O(2^n)$,因为每次递归都有多个分支。 - 空间复杂度:主要取决于递归栈深度,最多为 $O(n)$。 这种方法适用于题目给定的数据规模(字符串长度 ≤ 40),通过适当的剪枝优化,可以在合理时间内完成计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值