POJ_1986

http://poj.org/problem?id=1986

//最近公共祖先算法 #include<stdio.h> #include<string.h> #define MAXN 40010 #define MAXK 10010 struct Edge { int v, w, next; }e1[2*MAXN], q[2*MAXN]; int head1[MAXN], head2[MAXN], parent[MAXN], d[MAXN]; int ans[MAXK]; bool vis[MAXN]; int n, m, cnt1, cnt2; void init()//初始化的操作 { cnt1 = cnt2 = 1; memset(head1, -1, sizeof(head1)); memset(head2, -1, sizeof(head2)); memset(parent, -1, sizeof(parent)); memset(vis, 0, sizeof(vis)); } void addedge1(int u, int v, int w)//建立原图 { e1[cnt1].v = v; e1[cnt1].w = w; e1[cnt1].next = head1[u]; head1[u] = cnt1++; } void addedge2(int u, int v, int w)//建立查询图 { q[cnt2].v = v; q[cnt2].w = w; q[cnt2].next = head2[u]; head2[u] = cnt2++; } int find_set(int u)//寻找根节点 { if(parent[u] == -1) { return u; } return parent[u] = find_set(parent[u]); } void tarjan(int u, int distnow) { int i, v, w; d[u] = distnow; vis[u] = true; for(i = head2[u]; i!=-1; i = q[i].next) { v = q[i].v; w = q[i].w; if(vis[v]) { ans[w] = d[u] + d[v] - 2*d[find_set(v)]; } } for(i = head1[u]; i!=-1; i = e1[i].next) { v = e1[i].v; w = e1[i].w; if(!vis[v]) { tarjan(v, distnow+w); parent[v] = u; } } } int main() { // freopen("in.txt", "r", stdin); int i, k, u, v, w; char str[5]; while(scanf("%d %d", &n, &m) != EOF) { init(); while(m--) { scanf("%d %d %d %s", &u, &v, &w, str); addedge1(u, v, w); addedge1(v, u, w); } scanf("%d", &k); for(i=1; i<=k; i++) { scanf("%d %d", &u, &v); addedge2(u, v, i); addedge2(v, u, i); } tarjan(1, 0); for(i=1; i<=k; i++) printf("%d/n", ans[i]); } return 0; }

你可能感兴趣的:(算法,struct)