题目链接:http://poj.org/problem?id=2831

题意:

  给你一个图,每条边有边权。

  然后有q组询问(i,x),问你如果将第i条边的边权改为x,这条边是否有可能在新的最小生成树中。

题解:

  更改边权相当于新添加了一条边。

  新边在新MST中的充要条件是:

    加入新边后,在原来的MST上形成的环中,有一条旧边的边权>=x。

    (因为如果这样的话,新边可以替换掉那条最大的边)

  所以可以预处理出 maxn[i][j]:在原来的MST上,任意两点间路径上的最大边权。

  dfs即可。

  对于每一个新访问到的节点i,枚举每一个已访问过的节点j,那么:

    maxn[i][j] = maxn[j][i] = max(maxn[j][fa[i]], v[fa[i]][i])

  复杂度O(N^2)。

AC Code:

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define MAX_N 1005
#define MAX_M 100005 using namespace std; struct E
{
int s;
int t;
int len;
E(int _s,int _t,int _len)
{
s=_s;
t=_t;
len=_len;
}
E(){}
friend bool operator < (const E &a,const E &b)
{
return a.len<b.len;
}
}; struct Edge
{
int dest;
int len;
Edge(int _dest,int _len)
{
dest=_dest;
len=_len;
}
Edge(){}
}; int n,m,q;
int s[MAX_M];
int t[MAX_M];
int v[MAX_M];
int par[MAX_N];
int maxn[MAX_N][MAX_N];
bool vis[MAX_N];
vector<E> e;
vector<Edge> edge[MAX_N]; void read()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&s[i],&t[i],&v[i]);
e.push_back(E(s[i],t[i],v[i]));
}
} void init_union_find()
{
for(int i=;i<=n;i++)
{
par[i]=i;
}
} int find(int x)
{
return par[x]==x ? x : par[x]=find(par[x]);
} void unite(int x,int y)
{
int px=find(x);
int py=find(y);
if(px==py) return;
par[px]=py;
} bool same(int x,int y)
{
return find(x)==find(y);
} int kruskal()
{
init_union_find();
sort(e.begin(),e.end());
int cnt=;
int res=;
for(int i=;i<e.size();i++)
{
E temp=e[i];
if(!same(temp.s,temp.t))
{
cnt++;
res+=temp.len;
unite(temp.s,temp.t);
edge[temp.s].push_back(Edge(temp.t,temp.len));
edge[temp.t].push_back(Edge(temp.s,temp.len));
}
}
return cnt==n- ? res : -;
} void dfs(int now)
{
vis[now]=true;
for(int i=;i<edge[now].size();i++)
{
Edge temp=edge[now][i];
if(!vis[temp.dest])
{
for(int j=;j<=n;j++)
{
if(vis[j])
{
maxn[j][temp.dest]=maxn[temp.dest][j]=max(maxn[j][now],temp.len);
}
}
dfs(temp.dest);
}
}
} void work()
{
kruskal();
memset(maxn,,sizeof(maxn));
memset(vis,false,sizeof(vis));
dfs();
int i,x;
while(q--)
{
scanf("%d%d",&i,&x);
if(x<=maxn[s[i]][t[i]]) printf("Yes\n");
else printf("No\n");
}
} int main()
{
read();
work();
}

POJ 2831 Can We Build This One:次小生成树【N^2预处理】的更多相关文章

  1. POJ 2831 Can We Build This One?

    Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1728   Accepted: 643 Case Time Limit: 2 ...

  2. 【POJ 1679】The Unique MST(次小生成树)

    找出最小生成树,同时用Max[i][j]记录i到j的唯一路径上最大边权.然后用不在最小生成树里的边i-j来替换,看看是否差值为0. #include <algorithm> #includ ...

  3. POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 D ...

  4. POJ 1679 The Unique MST 【最小生成树/次小生成树模板】

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 D ...

  5. POJ 1679 The Unique MST (次小生成树)

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

  6. poj 1679 The Unique MST 【次小生成树】【模板】

    题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...

  7. poj 1679 The Unique MST 【次小生成树+100的小数据量】

    题目地址:http://poj.org/problem?id=1679 2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2 Sample Outpu ...

  8. POJ 1679 The Unique MST:次小生成树【倍增】

    题目链接:http://poj.org/problem?id=1679 题意: 给你一个图,问你这个图的最小生成树是否唯一. 题解: 求这个图的最小生成树和次小生成树.如果相等,则说明不唯一. 次小生 ...

  9. POJ(2784)Buy or Build

    Buy or Build Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1369   Accepted: 542 Descr ...

随机推荐

  1. Unity3D 之IAP

    本人是一个Unity忠实爱好者,鉴于网上关于Unity的内置付费教程 少之甚少,本人就把自己倒腾过的IAp分享出来,仅供大家参考.一.搭建号沙盒环境( 详细请看:http://xiaominghimi ...

  2. 启动 ./spark-shell 命令报错

    当使用./spark-shell 命令报错 Caused by: ERROR XJ040: Failed to start database @476fde05, see the next excep ...

  3. [译]GLUT教程 - 笔划字体

    Lighthouse3d.com >> GLUT Tutorial >> Fonts >> Stroke Fonts 笔划字体是用线条生成的.跟位图字体相反,笔划字 ...

  4. dedecms织梦如何调用指定的多个栏目导航

    {dede:channelartlist row='2' typeid='1,2这里输入多个指定的栏目ID' } <li><a href='{dede:field name='typ ...

  5. jQuery效果之显示与隐藏

    .hide([duration][,complete])--目标元素的隐藏. .show([duration][,complete])--目标元素的显示: .toggle([duration][,co ...

  6. python 微信跳一跳和源码解读

    刚好周末,想研究一下前阵子很火的微信跳一跳 下面进入正文. 本文适用对象为WIN10系统,安卓用户.目的在于让丝毫没有接触过Python的小伙伴都能成功运行,如果你恰好是这样的对象,那么跟着我开始操作 ...

  7. 大数据学习系列(5)-- 局域网yum仓库搭建

    https://www.cnblogs.com/nulige/p/6081192.html

  8. jar -cmf file1 file2 file3命令

    jar -cmf file1 file2 file3中的参数c.m.f和file1.file2.file3是一一对应的. 也就是说,file1是输出的.jar文件,file2是往META-INF/MA ...

  9. Python菜鸟之路:Python基础(三)

    一.编码 推荐阅读<字符编码的前世今生>:http://tgideas.qq.com/webplat/info/news_version3/804/808/811/m579/201307/ ...

  10. Python字典的入门案例

    查看python版本: [root@localhost ~]# python -V Python 2.7.5 1.基本的字典操作 案例1:简单电话本实现 [root@localhost ~]# vim ...