Ghostbusters(并查集,最小生成树)
Ghostbusters
时间限制: 1 Sec 内存限制: 128 MB
提交: 33 解决: 7
[提交] [状态] [讨论版] [命题人:admin]
题目描述
A computer keyboard is an array of M rows and N columns of buttons. Every button has an associated probability. Furthermore, every column and every row of buttons has an associated cable, and every pressed button connects their row cable with their column cable (and vice versa!). The keyboard detects key presses by “sampling”. It sends an electric signal through the first row. This signal spreads to columns that are connected to it through pressed buttons
on that column and to rows connected to these columns through other pressed buttons and so on. Every row or column that is connected, possibly indirectly, to the original row via pressed buttons receives the signal. The firmware stores which columns have received the signal. This process is repeated for every row.
It is easy to identify what was pressed if only one key was pressed. In this case only one pair (row, column) will make contact. But keyboards allow to press more than one key at the same time and unfortunately some combinations of key presses are impossible to tell apart.
This phenomenon is called “ghosting”. For example, in a 2 × 2 keyboard, all combinations of three or four presses are impossible to tell apart, since every pair (row, column) makes electric contact (maybe indirectly), as can be seen in Figure 3.

Figure 3: Four examples of connected wires in a keyboard. Bold lines of the same colour indicate wires that are connected via pressed buttons, which are depicted as red dots. The two sets of pressed buttons on the right cannot be distinguished from each other, since they connect the same rows and columns.
The BAPC wants to deal with the problem of ghosting by finding the most likely combination of pressed keys that could have produced a particular set of signals.
输入
• A line containing two integers, M the number of rows of the keyboard and N the number of columns, with 1 ≤ M, N ≤ 500.
• M lines with N numbers each, where the jth number in the ith line indicates the probability 0 < p < 0.5 that the key in row i and column j is pressed. Here 0 ≤ i ≤ M − 1 and 0 ≤ j ≤ N − 1.
• M lines, each with an integer 0 ≤ k ≤ N and a list of k integers. The list of integers on the ith line indicates the columns that received the signal emitted by the ith row.
输出
样例输入
2 2
0.1 0.4
0.4 0.4
2 0 1
2 0 1
样例输出
0 1
1 0
1 1
思路:题意晦涩难懂,读明白就非常简单!
AC代码:
#include <bits/stdc++.h>
using namespace std;
struct UnionFind
{
vector<int> par,ra,si;
int c;
UnionFind(int n):par(n),ra(n,),si(n,),c(n)
{
for(int i=;i<n;++i) par[i]=i;
}
int findd(int i)
{
return (par[i]==i?i:(par[i]=findd(par[i])));
}
bool same(int i,int j)
{
return findd(i)==findd(j);
}
int get_size(int i)
{
return si[findd(i)];
}
int countt()
{
return c;
}
void merg(int i, int j)
{
if((i=findd(i))==(j=findd(j))) return;
c--;
if(ra[i]>ra[j]) swap(i,j);
par[i]=j;
si[j]+=si[i];
if(ra[i]==ra[j]) ra[j]++;
}
};
struct prob
{
double p;
int r,c;
};
bool cmp(const prob &l, const prob &r)
{
return l.p>r.p;
}
bool super_cmp(const prob &l,const prob &r)
{
return tie(l.r,l.c)<tie(r.r,r.c);
}
int main()
{
int m,n;
scanf("%d %d",&m,&n);
vector<prob> ps;
ps.reserve(m*n);
for(int r=;r<m;++r)
{
for(int c=;c<n;++c)
{
prob p{,r,c};
scanf("%lf",&p.p);
ps.push_back(p);
}
}
UnionFind target(m+n),cur(m+n);
for(int r=;r<m;++r)
{
int k,c;
scanf("%d",&k);
while(k--) scanf("%d",&c),target.merg(r,m+c);
}
sort(ps.begin(),ps.end(),cmp);
vector<prob> ans;
for(auto &p:ps)
{
if(target.same(p.r,m+p.c) && !cur.same(p.r,m+p.c))
{
cur.merg(p.r,m+p.c),ans.push_back(p);
}
}
sort(ans.begin(),ans.end(),super_cmp);
for(auto &x:ans) printf("%d %d\n",x.r,x.c);
return ;
}
Ghostbusters(并查集,最小生成树)的更多相关文章
- 并查集 & 最小生成树详细讲解
并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集? 并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...
- ACM: 继续畅通工程-并查集-最小生成树-解题报告
继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...
- CodeForces892E 可撤销并查集/最小生成树
http://codeforces.com/problemset/problem/892/E 题意:给出一个 n 个点 m 条边的无向图,每条边有边权,共 Q 次询问,每次给出 ki 条边,问这些边 ...
- hdu 1863 畅通工程 (并查集+最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others) M ...
- CodeForces - 891C: Envy(可撤销的并查集&最小生成树)
For a connected undirected weighted graph G, MST (minimum spanning tree) is a subgraph of G that con ...
- ACM : Travel-并查集-最小生成树 + 离线-解题报告
Travel Time Limit:1000MS Memory Limit:131072KB 64bit IO Format:%I64d & %I64u /*题意 给出n[节点 ...
- Aizu-2224Save your cats并查集+最小生成树
Save your cats 题意:存在n个点,有m条边( input中读入的是 边的端点,要先转化为边的长度 ),做一个最小生成树,使得要去除的边的长度总和最小: 思路:利用并查集和求最小生成树的方 ...
- ACM: meixiuxiu学图论-并查集-最小生成树-解题报告
/* 最小生成树,最小环的最大权值按照排序后去构建最小生成树就可以了,注意遇到的第一个根相同的点就记录权值,跳出,生成的环就是最小权值环. */ //AC代码: #include"iostr ...
- hdu1875 畅通工程再续 并查集/最小生成树
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ...
- ACM: 还是畅通工程-并查集-最小生成树-解题报
还是畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查乡村交通 ...
随机推荐
- Python学习笔记_零碎知识
1. 变量本身类型不固定的语言称之为动态语言,与之对应的是静态语言.静态语言在定义变量时必须指定变量类型,如果赋值的时候类型不匹配,就会报错. 2. Python有两种除法: /除法计算结果是浮点数, ...
- Django框架的理解和使用的常见问题
1.什么是中间件? 中间件是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出. 中间件一般做认证或批量请求处理,django中的中间 ...
- Django-3 视图层
5.1 视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片 ...
- js循环里进行回调,引用循环里的变量,发现只是最后值的问题
做项目的时候,栽在一个小地方,是这样的 我有很多个坐标点,我想把这些坐标点都绑定一个事件,当点击了这个坐标点之后,发送一个ajax 请求,将坐标点的id 发出去,等待显示返回的数据 但是实际当中,无论 ...
- windows下curl的安装和简单使用
curl是利用URL语法在命令行方式下工作的开源文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP. 一 ...
- B树和B+树对比,为什么MySQL数据库索引选择使用B+树?
一 基础知识 二叉树 根节点,第一层的节点 叶子节点,没有子节点的节点. 非叶子节点,有子节点的节点,根节点也是非叶子节点. B树 B树的节点为关键字和相应的数据(索引等) B+树 B+树是B树的一个 ...
- gdb调试汇总
1. 启动GDB开始调试: (1)gdb program ///最常用的用gdb启动程序,开始调试的方式(2)gdb program core ///用gdb查看core dump文件,跟踪程序cor ...
- node搭环境(三)之安装vue-devtools
前面已经安装了bower gulp,马上要学vue了,今天安装vue及调试神器vue-devtools 安装步骤: 1.在GitHub上输入 github.com/vuejs/vue-devtool后 ...
- C#语言开发规范
1. 命名规范 a) 类 [规则1-1]使用Pascal规则命名类名,即首字母要大写. eg: Class Test { ... } [规则1-2]使用能够反映类功能的名词或名词短语命名类. [规则 ...
- StringMVC入门案例
1.首先要引入jar包 <!--引入beans节点--> <dependency> <groupId>org.springframework</groupId ...