标签:this mon ace 之间 sam nts lca use integer
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <vector> 5 using namespace std; 6 const int N = 40010; 7 struct Nod{ 8 int to, w; 9 Nod(int a = 0, int b = 0){ 10 to = a; w = b; 11 } 12 }; 13 int ans[N]; 14 int par[N],vis[N],mark[N],dis[N]; 15 int n,m; 16 vector<Nod> mp[N]; 17 vector<Nod> query[N]; 18 void init(){ 19 for(int i = 0; i <= n; i ++){ 20 mp[i].clear(); 21 query[i].clear(); 22 vis[i] = mark[i] = 0; 23 par[i] = i; 24 } 25 memset(ans,-1,sizeof(ans)); 26 } 27 int findset(int x){ 28 if(x != par[x]) par[x] = findset(par[x]); 29 return par[x]; 30 } 31 32 void dfs(int u){ 33 for(int i = 0; i < query[u].size(); i ++){ 34 int v = query[u][i].to; 35 if(vis[v]&&ans[query[u][i].w]==-1&&!mark[findset(v)]){ 36 ans[query[u][i].w] = dis[u]+dis[v]-2*dis[findset(v)]; 37 } 38 } 39 for(int i = 0; i < mp[u].size(); i++){ 40 int v = mp[u][i].to; 41 if(!vis[v]){ 42 vis[v] = 1; 43 dis[v] = dis[u]+mp[u][i].w; 44 dfs(v); 45 par[v] = u; 46 } 47 } 48 } 49 50 int main(){ 51 int t; 52 scanf("%d",&t); 53 while(t--){ 54 scanf("%d%d",&n,&m); 55 init(); 56 int x, y, z; 57 for(int i = 0; i < n-1; i ++){ 58 scanf("%d%d%d",&x,&y,&z); 59 mp[x].push_back(Nod(y,z)); 60 mp[y].push_back(Nod(x,z)); 61 } 62 for(int i = 1; i <= m; i ++){ 63 scanf("%d%d",&x,&y); 64 query[x].push_back(Nod(y,i)); 65 query[y].push_back(Nod(x,i)); 66 } 67 for(int i = 1; i <= n; i ++){ 68 if(!vis[i]){ 69 printf("%d+++\n",i); 70 vis[i] = 1; 71 dis[i] = 0; 72 dfs(i); 73 mark[i] = 1; 74 } 75 } 76 for(int i = 1; i <= m; i ++){ 77 printf("%d\n",ans[i]); 78 } 79 } 80 return 0; 81 }
标签:this mon ace 之间 sam nts lca use integer
原文地址:http://www.cnblogs.com/xingkongyihao/p/7214263.html