hdu-2419 Boring Game
http://acm.hdu.edu.cn/showproblem.php?pid=2419
给一个图,预分配点值。随后有三种操作,F u v查询与u联通部分大于等于v的最小的数,没有则返回0,U u v更新u的值为v,E u v删除u-v的边。
联通块可以用并查集解决,但是删边无法处理。因为没有加边,我们可以把整个操作过程反过来进行,就变成只有加边没有删边了。期间仔细维护各个值就好。
涉及边的删除,所以用set存边。查找最值可以用multi_set的lower_bound。并查集涉及集合合并,可以用s[a].insert(b.begin(),b.end()),实测效率和遍历b并Insert到a中几乎没差别,切记要让小集合向大集合合并!!!
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL int
using namespace std;
const LL N = ;
multiset<int> g[N];
int n, m, q;
LL val[N];
struct cmd
{
char e;
LL k, v;
cmd(char ee, LL kk, LL vv)
{
e = ee;
k = kk;
v = vv;
}
cmd()
{ }
};
LL pre[N];
multiset<LL> s[N];
LL Find(LL x)
{
if (pre[x] == x)return x;
return pre[x]=Find(pre[x]);
}
void mix(LL t1, LL t2)
{
LL pt1 = Find(t1), pt2 = Find(t2);
if (pt1==pt2)return;
if (s[pt1].size() > s[pt2].size()) swap(pt1, pt2);
pre[pt1] = pt2;
s[pt2].insert(s[pt1].begin(), s[pt1].end());
//for (multiset<LL>::iterator it = s[pt1].begin(); it != s[pt1].end(); it++)
//s[pt2].insert(*it);
s[pt1].clear();
}
void upt(LL nod, LL v)
{
LL pt = Find(nod);
//cout << pt << endl;
s[pt].erase(s[pt].find(val[nod]));
val[nod] = v;
s[pt].insert(v);
}
LL fd(LL nod, LL v)
{
LL pt = Find(nod);
multiset<LL>::iterator it= s[pt].lower_bound(v);
if (it == s[pt].end())return ;
return *it;
}
cmd v[N * ];
int main() {
int cas = ;
while (scanf("%d%d%d",&n,&m,&q)!=EOF)
{
for (int i = ; i <= n; i++)
{
LL num;
s[i].clear();
g[i].clear();
scanf("%d", &num);
val[i] = num;
pre[i] = i;
}
for (int i = ; i <= m; i++)
{
int fr, to;
scanf("%d%d", &fr, &to);
g[min(fr,to)].insert(max(fr,to));
//g[to].insert(fr);
}
LL siz = ;
while (q--)
{
char c[];
LL t1, t2;
scanf("%s %d %d", c, &t1, &t2);
if (c[] == 'E')
{
//g[t1].erase(t2);
g[min(t1,t2)].erase(g[min(t1,t2)].find(max(t1,t2)));
}
if (c[] == 'U')
{
LL temp = val[t1];
val[t1] = t2;
t2 = temp;
}
v[siz++]=cmd(c[], t1, t2);
}
for (int i = ; i <= n; i++)s[i].insert(val[i]);
for (int i = ; i <= n; i++)
{
for (multiset<int>::iterator j = g[i].begin(); j != g[i].end(); j++)
{
int e = *j;
mix(i, e);
}
}
LL cnt = ;
double ans = ;
for (int i = siz-; i >=; i--)
{
cmd e = v[i];
//cout << e.e << ": " << e.k <<' '<< e.v << endl;
if (e.e == 'E')
{
mix(e.k, e.v);
}
if (e.e == 'U')
{
upt(e.k, e.v);
}
if (e.e == 'F')
{
ans += fd(e.k, e.v);
//cout << fd(e.k, e.v) << endl;
cnt++;
}
}
printf("Case %d: %.3f\n", cas++,ans*1.0 / cnt);
} return ;
}
hdu-2419 Boring Game的更多相关文章
- HDU 2419 Boring Game(并查集+map)
感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...
- hdu 4961 Boring Sum(高效)
pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- 后缀数组 --- HDU 3518 Boring counting
Boring counting Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...
- hdu 4961 Boring Sum(数学题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...
- HDU 3518 Boring counting
题目:Boring counting 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3518 题意:给一个字符串,问有多少子串出现过两次以上,重叠不能算两次 ...
- 2015 Multi-University Training Contest 3 hdu 5324 Boring Class
Boring Class Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- HDU - 4358 Boring counting (dsu on tree)
Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意: 求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数. 思 ...
- hdu 4358 Boring counting dfs序+莫队+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- hdu 4961 Boring Sum
Boring Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tota ...
随机推荐
- SpringBoot cookie工具类
code: import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.annot ...
- 如何卸载旧版本的dotnet core
How to remove the .NET Core Runtime and SDK https://docs.microsoft.com/en-us/dotnet/core/versions/re ...
- Vue学习五:v-for指令使用方法
本文为博主原创,未经允许不得转载: <!DOCTYPE html> <html lang="zh"> <head> <meta http- ...
- windows 上让文件类型和程序关联的批处理程序。
文件关联工具 地址: https://github.com/wll8/assoc-tool 本工具可以用来为你的便携程序添加文件关联,比如 nodepad2.exe . vscode 或其他图片处理程 ...
- js转义和反转义html
本文地址: http://www.cnblogs.com/daysme/p/7100553.html 下面的代码网上常用有,但不是想要的. JS实现HTML标签转义及反转义 http://blog.c ...
- SAP应用创新-维护控制表、视图统一路径
SAP应用创新-维护控制表.视图统一路径 背景: Sap 里面通过技术支持人员维护表的值控制业务操作的表不少,一般通过事物代码或记录在系统外的文档或在某个程序上放置一个按钮.缺点:分散,不易记,不好找 ...
- C# 委托例子
两个子窗口向一个主窗口发送信息 主窗口: using System; using System.Collections.Generic; using System.ComponentModel; us ...
- 理解ffmpeg中的pts,dts,time_base
首先介绍下概念: PTS:Presentation Time Stamp.PTS主要用于度量解码后的视频帧什么时候被显示出来 DTS:Decode Time Stamp.DTS主要是标识读入内存中的b ...
- 转一个集成速锐的ss 回头试试 补充加速一、Vultr安装锐速
https://liyuans.com/archives/ssr-serverspeeder-onekey.html Debian/Ubuntu 系统 ShadowsocksR 一键安装脚本 (集成锐 ...
- VS IIS Express 支持局域网访问
使用Visual Studio开发Web网页的时候有这样的情况:想要在调试模式下让局域网的其他设备进行访问,以便进行测试.虽然可以部署到服务器中,但是却无法进行调试,就算是注入进程进行调试也是无法达到 ...