ZJU 2588 求一个图中的桥;
太假了。。
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N = 10005;
const int M = 100008;
struct Node
{
int vdx;
int flag;
Node *next;
}*map[N],nome[4*M];
int again[M],n,m,k;
void addedge(int x,int y,int flag)
{
Node *ptr=&nome[k++];
ptr->vdx =y;
ptr->flag =flag;
ptr->next =map[x];
map[x] = ptr;
}
int find(int x,int y)
{
for(Node *ptr=map[x];ptr;ptr=ptr->next)
{
if(ptr->vdx==y)
{
return ptr->flag;
}
}
return -1;
}
int dfn[N],brige[M],cl[M],low[N],Num,ans;
void dfnlow(int now,int father)
{
dfn[now]=low[now]=ans++;
for(Node *ptr=map[now];ptr;ptr=ptr->next)
{
int idx = ptr-> vdx,flag = ptr -> flag;
if(dfn[idx]==-1)
{
dfnlow(idx,now);
low[now]=min(low[now],low[idx]);
if(low[idx]>dfn[now])
{
if(!again[flag])
{
cl[Num++]=flag;
}
}
}
else if(idx!=father) low[now]=min(low[now],dfn[idx]);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
Num=0,k=0,ans=1;
for(int i=1;i<=N;i++)
{
map[i]=NULL;
dfn[i]=-1;
}
for(int i=1;i<=M;i++)
again[i]=brige[i]=0;
int from,to;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&from,&to);
int tmp=find(from,to);
if(tmp!=-1)
{
again[tmp]=1,again[i]=1;
}else
{
addedge(from,to,i);
addedge(to,from,i);
}
}
dfnlow(1,-1);
printf("%d/n",Num);
sort(cl,cl+Num);
if(Num>0)printf("%d",cl[0]);
for(int i=1;i<Num;i++)
printf(" %d",cl[i]);
if(Num>0) printf("/n");
if(T>0)printf("/n");
}
return 0;
}
pku 1523 求割点;
水~。。。。。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N = 1005;
struct Node
{
int vdx;
Node *next;
}*Graph[N],nome[20*N];
int dfn[N],low[N],cut[N],sub[N],root,k,cnt;
void addedge(int from,int to)
{
Node *ptr=&nome[k++];
ptr->vdx= to;
ptr->next=Graph[from];
Graph[from]=ptr;
}
void dfnlow(int now,int father)
{
int son=0;
dfn[now]=low[now]=cnt++;
for(Node *ptr=Graph[now];ptr;ptr=ptr->next)
{
int idx = ptr->vdx;
if(dfn[idx]==-1)
{
son++;
dfnlow(idx,now);
low[now]=min(low[now],low[idx]);
if(now!=root&&low[idx]>=dfn[now]){ sub[now]++;cut[now]=1;}
if(now==root&&son>1){sub[now]=son;cut[now]=1;}
}
else if(idx!=father)
{
low[now]=min(low[now],dfn[idx]);
}
}
}
int main()
{
int T=1,flag,more=0;
while(1)
{
for(int i=0;i<=N;i++) Graph[i]=NULL;
int from,to,n=0;
flag=0,k=0,cnt=1,root=1;
while(1)
{
scanf("%d",&from);
if(from==0)
{
if(flag==0){more=1;break;}
else break;
}
flag=1;
scanf("%d",&to);
if(n<from) n=from;
if(n<to) n=to;
addedge(from,to);
addedge(to,from);
}
if(more) break;
memset(dfn,-1,sizeof(dfn));
memset(low,-1,sizeof(low));
memset(cut, 0,sizeof(cut));
memset(sub, 0,sizeof(sub));
dfnlow(1,-1);
int ans=1;
printf("Network #%d/n",T++);
for(int i=1;i<=5;i++)
if(cut[i])
{
if(i==root) printf(" SPF node %d leaves %d subnets/n",i,sub[i]);
else printf(" SPF node %d leaves %d subnets/n",i,sub[i]+1);
ans=0;
}
if(ans) printf(" No SPF nodes/n");
printf("/n");
}
return 0;
}