POJ_1986_Distance Queries(LCA+tarjan)
2015-08-03 23:51
417 查看
Distance Queries
SubmitStatus
Description
Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same
input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing
distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!
Input
* Lines 1..1+M: Same format as "Navigation Nightmare"
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.
Sample Input
Sample Output
题意:在一棵树上,查询(u,v)最短距离。
分析:LCA+tarjan离线算法,模板题。
ps:最近写LCA用的都是数组来存边,感觉比vector好用多了~~~~~
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11131
代码清单:
Time Limit: 2000MS | Memory Limit: 30000KB | 64bit IO Format: %I64d & %I64u |
Description
Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same
input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing
distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible!
Input
* Lines 1..1+M: Same format as "Navigation Nightmare"
* Line 2+M: A single integer, K. 1 <= K <= 10,000
* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms.
Output
* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance.
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
题意:在一棵树上,查询(u,v)最短距离。
分析:LCA+tarjan离线算法,模板题。
ps:最近写LCA用的都是数组来存边,感觉比vector好用多了~~~~~
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11131
代码清单:
//#pragma comment(linker, "/STACK:102400000,102400000") #include<set> #include<map> #include<cmath> #include<queue> #include<stack> #include<ctime> #include<string> #include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; const int maxn = 40000 + 5; const int maxv = 40000 + 5; const int maxq = 10000 + 5; struct MAX{ int v,d; MAX(){} MAX(int v,int d){ this -> v = v; this -> d = d; } }; struct Q{ int v,id,next; }quary[2*maxq]; struct e{ int v,dis,next; }graph[2*maxn]; int n,m,q; int a,b,c; char s[3]; int father[maxn]; bool vis[maxn]; int ans[maxq]; int color[maxn]; int depth[maxn]; int nume,numq; int heade[maxn]; int headq[maxn]; void init(){ for(int i=1;i<=maxn;i++) father[i]=i; memset(ans,-1,sizeof(ans)); memset(color,0,sizeof(color)); memset(depth,0,sizeof(depth)); memset(vis,false,sizeof(vis)); memset(graph,0,sizeof(graph)); memset(quary,0,sizeof(quary)); memset(heade,-1,sizeof(heade)); memset(headq,-1,sizeof(headq)); nume=numq=0; } void add_E(int u,int v,int dis){ graph[nume].v=v; graph[nume].dis=dis; graph[nume].next=heade[u]; heade[u]=nume++; } void add_Q(int u,int v,int id){ quary[numq].v=v; quary[numq].id=id; quary[numq].next=headq[u]; headq[u]=numq++; } void input(){ for(int i=0;i<m;i++){ scanf("%d%d%d%s",&a,&b,&c,s); add_E(a,b,c); add_E(b,a,c); } scanf("%d",&q); for(int i=1;i<=q;i++){ scanf("%d%d",&a,&b); add_Q(a,b,i); add_Q(b,a,i); } } int Find(int x){ return x!=father[x] ? father[x]=Find(father[x]) : father[x]; } void tarjan(int u){ color[u]=1; vis[u]=true; for(int i=headq[u];i!=-1;i=quary[i].next){ int ID=quary[i].id; if(ans[ID]!=-1) continue; int v=quary[i].v; if(color[v]==0) continue; if(color[v]==1) ans[ID]=depth[u]-depth[v]; if(color[v]==2) ans[ID]=depth[u]+depth[v]-2*depth[Find(v)]; } for(int i=heade[u];i!=-1;i=graph[i].next){ int vv=graph[i].v; int dis=graph[i].dis; if(!vis[vv]){ depth[vv]=depth[u]+dis; tarjan(vv); color[vv]=2; father[vv]=u; } } } void solve(){ tarjan(1); for(int i=1;i<=q;i++){ printf("%d\n",ans[i]); } } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ init(); input(); solve(); } return 0; }
相关文章推荐
- iOS开发——UI进阶篇(十四)modal
- SPOJ PTO7J Query on a tree III(dfs序+主席树)
- SPOJ QTREE2 Query on a tree II (倍增LCA)
- 想要App做得美,这些UI/UX设计趋势你要知道啊
- SPOJ QTREE Query on a tree (树链剖分)
- UIScrollView上面添加的uiftextfield如何失去第一响应者
- UITableView Cell
- UE4信息交互-蓝图类中的信息交互
- UI 03 关于UITextField键盘遮挡问题
- FZU 1752 Quickmod Quickmulti
- Android UI: LinearLayout中layout_weight 属性的使用规则
- Xcode6以后segue取消原push与modal(deprecated)
- UI 03 UIButton 和 UITextField
- UITextField总结
- Unknown Class XX in Interface Builder File
- UIRectEdge
- UICntrol
- [leetcode-60]Permutation Sequence(C)
- hdu 4893 Wow! Such Sequence! (线段树 区间更新+单点更新)
- POJ2785 4 Values whose Sum is 0