The main topic: give you a diagram, and then give you a Q representative has q, each time plus a side after a few bridges. This is where the heavy edges are processed. Each time an edge is added, the common ancestor of two points on the search tree is lost on all points of the bridge. Here on the heavy side of the processing to say, I used to write the first time did not consider this problem, but incredibly past ... After the ... Very nonsense, but the problem of the re-edge is really there, here we use a bridge array to save bridges, because there is a heavy edge exists only bridge number is 1 when the path is the bridges, otherwise it is not bridge bridge[i] refers to I and Father[i] is a bridge topic big Meaning: Give you a diagram, and then give you a Q representative has q, every time plus a side after a few bridges. This is where the heavy edges are processed. Each time an edge is added, the common ancestor of two points on the search tree is lost on all points of the bridge. Here on the heavy side of the processing to say, I used to write the first time did not consider this problem, but incredibly past ... After the ... Very nonsense, but the problem of the heavy side is really there, here we use an array of bridges to save the bridge, because there is a heavy edge exists only bridge number is 1 when the path is only when the road is the bridges, otherwise it is not bridge bridge[i] refers to I and Father[i] is a bridge
#include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include < vector> #include <queue> #include <cmath> #include <stack> #include <cstring>usingnamespace STD; #define INF 0xfffffff#define min (A, b) (A<B?A:B) #define MAXN 100005int m, N, Time, Ans;int DFN[MAXN], LOW[MAXN], Fat HER[MAXN], bridge[maxn];vector<int> g[maxn];void init () {memset (DFN, 0, sizeof (DFN)); memset (Low, 0, sizeof (low)); memset (bridge, 0, sizeof (bridge)); memset (Father, 0, sizeof (Father)); Time = ans = 0; for (int i=0; i<=n; i++) G[i].clear ();} void Tarjan (int u,int fa) {Dfn[u] = low[u] = ++time; Father[u] = FA; int len = G[u].size (), V; for (int i=0; i<len; i++) {v = g[u][i]; if (!low[v]) {Tarjan (V, u); Low[u] = min (Low[u], low[v]); if (Dfn[u] < Low[v]) {Bridge[v] + +; Ans + +; } } ElseIf (v! = FA) {Low[u] = min (Low[u], dfn[v]); if (Dfn[u] < Low[v]) {Bridge[v] + +; Ans--; }}}}void Lca (int a,int b) {if (a = = b) return; if (Dfn[a] > Dfn[b]) {if (bridge[a] = = 1) {Bridge[a] = 0; Ans--; } Lca (Father[a], b); } else {if (bridge[b] = = 1) {Bridge[b] = 0; Ans--; } Lca (A, father[b]); }}int Main () {int cas = 1; while (scanf ("%d%d", &n, &m), m+n) {int Q, a, B; Init (); while (M-) {scanf ("%d%d", &a, &b); G[a].push_back (b); G[b].push_back (a); } scanf ("%d", &q); Tarjan (1, 0); printf ("%d\n", ans); printf ("Case%d:\n", CAS + +); while (Q--) {scanf ("%d%d", &a, &b); Lca (A, b); printf ("%d\n", ans); }} Return0;} /*4 41 22 12 31 421 23 4 * *
POJ 3694Network (Graph-free bridge + heavy-edge processing +lca)