题目大意:有一张n点m边的带权无向图,和一些问题,每次询问两个点之间的路径的最大边权最小是多少。

解题思路:同NOIP2013货车运输,只是数据增大,大变成小,小变成大了而已。所以具体思路见货车运输。可见两份代码仅有略微差别。

C++ Code:

#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cstring>
#include<cstdio>
using std::sort;
using std::swap;
struct edge{
int u,v,t;
bool operator < (const edge& rhs)const{return t<rhs.t;}
}e[500005];
struct tree_edge{
int to,dist,nxt;
}E[1200005];
int n,m,fa[100005],head[100005]={0},cnt=0,ans,deep[100005],p[100005][19],sml[100005][19];
inline int min(int a,int b){return(a>b)?(a):(b);}
inline int readint(){
char c=getchar();
int p=0;
for(;!isdigit(c);c=getchar());
for(;isdigit(c);c=getchar())p=(p<<3)+(p<<1)+(c^'0');
return p;
}
int dad(int x){return(fa[x]==x)?(x):(fa[x]=dad(fa[x]));}
inline int addedge(int from,int to,int dist){
E[++cnt]=(tree_edge){to,dist,head[from]};
head[from]=cnt;
E[++cnt]=(tree_edge){from,dist,head[to]};
head[to]=cnt;
}
void dfs(int u){
for(int i=head[u];i;i=E[i].nxt)
if(!deep[E[i].to]){
deep[E[i].to]=deep[u]+1;
p[E[i].to][0]=u;
sml[E[i].to][0]=E[i].dist;
dfs(E[i].to);
}
}
void init(){
for(int j=1;(1<<j)<=n;++j)
for(int i=1;i<=n;++i)
if(p[i][j-1]!=-1)
p[i][j]=p[p[i][j-1]][j-1],sml[i][j]=min(sml[i][j-1],sml[p[i][j-1]][j-1]);
}
int lca(int x,int y,int& ans){
ans=-2000000000;
int i;
if(deep[x]<deep[y])swap(x,y);
for(i=0;(1<<i)<=n;++i);--i;
for(int j=i;j>=0;--j)
if(deep[p[x][j]]>=deep[y]){
ans=min(ans,sml[x][j]),x=p[x][j];
}
if(x==y)return x;
for(int j=i;j>=0;--j)
if(p[x][j]!=p[y][j]&&p[x][j]!=-1){
ans=min(ans,min(sml[x][j],sml[y][j]));
x=p[x][j];
y=p[y][j];
}
ans=min(ans,min(sml[x][0],sml[y][0]));
return p[x][0];
}
int main(){
n=readint(),m=readint();
for(int i=1;i<=m;++i)e[i].u=readint(),e[i].v=readint(),e[i].t=readint();
sort(e+1,e+m+1);
for(int i=1;i<=n;++i)fa[i]=i;
for(int okE=1,now=1;now<=m;++now){
int a=dad(e[now].u),b=dad(e[now].v);
if(a!=b){
fa[b]=a;
addedge(e[now].u,e[now].v,e[now].t);
++okE;
}
if(okE==n)break;
}
int Q=readint();
memset(deep,0,sizeof deep);
memset(p,-1,sizeof p);
memset(sml,0x3f,sizeof sml);
for(int i=1;i<=n;++i)
if(!deep[i]){
deep[i]=1;
dfs(i);
}
init();
while(Q--){
int x=readint(),y=readint();
int a=dad(x),b=dad(y);
if(a!=b){
puts("impossible");
continue;
}
lca(x,y,ans);
printf("%d\n",ans);
}
}

最新文章

  1. 谷歌chrome浏览器www.tradeadexchange.com广告弹窗跳转劫持病毒
  2. Error #2044: 未处理的 IOErrorEvent:。 text=Error #2035: 找不到 URL这是flash加载外部资源时有时会遇到的问题,对于此问题解决如下
  3. JSTL的全称:JSP Standard Tag Library, jsp 标准标签库
  4. centos在安装apache2.4版本的时候遇到ARP not found解决办法
  5. 【Struts2学习笔记(11)】对action的输入校验和XML配置方式实现对action的全部方法进行输入校验
  6. easyui datagrid 列排序
  7. 在Linux下安装Oracle12c
  8. C 上传文件到服务器(含接收端源码)
  9. Java工具类 通过ResultSet对象返回对应的实体List集合
  10. git学习01- 下载安装&amp;初始化库&amp;提交
  11. [Swift]LeetCode102. 二叉树的层次遍历 | Binary Tree Level Order Traversal
  12. 【译】如何高效的使用 Git
  13. The last packet sent successfully to the server was 0 milliseconds ago.[nutch---mysql ]
  14. 格式化json
  15. bootstrap datarangepicker如何使用
  16. js删除数组中某一项或几项的几种方法
  17. Derby的jar说明
  18. Linux设备驱动开发基础--阻塞型设备驱动
  19. Codeforces 1136F Cooperative Game (神仙题)
  20. Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree

热门文章

  1. sql 跟踪
  2. python网页问题
  3. png库结合zlib库使用出现的一个链接问题的解决
  4. Android 优雅的让Fragment监听返回键
  5. Java文件(io)编程——文件字节流的使用
  6. (Spring+IBatis+Struts1+Struts2+Hibernate+Java EE+Oracle)
  7. 理解ZBrush中的笔触
  8. VC++ 借助 Win32 API 绘图实现基本的细胞自动机演示
  9. SpringBoot 获取客户端 ip
  10. 紫书 习题8-12 UVa 1153(贪心)