zoj 3195
Posted AC菜鸟机
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zoj 3195相关的知识,希望对你有一定的参考价值。
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3320
离线算法RE了..
#include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int N = 50005; const int M =100005; struct Edge{ int u,v,w,next; }e[2*N]; struct Q_Edge{ int u,v,next,lca; }eq[2*M]; int head[N]; int _head[N]; int dis[N]; int vis[N]; int ances[N]; int father[N]; void addedge(int u,int v,int w,int &k){ e[k].u = u;e[k].v = v;e[k].w= w; e[k].next = head[u];head[u]=k++; } void add_qedge(int u,int v,int &k){ eq[k].u = u;eq[k].v = v;eq[k].lca = -1; eq[k].next = _head[u];_head[u]=k++; } int _find(int u){ if(u==father[u]) return father[u]; return father[u] = _find(father[u]); } void unions(int u,int v){ int x = _find(u),y = _find(v); father[x]=y; } void Targin(int u){ vis[u] = true; ances[u]=father[u] = u; for(int k = head[u];k!=-1;k=e[k].next){ if(!vis[e[k].v]){ int v = e[k].v,w = e[k].w; dis[v] = dis[u]+w; Targin(v); unions(v,u); ances[_find(u)] = u; } } for(int k = _head[u];k!=-1;k=eq[k].next){ if(vis[eq[k].v]){ int v = eq[k].v; eq[k].lca = eq[k^1].lca = ances[_find(v)]; } } } int main() { int n; while(scanf("%d",&n)!=EOF){ memset(head,-1,sizeof(head)); memset(_head,-1,sizeof(_head)); int tot=0; for(int i=1;i<n;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w,tot); addedge(v,u,w,tot); } tot = 0; int m; scanf("%d",&m); for(int i=0;i<m;i++){ int a,b,c; scanf("%d%d%d",&a,&b,&c); add_qedge(a,b,tot); add_qedge(b,a,tot); add_qedge(a,c,tot); add_qedge(c,a,tot); add_qedge(b,c,tot); add_qedge(c,b,tot); } memset(vis,0,sizeof(vis)); dis[0]=0; Targin(0); int a[3]; for(int i=0;i<m;i++){ int s = i*6; a[0] = dis[eq[s].u]+dis[eq[s].v]-2*dis[eq[s].lca]; ///a->b a[1] = dis[eq[s+2].u]+dis[eq[s+2].v]-2*dis[eq[s+2].lca]; ///a->c a[2] = dis[eq[s+4].u]+dis[eq[s+4].v]-2*dis[eq[s+4].lca]; ///b->c //sort(a,a+3); printf("%d\n",(a[2]+a[1]+a[0])/2); } printf("\n"); } }
以上是关于zoj 3195的主要内容,如果未能解决你的问题,请参考以下文章