标签:des style http color io os java ar strong
9 1 3 4 1 3 5 1 2 2 6 1 5 6 3 1 6 3 2 6 1 2 1 3 2 4 2 5 3 6 3 7 0
Case 1: 2 4 Case 2: 4 1
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #define maxn 100005 #define MAXN 100005 #define INF 0x3f3f3f3f #pragma comment (linker,"/STACK:102400000,102400000") typedef long long ll; using namespace std; int n,m,cnt,tot,flag; int lev,bcccnt; int head[maxn]; int dfn[maxn],low[maxn]; bool vis[maxn],ok[maxn]; struct Node { int v,w,next; } edge[MAXN]; int stau[maxn],stav[maxn],top,bccno[maxn]; vector<int>bcc[maxn]; void addedge(int u,int v,int w) { cnt++; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt; } void Tarjan(int u,int pre) { int i,j,t,v,num=0; low[u]=dfn[u]=++lev; for(i=head[u];i;i=edge[i].next) { v=edge[i].v; if(vis[v]) { if(v!=pre) low[u]=min(low[u],dfn[v]); // 桥不能用父亲来更新 割点随意 } else { vis[v]=1; top++; stau[top]=u; stav[top]=v; num++; Tarjan(v,u); if(dfn[u]<=low[v]) { if(pre!=0) ok[u]=1; // 不是根 bcccnt++; bcc[bcccnt].clear(); while(!(stau[top]==u&&stav[top]==v)) { if(bccno[stav[top]]!=bcccnt) bccno[stav[top]]=bcccnt,bcc[bcccnt].push_back(stav[top]); if(bccno[stau[top]]!=bcccnt) bccno[stau[top]]=bcccnt,bcc[bcccnt].push_back(stau[top]); top--; } if(bccno[stav[top]]!=bcccnt) bccno[stav[top]]=bcccnt,bcc[bcccnt].push_back(stav[top]); if(bccno[stau[top]]!=bcccnt) bccno[stau[top]]=bcccnt,bcc[bcccnt].push_back(stau[top]); top--; } low[u]=min(low[u],low[v]); } } if(pre==0&&num>1) ok[u]=1; } int main() { int i,j,t,u,v,w,ca=0; while(scanf("%d",&m),m) { cnt=n=0; memset(head,0,sizeof(head)); for(i=1; i<=m; i++) // 建图 { scanf("%d%d",&u,&v); addedge(u,v,0); addedge(v,u,0); n=max(n,u); n=max(n,v); } memset(vis,0,sizeof(vis)); memset(ok,0,sizeof(ok)); memset(bccno,0,sizeof(bccno)); bcccnt=0; for(i=1; i<=n; i++)// 割点或者桥的求解 { if(vis[i]) continue ; lev=0; vis[i]=1; top=0; Tarjan(i,0); } ll ans=0,res=1; for(i=1;i<=bcccnt;i++) { int num=0; for(j=0;j<bcc[i].size();j++) { if(ok[bcc[i][j]]) num++; } if(num==1) { ans++; res*=(bcc[i].size()-1); } } if(bcccnt==1) { ans=2; res=ll(n)*ll(n-1)/2; } printf("Case %d: %I64d %I64d\n",++ca,ans,res); } return 0; }
hdu 3844 Mining Your Own Business (点双连通分量)
标签:des style http color io os java ar strong
原文地址:http://blog.csdn.net/tobewhatyouwanttobe/article/details/39499183