Ghostbusters(并查集,最小生成树)
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.
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.
• 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代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 struct UnionFind 4 { 5 vector<int> par,ra,si; 6 int c; 7 UnionFind(int n):par(n),ra(n,0),si(n,1),c(n) 8 { 9 for(int i=0;i<n;++i) par[i]=i; 10 } 11 int findd(int i) 12 { 13 return (par[i]==i?i:(par[i]=findd(par[i]))); 14 } 15 bool same(int i,int j) 16 { 17 return findd(i)==findd(j); 18 } 19 int get_size(int i) 20 { 21 return si[findd(i)]; 22 } 23 int countt() 24 { 25 return c; 26 } 27 void merg(int i, int j) 28 { 29 if((i=findd(i))==(j=findd(j))) return; 30 c--; 31 if(ra[i]>ra[j]) swap(i,j); 32 par[i]=j; 33 si[j]+=si[i]; 34 if(ra[i]==ra[j]) ra[j]++; 35 } 36 }; 37 struct prob 38 { 39 double p; 40 int r,c; 41 }; 42 bool cmp(const prob &l, const prob &r) 43 { 44 return l.p>r.p; 45 } 46 bool super_cmp(const prob &l,const prob &r) 47 { 48 return tie(l.r,l.c)<tie(r.r,r.c); 49 } 50 int main() 51 { 52 int m,n; 53 scanf("%d %d",&m,&n); 54 vector<prob> ps; 55 ps.reserve(m*n); 56 for(int r=0;r<m;++r) 57 { 58 for(int c=0;c<n;++c) 59 { 60 prob p{0,r,c}; 61 scanf("%lf",&p.p); 62 ps.push_back(p); 63 } 64 } 65 UnionFind target(m+n),cur(m+n); 66 for(int r=0;r<m;++r) 67 { 68 int k,c; 69 scanf("%d",&k); 70 while(k--) scanf("%d",&c),target.merg(r,m+c); 71 } 72 sort(ps.begin(),ps.end(),cmp); 73 vector<prob> ans; 74 for(auto &p:ps) 75 { 76 if(target.same(p.r,m+p.c) && !cur.same(p.r,m+p.c)) 77 { 78 cur.merg(p.r,m+p.c),ans.push_back(p); 79 } 80 } 81 sort(ans.begin(),ans.end(),super_cmp); 82 for(auto &x:ans) printf("%d %d\n",x.r,x.c); 83 return 0; 84 }