zoukankan      html  css  js  c++  java
  • Easy Finding POJ

    显然这是一道dfs简单题

    或许匹配也能做

    然而用了dancing links

    显然这也是一道模板题

    好的吧

    调了一上午 终于弄好了模板

    Easy Finding
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 19052   Accepted: 5273

    Description

    Given a M×N matrix AAij ∈ {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 MN (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 3
    0 1 0
    0 0 1
    1 0 0
    4 4
    0 0 0 1
    1 0 0 0
    1 1 0 1
    0 1 0 0

    Sample Output

    Yes, I found it
    It is impossible
    

    Source

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <bitset>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define rb(a) scanf("%lf", &a)
    #define rf(a) scanf("%f", &a)
    #define pd(a) printf("%d
    ", a)
    #define plld(a) printf("%lld
    ", a)
    #define pc(a) printf("%c
    ", a)
    #define ps(a) printf("%s
    ", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 11000, INF = 0x7fffffff;
    
    int S[maxn], head[maxn], vis[maxn];
    int U[maxn], D[maxn], L[maxn], R[maxn];
    int C[maxn], X[maxn];
    int n, m, ans, ret;
    
    void init()
    {
        for(int i = 0; i <= m; i++)
            D[i] = i, U[i] = i, R[i] = i + 1, L[i] = i - 1;
        L[0] = m, R[m] = 0;
        mem(S, 0), mem(head, -1);
        ans = m + 1;
    }
    
    void delc(int c)
    {
        L[R[c]] = L[c], R[L[c]] = R[c];
        for(int i = D[c]; i != c; i = D[i])
            for(int j = R[i]; j != i; j = R[j])
                U[D[j]] = U[j], D[U[j]] = D[j], S[C[j]]--;
    
    }
    
    void resc(int c)
    {
        for(int i = U[c]; i != c; i = U[i])
            for(int j = L[i]; j != i; j = L[j])
                U[D[j]] = j, D[U[j]] = j, S[C[j]]++;
        L[R[c]] = c, R[L[c]] = c;
    }
    
    void add(int r, int c)
    {
        ans++, S[c]++, C[ans] = c, X[ans] = r;
        D[ans] = D[c];
        U[ans] = c;
        U[D[c]] = ans;
        D[c] = ans;
        if(head[r] < 0) head[r] = L[ans] = R[ans] = ans;
        else L[ans] = head[r], R[ans] = R[head[r]],L[R[head[r]]] = ans, R[head[r]] = ans;
    }
    
    
    bool dfs(int sh)
    {
        if(!R[0])
        {
            ret = sh;
            return true;
        }
        int c = R[0];
        delc(c);
        for(int i = D[c]; i != c; i = D[i])
        {
            vis[sh] = i;
            for(int j = R[i]; j != i; j = R[j])
                delc(C[j]);
            if(dfs(sh + 1)) return true;
            for(int j = L[i]; j != i; j = L[j])
                resc(C[j]);
        }
        resc(c);
        return false;
    }
    
    
    int main()
    {
        while(scanf("%d%d", &n, &m) != EOF)
        {
            init();
            int tmp;
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= m; j++)
                {
                    rd(tmp);
                    if(tmp) add(i, j);
                }
            if(dfs(0))
                printf("Yes, I found it
    ");
            else
                printf("It is impossible
    ");
    
    
        }
    
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    CV类数据集高速下载
    pytorch和tensorflow的爱恨情仇之参数初始化
    pytorch和tensorflow的爱恨情仇之定义可训练的参数
    同时打乱数据集和标签的几种方式
    pytorch和tensorflow的爱恨情仇之张量
    解决pip安装pytorch缓慢问题(直接使用命令)
    pytorch和tensorflow的爱恨情仇之基本数据类型
    完美解决xxx网盘上传和下载慢问题
    大学生运动情况调研计划
    dockerFile解析
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/10613348.html
Copyright © 2011-2022 走看看