hdu-5861 Road(并查集)
题目链接:
Road
Time Limit: 12000/6000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
We know the transport plan in the next m days, each day there is one cargo need to transport from village ai to village bi, and you need to guarantee that the segments between ai and bi are open in the i-th day. Your boss wants to minimize the total cost of the next m days, and you need to tell him the charge for each day.
(At the beginning, all the segments are closed.)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+20;
const int maxn=1e4+220;
const double eps=1e-12; int n,m,w[N],a[N],b[N],p[N];
int x,y;
vector<int>st[N],en[N];
int findset(int x)
{
if(x==p[x])return x;
return p[x]=findset(p[x]);
}
int same(int x,int y)
{
int fx=findset(x),fy=findset(y);
if(fx>fy)p[fy]=fx;
else if(fx<fy)p[fx]=fy;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
For(i,1,n-1)read(w[i]);
int high=max(n,m)+10;
for(int i=0;i<=high;i++)
{
p[i]=i;
st[i].clear();
en[i].clear();
}
For(i,1,m)
{
read(a[i]);read(b[i]);
if(a[i]>b[i])swap(a[i],b[i]);
int r=a[i];
while(1)
{
r=findset(r);
if(r>=b[i])break;
same(r,r+1);
st[i].push_back(r);
}
}
for(int i=0;i<=high;i++)p[i]=i;
for(int i=m;i>0;i--)//找到第i天要关闭的线段
{
int r=a[i];
while(1)
{
r=findset(r);
if(r>=b[i])break;
same(r,r+1);
en[i].push_back(r);
}
}
int ans=0;
for(int i=1;i<=m;i++)
{
int len=st[i].size();
for(int j=0;j<len;j++)//加上这一天要打开的线段
{
int x=st[i][j];
ans+=w[x];
}
print(ans);
len=en[i].size();
for(int j=0;j<len;j++)//关闭这天需要关闭的线段;
{
int x=en[i][j];
ans-=w[x];
}
}
}
return 0;
}
hdu-5861 Road(并查集)的更多相关文章
- hdu 5861 Road 两棵线段树
传送门:hdu 5861 Road 题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路 每条路只能开放或关闭一次. ( ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- hdu 1598 find the most comfortable road (并查集)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 2818 (矢量并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...
- hdu 1116 欧拉回路+并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...
- Bipartite Graph hdu 5313 bitset 并查集 二分图
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset ...
- HDU 5861 Road 线段树区间更新单点查询
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5861 Road Time Limit: 12000/6000 MS (Java/Othe ...
- hdu 3081(二分+并查集+最大流||二分图匹配)
Marriage Match II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...
随机推荐
- SPSS统计功能与模块对照表
SPSS统计功能 - 应用速查表第一列为统计方法,中间为统计功能,最后一列为所在模块 1 ANOVA Models(单因素方差分析:简单因子) : 摘要 描述 方差 轮廓 - SPSS Base 2 ...
- unity回调函数范例
using System.Collections; using System.Collections.Generic; using UnityEngine; public class callback ...
- ios 手势返回<1>2
iOS-给push出来的控制器添加全局滑动(返回)手势 在iOS中,当我们push出一个新的控制器的时候,我们可以向右拖拽屏幕的左边缘来返回(pop)到上一级控制器,但是这个功能有两个缺陷: 当自 ...
- C# 关于类型转换 面试题
分别分析一下两种写法是否正确.假设不对.请说明原因 写法一: short s=1; s = s + 1; 写法二: short s=1; s += 1; 解答: 写法一不对,会报出以下的错误: 无法将 ...
- CAFFE学习笔记(一)Caffe_Example之训练mnist
0.参考文献 [1]caffe官网<Training LeNet on MNIST with Caffe>; [2]薛开宇<读书笔记4学习搭建自己的网络MNIST在caffe上进行训 ...
- Black And White(DFS+剪枝)
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others ...
- Python中的TCP编程,实现客户端与服务器的聊天(socket)
参考大神blog:自己再写一个 https://blog.csdn.net/qq_31187881/article/details/79067644
- java高级主题
1 java.util.concurrent.locks.LockSupport park:阻塞线程. unpark:解除阻塞线程. 线程阻塞最基础的组件. 2 sun.misc.Unsafe 可以用 ...
- Netty 源码(ChannelHandler 死磕)
精进篇:netty源码死磕5 - 揭开 ChannelHandler 的神秘面纱 目录 1. 前言 2. Handler在经典Reactor中的角色 3. Handler在Netty中的坐标位置 4 ...
- nginx搭建服务
1.当然首先是安装nginx :http://nginx.org/en/download.html 2.一波安装之后 3.在nginx 文件夹打开命令行:cmd 4.开启nginx 服务命令:star ...