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的更多相关文章

  1. HDU 2419 Boring Game(并查集+map)

    感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...

  2. hdu 4961 Boring Sum(高效)

    pid=4961" target="_blank" style="">题目链接:hdu 4961 Boring Sum 题目大意:给定ai数组; ...

  3. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  4. 后缀数组 --- HDU 3518 Boring counting

    Boring counting Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3518 Mean: 给你一个字符串,求:至少出 ...

  5. hdu 4961 Boring Sum(数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting ...

  6. HDU 3518 Boring counting

    题目:Boring counting 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3518 题意:给一个字符串,问有多少子串出现过两次以上,重叠不能算两次 ...

  7. 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 ...

  8. HDU - 4358 Boring counting (dsu on tree)

    Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意: 求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数. 思 ...

  9. hdu 4358 Boring counting dfs序+莫队+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  10. hdu 4961 Boring Sum

    Boring Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

随机推荐

  1. log4j的基本使用方法

    本文转载自http://www.codeceo.com/article/log4j-usage.html 一.基本用法 1.log4j需要jar包log4j-1.2.14.jar,链接: http:/ ...

  2. 展讯7731C_M Android6.0 充电指示灯实现(一)------关机充电实现【转】

    本文转载自:https://blog.csdn.net/m0_37870649/article/details/80566131 前言: 在手机充电中常常使用充电指示灯来观察手机充电状态,比如说将手机 ...

  3. input事件手机端可能会影响手写

    <input type="text" oninput="this.value=this.value.trim()"> 这样会导致手写出现问题,一个字 ...

  4. Component 父子组件关系

    我们把组件编写的代码放到构造器外部或者说单独文件 我们需要先声明一个对象,对象里就是组件的内容. var zdy = { template:`<div>Panda from China!& ...

  5. Jupyter Notebook主题字体设置及自动代码补全

    Jupyter Notebook用久了就离不开了,然而自带的主题真的不忍直视.为了视力着想,为了自己看起来舒服,于是折腾了一番..在github上发现了一个jupyter-themes工具,可以通过p ...

  6. Javascript 高级程序设计(第3版) - 第02章

    2017-05-10 更新原文: http://www.cnblogs.com/daysme 在 html 中使用 js 把js代码写在 <script type="text/java ...

  7. 前端面试总结 (转 0C°)

    前端面试总结 1.一些开放性题目 1.自我介绍:除了基本个人信息以外,面试官更想听的是你与众不同的地方和你的优势. 2.项目介绍 3.如何看待前端开发? 4.平时是如何学习前端开发的? 5.未来三到五 ...

  8. 【搬运工】——Java中的static关键字解析(转)

    原文链接:http://www.cnblogs.com/dolphin0520/p/3799052.html static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大 ...

  9. 给 layui upload 带每个文件的进度条, .net 后台代码

    1.upload.js 扩展 功能利用ajax的xhr属性实现该功能修改过modules中的upload.js文件功能具体实现:在js文件中添加监听函数 //创建监听函数 var xhrOnProgr ...

  10. Vue项目中如何使用Element-UI以及如何使用sass

    Vue项目中如何使用Element-UI以及如何使用sass 当我们在开发Vue项目的时候通常会选择Element-UI作为我们的UI框架,其官方中文文档地址是http://element.eleme ...