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. Junit 的Assertions的使用

    import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.anyOf; import ...

  2. Python3基础 list in/not in 判断一个变量是否在列表中存在

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. (zhuan) Notes on Representation Learning

    this blog from: https://opendatascience.com/blog/notes-on-representation-learning-1/   Notes on Repr ...

  4. Nand flash code

    (1)流水灯   1>我们来看原理图                                2>datasheet                             3> ...

  5. Codeforces 808G Anthem of Berland(KMP+基础DP)

    题意 给定一个字符串 \(s\) ,一个字符串 \(t\) ,其中 \(s\) 包含小写字母和 "?" ,\(t\) 只包含小写字母,现在把 \(s\) 中的问号替换成任意的小写字 ...

  6. python FAE

    1.python 时间戳用localtime转换时间戳较大时报错 ValueError: timestamp out of range for platform time_t 2.python面向对象 ...

  7. OpenLayers中的球面墨卡托投影

    最近看OpenLayers,研究到地图投影时找到官方的文档,就翻译了一下,由于英文能力差,翻译不好的地方,请看原文 原文地址:http://docs.openlayers.org/library/sp ...

  8. WaitingFormHelper

    using Lba_Ciac; using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  9. 【Oracle】【问题】

    1. java.sql.SQLException: 对只转发结果集的无效操作: last 参考:https://www.cnblogs.com/gaoyuchuanIT/articles/411888 ...

  10. c# 通过反射输出成员变量以及成员变量的值

    /*** @Author rexzhao* 工具类 仅限于* public variable*/using System.Collections;using System.Collections.Ge ...