Description
Input
Output
Sample Input
7 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 3 1 6 1 4 2 6
Sample Output
13 3 36
Hint
Source
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int maxn=100005; vector<pair<int,int> >tree[maxn],que[maxn];//树和询问 int ans[maxn],dis[maxn]; bool vis[maxn]; int father[maxn]; int ancestor[maxn]; int rank[maxn]; int in[maxn];//入度,找根 int n; void init() { for (int i=1;i<=n;i++) { rank[i]=1; father[i]=i; in[i]=0; vis[i]=0; ancestor[i]=0; ans[i]=0; //dis[i]=0; que[i].clear(); tree[i].clear(); } } int find(int x) { if (x==father[x]) return x; father[x]=find(father[x]); return father[x]; } void Union(int x,int y) { int a=find(x); int b=find(y); if (a!=b) { if (rank[a]<=rank[b]) //合并的时候按深度浅的向深的合并 { father[a]=b; rank[b]+=rank[a]; } else { father[b]=a; rank[a]+=rank[b]; } } } void LCA(int root,int fa) { ancestor[root]=root; int size=tree[root].size(); for (int i=0;i<size;i++) { if(tree[root][i].first==fa) continue; dis[tree[root][i].first]=dis[root]+tree[root][i].second; LCA(tree[root][i].first,root); Union(root,tree[root][i].first); ancestor[find(root)]=root; } vis[root]=1; size=que[root].size(); for (int i=0;i<size;i++) { if (vis[que[root][i].first]) ans[que[root][i].second]=dis[root]+dis[que[root][i].first]-2*dis[ancestor[find(que[root][i].first)]]; } } int main() { int m; //scanf("%d",&t); // while (t--) while(~scanf("%d%d",&n,&m)) { int x,y,d,k; char c[4]; //scanf("%d",&n); init(); for (int i=1;i<=m;i++) { scanf("%d%d%d%s",&x,&y,&d,c); tree[x].push_back(make_pair(y,d)); tree[y].push_back(make_pair(x,d)); // in[y]++; } scanf("%d",&k); for(int i=1;i<=k;i++) { scanf("%d%d",&x,&y); que[x].push_back(make_pair(y,i)); que[y].push_back(make_pair(x,i)); } dis[1]=0; // scanf("%d%d",&x,&y); LCA(1,0); for (int i=1;i<=k;i++) { printf("%d\n",ans[i]); } } return 0; }
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<vector> using namespace std; const int maxn=100005; vector<pair<int,int> >que[maxn]; int ans[maxn],dis[maxn]; bool vis[maxn]; int father[maxn]; int ancestor[maxn]; int rank[maxn]; int in[maxn];//入度,找根 int n; struct node { int weight; int to; int next; }edge[maxn]; int head[maxn]; int tot; void addedge(int from,int to,int weight) { edge[tot].to=to; edge[tot].weight=weight; edge[tot].next=head[from]; head[from]=tot++; } void init() { memset(head,-1,sizeof(head)); tot=0; for (int i=1;i<=n;i++) { rank[i]=1; father[i]=i; in[i]=0; vis[i]=0; ancestor[i]=0; ans[i]=0; //dis[i]=0; que[i].clear(); //tree[i].clear(); } } int find(int x) { if (x==father[x]) return x; father[x]=find(father[x]); return father[x]; } void Union(int x,int y) { int a=find(x); int b=find(y); if (a!=b) { if (rank[a]<=rank[b]) //合并的时候按深度浅的向深的合并 { father[a]=b; rank[b]+=rank[a]; } else { father[b]=a; rank[a]+=rank[b]; } } } void LCA(int root,int fa) { ancestor[root]=root; //int size=tree[root].size(); /*for (int i=0;i<size;i++) { if(tree[root][i].first==fa) continue; dis[tree[root][i].first]=dis[root]+tree[root][i].second; LCA(tree[root][i].first,root); Union(root,tree[root][i].first); ancestor[find(root)]=root; }*/ for(int i=head[root];i!=-1;i=edge[i].next) { if(edge[i].to==fa) continue; dis[edge[i].to]=dis[root]+edge[i].weight; LCA(edge[i].to,root); Union(root,edge[i].to); ancestor[find(root)]=root; } vis[root]=1; int size=que[root].size(); for (int i=0;i<size;i++) { if (vis[que[root][i].first]) ans[que[root][i].second]=dis[root]+dis[que[root][i].first]-2*dis[ancestor[find(que[root][i].first)]]; } } int main() { int m; //scanf("%d",&t); // while (t--) while(~scanf("%d%d",&n,&m)) { int x,y,d,k; char c[4]; //scanf("%d",&n); init(); for (int i=1;i<=m;i++) { scanf("%d%d%d%s",&x,&y,&d,c); //tree[x].push_back(make_pair(y,d)); //tree[y].push_back(make_pair(x,d)); addedge(x,y,d); addedge(y,x,d); // in[y]++; } scanf("%d",&k); for(int i=1;i<=k;i++) { scanf("%d%d",&x,&y); que[x].push_back(make_pair(y,i)); que[y].push_back(make_pair(x,i)); } dis[1]=0; // scanf("%d%d",&x,&y); LCA(1,0); for (int i=1;i<=k;i++) { printf("%d\n",ans[i]); } } return 0; }