POJ 2831 Can We Build This One:次小生成树【N^2预处理】
题目链接: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预处理】的更多相关文章
- POJ 2831 Can We Build This One?
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1728 Accepted: 643 Case Time Limit: 2 ...
- 【POJ 1679】The Unique MST(次小生成树)
找出最小生成树,同时用Max[i][j]记录i到j的唯一路径上最大边权.然后用不在最小生成树里的边i-j来替换,看看是否差值为0. #include <algorithm> #includ ...
- POJ 1679:The Unique MST(次小生成树&&Kruskal)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19941 Accepted: 6999 D ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ 1679 The Unique MST (次小生成树)
题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...
- poj 1679 The Unique MST 【次小生成树】【模板】
题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后加入 ...
- 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 ...
- POJ 1679 The Unique MST:次小生成树【倍增】
题目链接:http://poj.org/problem?id=1679 题意: 给你一个图,问你这个图的最小生成树是否唯一. 题解: 求这个图的最小生成树和次小生成树.如果相等,则说明不唯一. 次小生 ...
- POJ(2784)Buy or Build
Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1369 Accepted: 542 Descr ...
随机推荐
- iOS tableView高度缓存
tableView计算完高度后,把高度缓存起来,避免下次重复计算,以减少不必要的消耗 // declare cellHeightsDictionary NSMutableDictionary *cel ...
- openWRT自学---对官方的开发指导文档的解读和理解 记录2:如何控制内核模块的编译
openwrt对于kernel module的处理分两类:随内核主线而来的kernel module 和 其他作为独立project的kernel module.而这两种,openwrt将采用相同的模 ...
- UVA 699 The Falling Leaves (二叉树水题)
本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...
- Web客户端语言HTML、XHTML和XML相关知识介绍
HTML简介 HTML(Hyper Text Mark-up Language)即超文本标记语言或超文本链接标示语言,是目前网络上应用最为广泛的语言,也是构成网页文档的主要语言.HTML文本是由HTM ...
- 石子合并DP
DP Time Limit:3000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Status Pra ...
- 论JavaWeb前后端分离放弃jsp
1.静态资源使用Nginx反向代理Tomcat,Tomcat挂了网站仍可访问.2.静态与后端服务器分离,提升性能.3.大并发情况下,可同时扩展前后端服务器.4.接口可复用至App相关服务.5.网站热部 ...
- 九度OJ 1340:小A的计算器 (进制转换)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:735 解决:202 题目描述: 以往的操作系统内部的数据表示都是二进制方式,小A新写了一个操作系统,系统内部的数据表示为26进制,其中0-2 ...
- 九度OJ 1262:Sequence Construction puzzles(I)_构造全递增序列 (DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:118 解决:54 题目描述: 给定一个整数序列,请问如何去掉最少的元素使得原序列变成一个全递增的序列. 输入: 输入的第一行包括一个整数N( ...
- CoreMotion 加速器陀螺仪
初始化CoreMotion #import <CoreMotion/CoreMotion.h> CMMotionManager *motionManager = [[CMMotionMan ...
- Go 学习笔记
官网: https://golang.org/ 环境: $GOROOT: GOROOT环境变量指定了Go的安装目录. $GOPATH: GOPATH 环境变量指定workspace的目录. 命令行: ...