POJ3740-Easy Finding
来源:互联网 发布:淘宝外贸原单死人衣服 编辑:程序博客网 时间:2024/05/22 10:42
Easy Finding
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18660 Accepted: 5120
Description
Given a M×N matrix A. Aij ∈ {0, 1} (0 ≤ i < M, 0 ≤ j < N), could you find some rows that let every cloumn contains and only contains one 1.
Input
There are multiple cases ended by EOF. Test case up to 500.The first line of input is M, N (M ≤ 16, N ≤ 300). The next M lines every line contains N integers separated by space.
Output
For each test case, if you could find it output "Yes, I found it", otherwise output "It is impossible" per line.
Sample Input
3 30 1 00 0 11 0 04 40 0 0 11 0 0 01 1 0 10 1 0 0
Sample Output
Yes, I found itIt is impossible
Source
POJ Monthly Contest - 2009.08.23, MasterLuo
题意:给你一个01矩阵,问能不能选出一些行,使得每一列都有且仅在一行的那一列为1
解题思路:舞蹈链的精确覆盖
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cctype>#include <map>#include <cmath>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int maxn = 300005;int n, m, a[20][400];struct DLX{int L[maxn], R[maxn], U[maxn], D[maxn];int row[maxn], col[maxn], ans[maxn], sum[maxn];int n, m, num, cnt;void add(int k, int l, int r, int u, int d, int x, int y){L[k] = l; R[k] = r; U[k] = u;D[k] = d; row[k] = x; col[k] = y;}void reset(int n, int m){this->n = n; this->m = m;for (int i = 0; i <= m; i++){add(i, i - 1, i + 1, i, i, 0, i);sum[i] = 0;}L[0] = m, R[m] = 0, cnt = m + 1;}void insert(int x, int y){int temp = cnt - 1;if (row[temp] != x){add(cnt, cnt, cnt, U[y], y, x, y);U[D[cnt]] = cnt; D[U[cnt]] = cnt;}else{add(cnt, temp, R[temp], U[y], y, x, y);R[L[cnt]] = cnt; L[R[cnt]] = cnt;U[D[cnt]] = cnt; D[U[cnt]] = cnt;}sum[y]++, cnt++;}void remove(int k){R[L[k]] = R[k], L[R[k]] = L[k];for (int i = D[k]; i != k; i = D[i])for (int j = R[i]; j != i; j = R[j]){D[U[j]] = D[j];U[D[j]] = U[j];sum[col[j]]--;}}void resume(int k){R[L[k]] = k, L[R[k]] = k;for (int i = D[k]; i != k; i = D[i])for (int j = R[i]; j != i; j = R[j]){D[U[j]] = j;U[D[j]] = j;sum[col[j]]++;}}bool dfs(int k){if (!R[0]) { num = k; return true; }int now = R[0];for (int i = now; i != 0; i = R[i])if (sum[now] > sum[i]) now = i;remove(now);for (int i = D[now]; i != now; i = D[i]){ans[k] = row[i];for (int j = R[i]; j != i; j = R[j]) remove(col[j]);if (dfs(k + 1)) return true;for (int j = L[i]; j != i; j = L[j]) resume(col[j]);}resume(now);return false;}}dlx;int main(){while (~scanf("%d %d", &n, &m)){dlx.reset(n, m);for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++){scanf("%d", &a[i][j]);if (a[i][j]) dlx.insert(i, j);}if (dlx.dfs(0)) printf("Yes, I found it\n");else printf("It is impossible\n");}return 0;}
阅读全文
0 0
- poj3740 Easy Finding(深搜)
- POJ3740--Easy Finding
- poj3740 Easy Finding DLX
- POJ3740-Easy Finding
- POJ3740 Easy Finding(Dancing Links)
- POJ3740--Easy Finding(Dancing Links)
- 【POJ3740】Easy Finding,解题报告+思路+代码
- POJ3740 Easy Finding 舞蹈链 DLX
- 【POJ3740】Easy Finding DLX(Dancing Links)精确覆盖问题
- poj3740
- POJ3740
- POJ3740
- poj3740
- pku3740 Easy Finding (搜索)
- POJ 3740 Easy Finding
- poj 3740 Easy Finding
- poj 3740 Easy Finding
- POJ 3740 Easy Finding
- linux 下 启动mysql服务出错!Starting MySQL.The server quit without updating PID file (XXX.pid)
- Java基础之JMX
- BZOJ 1009: [HNOI2008]GT考试(DP+KMP失配+矩阵快速幂)
- Android 能让你少走弯路的干货整理
- 转发和重定向的对比
- POJ3740-Easy Finding
- Python脚本进行主播招募相关数据统计的案例
- 三星公佈Bixby2.0升級後比蘋果更酷
- 微信小程序获取scope中权限管理
- iOS h5与原生态混编
- Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)【Dalston版】
- 决策树(Decision Tree)
- android helloCharts 柱状图小结
- 我发起一个http url,本地没问题,发布到测试环境后,报错post 400 bad request,原来是测试的tomcat版本比较高,它对post的长度做了限制。