VIJOS-P1626 桐桐的糖果计划

VIJOS 1325

筱柒Littleseven
QQ:3534947775

题目链接

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;

const int maxn = 10010;
const int maxm = 100010;

int n,m,ans,ans2;
int tot,head[maxn],to[maxm],nex[maxm];
int top,tar_st[maxn],tar_ins[maxn],deep[maxn],low[maxn];
int tar_cnt,tar_num,tar_bel[maxn],du[maxn];

void add(int x,int y)
{
  to[tot]=y;
  nex[tot]=head[x];
  head[x]=tot++;
}
void tarjan(int x,int fa)
{
  low[x]=deep[x]=++tar_cnt;
  tar_st[++top]=x; tar_ins[x]=1;
  for(int i=head[x]; i!=-1; i=nex[i])
  {
  	int y=to[i];
  	if(i==(fa^1)) continue;
  	if(deep[y]==0)
  	{
  		tarjan(y,i);
  		low[x]=min(low[x],low[y]);
  		if(low[y]>deep[x]) ans++;
  	}
  	else if(tar_ins[y]) low[x]=min(low[x],deep[y]);
  }
  if(deep[x]==low[x])
  {
  	tar_num++; int tmp;
  	do{
  		tmp=tar_st[top--]; tar_ins[tmp]=1;
  		tar_bel[tmp]=tar_num;
  	}while(tmp!=x);
  }
}
int main()
{
  cin>>n>>m;
  for(int i=1; i<=n; i++) head[i]=-1;
  for(int i=1; i<=m; i++) 
  {
  	int a,b; cin>>a>>b;
  	add(a,b); add(b,a);
  }
  for(int i=1; i<=n; i++)
  {
  	if(deep[i]==0) tarjan(i,-1);
  }
  cout<<ans<<endl;
  for(int i=1; i<=n; i++)
  	for(int j=head[i]; j!=-1; j=nex[j])
  	{
  		int y=to[j];
  		if(tar_bel[i]!=tar_bel[y]) 
  			du[tar_bel[i]]++,du[tar_bel[y]]++;
  	}
  for(int i=1; i<=tar_num; i++)
  	if(du[i]==2) ans2++;
  cout<<((ans2+1)/2)<<endl;
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值