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 ...
随机推荐
- 求最大流dinic算法模板
//最短增广路,Dinic算法 struct Edge { int from,to,cap,flow; };//弧度 void AddEdge(int from,int to,int cap) //增 ...
- linux内核中的cfq输入输出调度算法
1. 全称是什么? 完全公平调度算法(completely fair queuing) 2. 原理是怎样的? 先按照输入输出请求的地址进行排序,然后按排好的次序执行请求 3. 适用场景 适用于旋转式磁 ...
- 【做题】apc001_f-XOR Tree——巧妙转化及dp
对树上的路径进行操作是十分难处理的事情.一开始的思路主要针对于\(a_i<=15\)这一特殊性质上.于是考虑了\(a_i<=1\)的情况,然而除了糊出一个适用范围极小的结论外,并没有什么用 ...
- (zhuan) Using convolutional neural nets to detect facial keypoints tutorial
Using convolutional neural nets to detect facial keypoints tutorial this blog from: http://danieln ...
- 清除浏览器CSS样式
/* YUI 3.18.1 (build f7e7bcb) Copyright 2014 Yahoo! Inc. All rights reserved. Licensed under the BSD ...
- C# linq 最大、最小对象的扩展
public static class LinqExtension { public static T MaxBy<T, TR>(this IEnumerable<T> en, ...
- 【Django】Django-REST-Framework
[创建简单的API] 1. cmd.exe >django-admin startproject django_rest>cd django_rest\django_rest>pyt ...
- SSH KEY 设置 目录在open ~ 根目录下的.ssh 里面
当我们从github或者gitlab上clone项目或者参与项目时,需要证明我们的身份.github.gitlab支持使用SSH协议进行免密登录,而SSH协议采用了RSA算法保证了登录的安全性.我们要 ...
- netstat 在windows下和Linux下查看网络连接和端口占用
假设忽然起个服务,告诉我8080端口被占用了,OK,我要去看一下是什么服务正在占用着,能不能杀 先假设我是在Windows下: 第一列: Proto 协议 第二列: 本地地址[ip+端口] 第三列:远 ...
- MySQL中字符串和数字拼接
select * from qa_employ where EMPLOY_GROUP =2 原先雇佣表中所有雇佣姓名全部是"张三", 希望将雇用姓名变得不一样,比如张三+id SQ ...