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

    If you're running on Windows it is good to run Jenkins as a service so it starts up automatically wi ...

  2. 开源代码中的autogen.sh干了些什么?

    答:重新根据当前所处环境生成新的configure 分析脚本都做了些什么? 1. 指定make工具 MAKE=gmake or MAKE=make 2. 执行autoreconf autoreconf ...

  3. tp框架中的一些疑点知识-4

    $_SERVER的几个元素: script_name脚本名称, 是指这个脚本文件本身的文件名, 通常只是 : /index.php path_info: 是从控制器/操作方法开始到最后的内容,包括路径 ...

  4. 分布式知识点总结(来自CS-Notes)

    转载地址:https://github.com/CyC2018/CS-Notes/blob/master/notes/%E5%88%86%E5%B8%83%E5%BC%8F.md 注:如Paxos等的 ...

  5. javbus爬虫-老司机你值得拥有

    # 起因 有个朋友叫我帮忙写个爬虫,爬取javbus5上面所有的详情页链接,也就是所有的https://www.javbus5.com/SRS-055这种链接, 我一看,嘿呀,这是司机的活儿啊,我绝对 ...

  6. GDOI2018D2T1 谈笑风生

    T1 谈笑风生 [题目描述] [输入] [输出] 一行两个数,所需能量P与在能量最小的前提下最短的到达时间t. [样例输入] 5 7 66 4 3 2 1 5 1 2 1 5 2 3 2 4 2 5 ...

  7. P2120 [ZJOI2007]仓库建设(dp+斜率优化)

    思路 首先暴力DP显然,可以得20分 加上一个前缀和优化,可以得到40分 然后上斜率优化 设\(sum_i\)为\(\sum_{1}^iP_i\),\(sump_i\)为\(\sum_{1}^{i}P ...

  8. Ubuntu关机时间过长,总是停在logo界面

    有时候我们总能遇到ubuntu关机的时候卡住,无法关机,一查看发现是" a stop job is running..." 然后后面接着一串等待时间. 这时候我们需要修改一下sys ...

  9. 【JS】js操作json object

    //将表单序列化成字符串 $.fn.serializeObject = function () { var obj = {}; var count = 0; $.each(this.serialize ...

  10. Ubuntu 安装 matplotlib

    参考: ubuntu16 安装matplotlib Ubuntu 安装 matplotlib sudo apt-get install libpng-dev libfreetype6-dev pkg- ...