标签:new 结构体 ges sample from name 缩点 scanf memset
http://acm.hdu.edu.cn/showproblem.php?pid=4612
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 8309 Accepted Submission(s): 1905
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 #include <queue> 6 #include <vector> 7 using namespace std; 8 9 const int MAXN = 200010; 10 const int MAXM = 1000015; 11 struct Edge 12 { 13 int to,from,next; 14 }edge[MAXM*2],eedge[MAXM*2]; 15 int head[MAXN],edge_cnt; 16 int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~block 17 int Index,top; 18 int block; 19 bool Instack[MAXN]; 20 int bridge; 21 int n,m; 22 int key,d,vis[MAXN]; 23 void addedge(int u,int v) 24 { 25 edge[edge_cnt].from=u; 26 edge[edge_cnt].to = v;edge[edge_cnt].next = head[u]; 27 head[u] = edge_cnt++; 28 } 29 void addedge2(int u,int v) 30 { 31 eedge[edge_cnt].from=u; 32 eedge[edge_cnt].to = v;eedge[edge_cnt].next = head[u]; 33 head[u] = edge_cnt++; 34 } 35 void Tarjan(int u,int id) 36 { 37 int v; 38 Low[u] = DFN[u] = ++Index; 39 Stack[top++] = u; 40 Instack[u] = true; 41 for(int i = head[u];i != -1;i = edge[i].next) 42 { 43 v = edge[i].to; 44 if( i == (id^1))continue; 45 if( !DFN[v] ) 46 { 47 Tarjan(v,i); 48 if(Low[u] > Low[v])Low[u] = Low[v]; 49 if(Low[v] > DFN[u]) 50 { 51 bridge++; 52 } 53 } 54 else if(Instack[v] && Low[u] > DFN[v]) 55 Low[u] = DFN[v]; 56 } 57 if(Low[u] == DFN[u]) 58 { 59 block++; 60 do 61 { 62 v = Stack[--top]; 63 Instack[v] = false; 64 Belong[v] = block; 65 } 66 while( v != u ); 67 } 68 } 69 void dfs(int u,int len) 70 { 71 vis[u]=1; 72 if(len > d) 73 { 74 d=len; 75 key=u; 76 } 77 for(int i = head[u]; i != -1 ; i=eedge[i].next) 78 { 79 int v=eedge[i].to; 80 if(!vis[v]) 81 { 82 dfs(v,len+1); 83 } 84 } 85 return; 86 } 87 void init() 88 { 89 edge_cnt=0; 90 memset(head,-1,sizeof(head)); 91 } 92 void solve() 93 { 94 memset(DFN,0,sizeof(DFN)); 95 memset(Low,0,sizeof(Low)); 96 memset(Belong,0,sizeof(Belong)); 97 memset(Instack,false,sizeof(Instack)); 98 Index = top = block = bridge = 0; 99 Tarjan(1,-1); 100 int orz=edge_cnt; 101 edge_cnt=0; 102 memset(head,-1,sizeof(head)); 103 for(int i = 0 ; i < orz ; i++) 104 { 105 if(Belong[edge[i].to]!=Belong[edge[i].from]) 106 { 107 108 addedge2(Belong[edge[i].to],Belong[edge[i].from]); 109 addedge2(Belong[edge[i].from],Belong[edge[i].to]); 110 } 111 } 112 d=0; key=0; 113 memset(vis,0,sizeof(vis)); 114 dfs(1,0); 115 d=0;// cout << key <<endl; 116 memset(vis,0,sizeof(vis)); 117 dfs(key,0); 118 119 cout << bridge-d<<endl; 120 } 121 int main() 122 { 123 scanf("%d%d",&n,&m); 124 while(n||m) 125 { 126 init(); 127 int M=m; 128 while(M--) 129 { 130 int u,v; 131 scanf("%d%d",&u,&v); 132 addedge(u,v); 133 addedge(v,u); 134 } 135 solve(); 136 scanf("%d%d",&n,&m); 137 } 138 return 0; 139 }
标签:new 结构体 ges sample from name 缩点 scanf memset
原文地址:https://www.cnblogs.com/MekakuCityActor/p/9038071.html