#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=;
int n,m;
int x,y,z;
struct node
{
int u,v,w,next;
}edge[MAXN],a[MAXN];
int num=;
int head[MAXN];
int f[MAXN];
int anum=;
int ahead[MAXN];
int deep[MAXN];
int s[MAXN][];
int take[MAXN][];
void edge_add(int x,int y,int z)
{
edge[num].u=x;
edge[num].v=y;
edge[num].w=z;
edge[num].next=head[x];
head[x]=num++;
}
void a_add(int i)
{
a[anum].u=edge[i].u;
a[anum].v=edge[i].v;
a[anum].w=edge[i].w;
a[anum].next=ahead[a[anum].u];
ahead[a[anum].u]=anum++;
}
int comp(const node & a ,const node & b)
{return a.w>b.w;} int find(int x)
{
if(f[x]!=x)
f[x]=find(f[x]);
return f[x];
}
void unionn(int x,int y)
{
int fx=find(x);
int fy=find(y);
f[fx]=fy;
}
void Biggest_Kruskal()
{
sort(edge+,edge+num,comp);
int k=;
for(int i=;i<num;i++)
{
int uu=edge[i].u;
int vv=edge[i].v;
if(find(uu)!=find(vv))
{
unionn(uu,vv);
a_add(i);
k++;
}
if(k==n-)break;
}
for(int i=;i<=anum;i++)
cout<<a[i].u<<" "<<a[i].v<<" "<<a[i].w<<" "<<a[i].next<<endl;
}
void Build_Tree(int p)
{
for(int i=ahead[p];i!=-;i=a[i].next)
{
int will=a[i].v;
if(deep[will]==)
{
deep[will]=deep[p]+;
s[will][]=p;
take[will][]=a[i].w;
Build_Tree(will);
}
}
}
void Initialize_Step()
{
for(int i=;i<=;i++)
{
for(int j=;j<=n;j++)
{
s[j][i]=s[s[j][i-]][i-];
take[j][i]=min(take[j][i-],take[s[j][i-]][i-]);
}
}
}
int LCA(int x,int y)
{
int ans=0x7ff;
if(deep[x]<deep[y])
swap(x,y);
for(int i=;i>=;i--)
{
if(deep[s[x][i]]>=deep[y])
x=s[x][i];
}
if(x==y)
return x;
for(int i=;i>=;i--)
{
if(s[x][i]!=s[y][i])
{
x=s[x][i];
y=s[y][i];
ans=min(ans,take[x][i]);
ans=min(ans,take[y][i]);
}
}
ans=min(ans,take[x][]);
ans=min(ans,take[y][]);
return ans;
}
int main()
{
scanf("%d%d",&n,&m); for(int i=;i<=n;i++)
{head[i]=-;f[i]=i;ahead[i]=-;} for(int i=;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
edge_add(x,y,z);
//edge_add(y,x,z);
}
Biggest_Kruskal();
deep[]=;
for(int i=;i<=n;i++)
Build_Tree(i);
Initialize_Step();
int q;
scanf("%d",&q);
for(int i=;i<=q;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(find(x)!=find(y))
{
printf("-1\n");
continue;
}
printf("%d\n",LCA(x,y));
}
return ;
}

P1967 货车运输 未完成的更多相关文章

  1. 洛谷 P1967 货车运输

    洛谷 P1967 货车运输 题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在 ...

  2. P1967 货车运输

    P1967 货车运输最大生成树+lca+并查集 #include<iostream> #include<cstdio> #include<queue> #inclu ...

  3. 洛谷P3379lca,HDU2586,洛谷P1967货车运输,倍增lca,树上倍增

    倍增lca板子洛谷P3379 #include<cstdio> struct E { int to,next; }e[]; ],anc[][],log2n,deep[],n,m,s,ne; ...

  4. Luogu P1967 货车运输(Kruskal重构树)

    P1967 货车运输 题面 题目描述 \(A\) 国有 \(n\) 座城市,编号从 \(1\) 到 \(n\) ,城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 \ ...

  5. 【杂题总汇】NOIP2013(洛谷P1967) 货车运输

    [洛谷P1967] 货车运输 重做NOIP提高组ing... +传送门-洛谷P1967+ ◇ 题目(copy from 洛谷) 题目描述 A国有n座城市,编号从1到n,城市之间有m条双向道路.每一条道 ...

  6. P1967 货车运输(倍增LCA,生成树)

    题目链接: https://www.luogu.org/problemnew/show/P1967 题目描述 A国有n座城市,编号从 1到n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制, ...

  7. 洛谷 P1967 货车运输 Label: 倍增LCA && 最小瓶颈路

    题目描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多 ...

  8. 洛谷 P1967 货车运输(克鲁斯卡尔重构树)

    题目描述 AAA国有nn n座城市,编号从 11 1到n nn,城市之间有 mmm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qqq 辆货车在运输货物, 司机们想知道每辆车在不超过车 ...

  9. P1967 货车运输 树链剖分

    题目描述 AA国有nn座城市,编号从 11到nn,城市之间有 mm 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 qq 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最 ...

随机推荐

  1. (linux)mmccard驱动的读写过程解析

      mmc io的读写从mmc_queue_thread()的获取queue里面的request开始. 先列出调用栈,看下大概的调用顺序, 下面的内容主要阐述这些函数如何工作. host->op ...

  2. noip2016前的话[漫谈]

    今天是11月15日,离noip2016还剩三天: 今年我也是高二了,回首一下去年的时光,真的仿佛仍在昨天,我甚至现在还清楚的记得,当年那次我们做的每一件事: 星期五,回去与室友告别,得到了祝愿,乘公交 ...

  3. nyoj 999

    nyoj 999: 点击打开题目链接 题目思路,处理一下地图,把 D E 能看到的地方标记一下.然后就是暴力广搜一下.标记状态,因为同样在同一个点,但是你刚出发到达那点和找到D之后到达相同的点和找到E ...

  4. 西交校赛 I. GZP and CS(数位dp)

    I. GZP and CS GZP love to play Counter-Strike(CS). One day GZP was playing a mod of CS. The counter- ...

  5. POJ1474:Video Surveillance(求多边形的核)(占位)

    A friend of yours has taken the job of security officer at the Star-Buy Company, a famous depart- me ...

  6. NOI1995 石子合并

    传送门 这道题是经典的区间DP.因为它要求有每两个相邻的石子堆合并,所以很显然对于区间[l,r]内的情况,我们只要枚举端点k,之后把这左右两端的石子合并取最大/小即可. 之后,这题是环形怎么破?显然不 ...

  7. ES6之拷贝对象

    function copyObject(orig) { var copy = Object.create(Object.getPrototypeOf(orig)); //创建一个新的原型对象 copy ...

  8. C3P0Tool

    c3p0-config.xml <c3p0-config> <named-config name="c3p0"> <property name=&qu ...

  9. linux mplayer 播放yuv格式 (转载)

    转自:http://blog.csdn.net/ly0303521/article/details/38713791 在mplayer中查看YUV格式的图片或视频,可使用如下命令: mplayer - ...

  10. 【WIP】MVVM

    创建: 2018/04/05 懒得写了