POJ 1986 LCA,tarjan实现

Distance Queries
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 4010 Accepted: 1418
Case Time Limit: 1000MS

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

Hint

Farms 2 and 6 are 20+3+13=36 apart.

Source

最先开始用RMQ,却很郁闷的超时了后来改成了RMQ

Source Code

Problem: 1986 User: bingshen
Memory: 4924K Time: 1344MS
Language: C++ Result: Accepted
  • Source Code
    #include<stdio.h>
    #include<vector>
    #include<algorithm>
    #define maxn 40010
    
    using namespace std;
    
    struct node
    {
    	int v;
    	int w;
    };
    
    bool vis[maxn];
    int parent[maxn];
    int dis[maxn];
    int ans[maxn];
    
    vector<node>p[maxn];
    vector<node>q[maxn];
    
    void init()
    {
    	int i;
    	for(i=0;i<maxn;i++)
    	{
    		p[i].clear();
    		q[i].clear();
    	}
    	memset(dis,0,sizeof(dis));
    	memset(parent,-1,sizeof(parent));
    	memset(vis,0,sizeof(vis));
    }
    
    int find(int x)
    {
    	if(parent[x]==-1)
    		return x;
    	else
    		return parent[x]=find(parent[x]);
    }
    
    void tarjan(int u,int dist)
    {
    	int v,w,i;
    	dis[u]=dist;
    	vis[u]=1;
    	for(i=0;i<q[u].size();i++)
    	{
    		v=q[u][i].v;
    		w=q[u][i].w;
    		if(vis[v])
    		{
    			ans[w]=dis[u]+dis[v]-2*dis[find(v)];
    		}
    	}
    	for(i=0;i<p[u].size();i++)
    	{
    		v=p[u][i].v;
    		w=p[u][i].w;
    		if(!vis[v])
    		{
    			tarjan(v,dist+w);
    			parent[v]=u;
    		}
    	}
    }
    
    int main()
    {
    	int i,m,n,k,a,b,l;
    	char s[2];
    	node x;
    	init();
    	scanf("%d%d",&n,&m);
    	for(i=0;i<m;i++)
    	{
    		scanf("%d%d%d%s",&a,&b,&l,s);
    		x.v=b;
    		x.w=l;
    		p[a].push_back(x);
    		x.v=a;
    		p[b].push_back(x);
    	}
    	scanf("%d",&k);
    	for(i=1;i<=k;i++)
    	{
    		scanf("%d%d",&a,&b);
    		x.v=b;
    		x.w=i;
    		q[a].push_back(x);
    		x.v=a;
    		q[b].push_back(x);
    	}
    	tarjan(1,0);
    	for(i=1;i<=k;i++)
    		printf("%d/n",ans[i]);
    	return 0;
    }

用DFS+RMQ的也写在这儿吧,虽然超时了。。

Source Code

Problem: 1986 User: bingshen
Memory: N/A Time: N/A
Language: G++ Result: Time Limit Exceeded
  • Source Code
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<vector>
    #define inf 99999999
    #define maxn 40030
    
    using namespace std;
    
    struct node
    {
    	int v;
    	int dis;
    };
    
    vector<node>tree[maxn];
    vector<int>hash[maxn];
    int eular[2*maxn],deep[2*maxn],first[maxn],dis[maxn];
    bool used[maxn];
    int rmq[2*maxn][20];
    int num;
    
    void dfs(int root,int dep)
    {
    	int i;
    	used[root]=true;
    	eular[num]=root;
    	deep[num]=dep;
    	num++;
    	for(i=0;i<tree[root].size();i++)
    	{
    		if(!used[tree[root][i].v])
    		{		
    			dis[tree[root][i].v]=dis[root]+tree[root][i].dis;
    			dfs(tree[root][i].v,dep+1);
    			eular[num]=root;
    			deep[num]=dep;
    			num++;
    		}
    	}
    }
    /*
    int min(int a,int b)
    {
    	if(a>b)
    		return b;
    	else
    		return a;
    }
    */
    void rmqinit()
    {
    	int i,len,j;
    	for(i=0;i<num;i++)
    	{
    		rmq[i][0]=deep[i];
    		hash[deep[i]].push_back(i);
    	}
    	for(i=1;;i++)
    	{
    		len=(1<<i);
    		if(len+1>num)
    			break;
    		for(j=0;j+len<num;j++)
    			rmq[j][i]=min(rmq[j][i-1],rmq[j+len/2][i-1]);
    	}
    }
    
    int find(int l,int r)
    {
    	int i;
    	int len=r-l+1;
    	for(i=0;(1<<i)<=len;i++);
    	i--;
    	return min(rmq[l][i],rmq[r-(1<<i)+1][i]);
    }
    
    int main()
    {
    	int i,n,m,a,b,l;
    	char s[5];
    	scanf("%d%d",&n,&m);
    	for(i=1;i<=m;i++)
    	{
    		node p;
    		scanf("%d%d%d%s",&a,&b,&l,s);
    		p.v=b;
    		p.dis=l;
    		tree[a].push_back(p);
    		p.v=a;
    		tree[b].push_back(p);
    	}
    	num=0;
    	dfs(1,0);
    	memset(used,0,sizeof(used));
    	for(i=0;i<num;i++)
    	{
    		if(!used[eular[i]])
    		{
    			used[eular[i]]=true;
    			first[eular[i]]=i;
    		}
    	}
    	int q;
    	scanf("%d/n",&q);
    	rmqinit();
    	int ans;
    	while(q--)
    	{
    		scanf("%d%d",&a,&b);
    		if(first[b]<first[a])
    			swap(a,b);
    		int x=find(first[a],first[b]);
    		for(i=0;i<hash[x].size();i++)
    		{
    			int u=hash[x][i];
    			if(hash[x][i]>=first[a]&&hash[x][i]<=first[b])
    			{
    				ans=hash[x][i];
    				break;
    			}
    		}
    		printf("%d/n",dis[a]+dis[b]-2*dis[eular[ans]]);
    	}
    	return 0;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值