标签:
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
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #pragma comment(linker, "/STACK:102400000,102400000") 6 using namespace std; 7 typedef long long LL; 8 const int maxn = 50010; 9 struct arc { 10 int to,next; 11 arc(int x = 0,int y = -1) { 12 to = x; 13 next = y; 14 } 15 } e[1000010]; 16 int head[maxn],dfn[maxn],low[maxn],idx,tot,cn; 17 bool cut[maxn]; 18 void add(int u,int v) { 19 e[tot] = arc(v,head[u]); 20 head[u] = tot++; 21 e[tot] = arc(u,head[v]); 22 head[v] = tot++; 23 } 24 void tarjan(int u,int fa) { 25 dfn[u] = low[u] = ++idx; 26 int son = 0; 27 for(int i = head[u]; ~i; i = e[i].next) { 28 if(e[i].to == fa) continue; 29 if(!dfn[e[i].to]) { 30 tarjan(e[i].to,u); 31 son++; 32 low[u] = min(low[u],low[e[i].to]); 33 if(fa != -1 && low[e[i].to] >= dfn[u] || fa == -1 && son > 1) { 34 cut[u] = true; 35 cn++; 36 } 37 } else low[u] = min(low[u],dfn[e[i].to]); 38 } 39 } 40 bool vis[maxn]; 41 int cnt,n,m,cao; 42 bool fk[maxn]; 43 void dfs(int u,int fa) { 44 vis[u] = true; 45 cnt++; 46 for(int i = head[u]; ~i; i = e[i].next) { 47 if(e[i].to == fa) continue; 48 if(cut[e[i].to]) { 49 if(!fk[e[i].to]) { 50 cao++; 51 fk[e[i].to] = true; 52 } 53 continue; 54 } 55 if(vis[e[i].to]) continue; 56 dfs(e[i].to,fa); 57 } 58 } 59 int main() { 60 int u,v,cs = 1; 61 while(scanf("%d",&m),m) { 62 memset(head,-1,sizeof head); 63 for(int i = tot = n = idx = cn = 0; i < m; ++i) { 64 scanf("%d%d",&u,&v); 65 add(u,v); 66 n = max(n,max(u,v)); 67 } 68 memset(dfn,0,sizeof dfn); 69 memset(cut,false,sizeof cut); 70 for(int i = 1; i <= n; ++i) 71 if(!dfn[i]) tarjan(i,-1); 72 LL ret = 1; 73 if(!cn) printf("Case %d: %d %I64d\n",cs++,2,(LL)n*(n-1)/2); 74 else { 75 memset(vis,false,sizeof vis); 76 int ri = 0; 77 for(int i = 1; i <= n; ++i) { 78 if(cut[i] || vis[i]) continue; 79 cnt = cao = 0; 80 memset(fk,false,sizeof fk); 81 dfs(i,-1); 82 if(cao < 2 && cnt) {ri++; ret *= cnt;} 83 } 84 printf("Case %d: %d %I64d\n",cs++,ri,ret); 85 } 86 } 87 return 0; 88 }
HDU 3844 Mining Your Own Business
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4692769.html