bzoj4238: 电压

依题意:对电阻X,若可以选择使X不流过电流,当且仅当所有奇环都包含X且所有偶环都不包含X。

利用Dfs搜索树,如果点a搜到的边的端点b已经走过,说明点b为a的祖先,利用深度差可求得环上点的个数。


#include<iostream>
#include<cstdio>
#define N 100005
#define M 400005
using namespace std;
int n,m,l=1,Ans,tot[2],tmp;bool vis[N];
int fa[N],first[N],next[M],to[M],sum[N][2],dep[N];
inline int read()
{
	char c=getchar();int x=0;
	while(c<'0'||c>'9')c=getchar();
	while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
	return x;
}
inline void link(int x,int y)
{
	to[++l]=y;next[l]=first[x];first[x]=l;
	to[++l]=x;next[l]=first[y];first[y]=l;
}
void Dfs(int t)
{
	vis[t]=1;
	for(int i=first[t];i;i=next[i])
	if ( (i^1)!=fa[t] )
	{
		if (vis[to[i]])
		{
			if (dep[to[i]]>dep[t])continue;
			tmp=(dep[t]-dep[to[i]])&1;
			sum[t][tmp]++;sum[to[i]][tmp]--;tot[tmp]++;
		}
		else
		{
			fa[to[i]]=i;dep[to[i]]=dep[t]+1;
			Dfs(to[i]);
			sum[t][0]+=sum[to[i]][0];
			sum[t][1]+=sum[to[i]][1];
		}
	}
}
int main()
{
	n=read();m=read();
	for(int i=1;i<=m;i++)link(read(),read());
	for (int i=1;i<=n;i++)if(!vis[i]) Dfs(i);
	for (int i=1;i<=n;i++)if (sum[i][0]==tot[0]&&!sum[i][1]&&fa[i]) Ans++;
	if (tot[0]==1)Ans++;
	printf("%d\n",Ans);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值