Ghostbusters

时间限制: 1 Sec  内存限制: 128 MB
提交: 33  解决: 7
[提交] [状态] [讨论版] [命题人:admin]

题目描述

The Bureau of Approved Peripherals for Computers (BAPC) is designing a new standard for computer keyboards. With every new norm and regulation, hardware becomes obsolete easily, so they require your services to write firmware for them.
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.

输入

The input consists of
• 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.

输出

Output the set of pressed keys that is most likely given the input. Any solution that achieves the maximum probability will be accepted. For each pressed key output a line with two integers r and c, separated by a space, indicating the row r and the column c of the key. The lines must be outputted in lexicographical order, that is, output first the keys whose row is lower and if the rows are the same output first the key whose column is lower.

样例输入

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(并查集,最小生成树)的更多相关文章

  1. 并查集 & 最小生成树详细讲解

    并查集 & 最小生成树 并查集 Disjoint Sets 什么是并查集?     并查集,在一些有N个元素的集合应用问题中,我们通常是在开始时让每个元素构成一个单元素的集合,然后按一定顺序将 ...

  2. ACM: 继续畅通工程-并查集-最小生成树-解题报告

    继续畅通工程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Descri ...

  3. CodeForces892E 可撤销并查集/最小生成树

    http://codeforces.com/problemset/problem/892/E 题意:给出一个 n 个点 m 条边的无向图,每条边有边权,共 Q 次询问,每次给出 ki​ 条边,问这些边 ...

  4. hdu 1863 畅通工程 (并查集+最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others)    M ...

  5. CodeForces - 891C: Envy(可撤销的并查集&最小生成树)

    For a connected undirected weighted graph G, MST (minimum spanning tree) is a subgraph of G that con ...

  6. ACM : Travel-并查集-最小生成树 + 离线-解题报告

    Travel Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u /*题意 给出n[节点 ...

  7. Aizu-2224Save your cats并查集+最小生成树

    Save your cats 题意:存在n个点,有m条边( input中读入的是 边的端点,要先转化为边的长度 ),做一个最小生成树,使得要去除的边的长度总和最小: 思路:利用并查集和求最小生成树的方 ...

  8. ACM: meixiuxiu学图论-并查集-最小生成树-解题报告

    /* 最小生成树,最小环的最大权值按照排序后去构建最小生成树就可以了,注意遇到的第一个根相同的点就记录权值,跳出,生成的环就是最小权值环. */ //AC代码: #include"iostr ...

  9. hdu1875 畅通工程再续 并查集/最小生成树

    相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ...

  10. ACM: 还是畅通工程-并查集-最小生成树-解题报

    还是畅通工程 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 某省调查乡村交通 ...

随机推荐

  1. Autel MaxiSys Pro MS908P

    Autel MaxiSys pro MS908P is an evolutionary smart solution for specialized automotive diagnosis and ...

  2. STlink及烧写工具:

    一 STLINK可以随时在STLINK与jlink之间切换:方法:https://www.segger.com/products/debug-probes/j-link/models/other-j- ...

  3. FIR IIR数字滤波器特点简介

    FIR:有限脉冲滤波器,线性较好,用非递归算法,可用FFT,计算速度快,不用四舍五入,适合于对相位敏感的线性应用场合,设计灵活没有现成公式可用. 输出与现在和过去的输入有关. IIR:无限脉冲滤波器, ...

  4. java lamda

    // 查询数据模拟 List<SealListViewVo> list = new ArrayList<SealListViewVo>(); for (int i = 0; i ...

  5. 设置webstorm的file watch 监视scss文件

    参考:http://blog.founddrama.net/2013/04/watching-compass-files-in-webstorm/ 上面红色划线部分. 特别注意arguments: 像 ...

  6. PHP算法——生成唯一字符串

    经常遇到忘记密码的情况,点击一下忘记密码,然后收到更改密码的链接,连接中往往都会有一段很长而且很乱的字符串.试想一下,如果出现了重复的字符串,那岂不是把别人的密码给重置了? 所以产生唯一数对于网站的安 ...

  7. python 字符串转字节数组

    场景: java加解密和python加解密互转的时候,因一些非显示字符无法确认两者是否一致,故需要打出他们的十六进制字节数组进行比较 1.python代码实现 str='123';print str. ...

  8. Qmake 配置自定义编译过程

    Qmake 配置自定义编译过程 需求:动态更换资源文件 在 Windows10 下编写 Qt 项目时,有这样的需求: 程序用到的资源文件可以动态更换而不需要重新编译整个项目 解决方案 0.1 将所有的 ...

  9. 【Ionic】---Using Local Notifications In Your Ionic Framework App

    Using Local Notifications In Your Ionic Framework App 配置好ng-cordova先 <script src="lib/ngCord ...

  10. typedef struct 与 struct

    学c++之前最好先学c.特别要说的是,一些虽然冠名为c++的项目的文件中却大部分都是c的代码. 比如我们这个例子: 在c语言中,定义一个结构体和其实适合c++中有区别的.比如我们有如下的代码: str ...