您的位置:首页 > 其它

bzoj 2730: [HNOI2012]矿场搭建 dfs

2016-04-21 09:02 309 查看
首先求出所有割顶,然后统计一个点双中有多少割顶。如果=1个,那么只需要在点双中除了割顶的地方建一个;否则不需要建。如果只有一个点双就特判一下建两个。

AC代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 10005
using namespace std;

int ri,n,m,cnt,now,all,tot,dfsclk,fst
,pnt
,nxt
,pos
,low
,mrk
,ans1;
bool cut
; long long ans2;
void add(int x,int y){
pnt[++tot]=y; nxt[tot]=fst[x]; fst[x]=tot;
}
void dfs(int x,int fa){
int p,sz=0; pos[x]=low[x]=++dfsclk;
for (p=fst[x]; p; p=nxt[p]){
int y=pnt[p]; if (y==fa) continue;
if (!pos[y]){ sz++; dfs(y,x); }
low[x]=min(low[x],low[y]);
if (low[y]>=pos[x] && fa) cut[x]=1;
}
if (!fa && sz>1) cut[x]=1; if (cut[x]) cnt++;
}
void calc(int x){
mrk[x]=now; all++; int p;
for (p=fst[x]; p; p=nxt[p]){
int y=pnt[p];
if (cut[y] && mrk[y]!=now){ mrk[y]=now; cnt++; }
if (!mrk[y]) calc(y);
}
}
int main(){
while (~scanf("%d",&m) && m){
int i,x,y; tot=n=0; memset(fst,0,sizeof(fst));
for (i=1; i<=m; i++){
scanf("%d%d",&x,&y);
n=max(n,max(x,y)); add(x,y); add(y,x);
}
memset(pos,0,sizeof(pos)); memset(low,0,sizeof(low));
dfsclk=cnt=0; memset(cut,0,sizeof(cut));
for (i=1; i<=n; i++)
if (!pos[i]) dfs(i,0);
if (!cnt){ ans1=2; ans2=(long long)n*(n-1)>>1; } else{
ans1=0; ans2=1; memset(mrk,0,sizeof(mrk));
for (i=1; i<=n; i++) if (!cut[i] && !mrk[i]){
cnt=all=0; now++; calc(i);
if (cnt==1){ ans1++; ans2*=all; }
}
}
printf("Case %d: %d %lld\n",++ri,ans1,ans2);
}
}


by lych
2016.4.21
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: