(5943 Kingdom of Obsession)简单的二分图匹配问题

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5943

题意:有n个人,每个人的标号以此是s+1到s+n,要求将所有人重新排序之后满足每个人的位置y能够保证被他的标号整除,就是数组重排之后满足每一位的a[i]%i==0。

解题思路:由于每个人都要满足a[i]%i==0,所以a[i]的值为质数时只能被1整除,所以a[i]为质数超过1个时,便返回No。所以可知匹配的数区域不大,可以直接通过匈牙利算法进行完全二分图匹配。注意:存在n和s交叉重叠的情况,重叠部分的数不用管,直接取n为min(n,s),s为max(n,s)进行匹配。记住所有的数组都要进行合理的初始化,我在这里WA很多。

解题代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
bool line[1000][1000];
int peo[1000];
int used[1000];
int prime(int n)
{
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0)
           return 0;
    return 1;
}
int find(int x,int n)
{
    int i,j;
    for(j=1;j<=n;j++)
        if(line[x][j]==true&&peo[j]==0)
    {
        peo[j]=1;
        if(used[j]==0||find(used[j],n))
        {
            used[j]=x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int T,n,s,ans,summ,t;
    scanf("%d",&T);
    for(int i=0;i<T;i++)
    {
        memset(used,0,sizeof(used));
        memset(line,false,sizeof(line));
        ans=0;
        summ=0;
        scanf("%d%d",&n,&s);
        if(n>=s)
        {
            t=s;
            s=n;
            n=t;
        }
        for(int j=1;j<=n;j++)
        {
            if(prime(s+j)==1)
                ans++;
            if(ans>=2)
            {
                printf("Case #%d: No\n",i+1);
                break;
            }
        }
        if(ans==2)
            continue;
        for(int j=1;j<=n;j++)
            for(int k=1;k<=n;k++)
                if((j+s)%k==0)
                    line[k][j]=true;
        for(int j=1;j<=n;j++)
        {
            memset(peo,0,sizeof(peo));
            summ=summ+find(j,n);
        }
        if(summ==n)
            printf("Case #%d: Yes\n",i+1);
        else
            printf("Case #%d: No\n",i+1);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 SQL 实现书管理系统的完整代码: 创建书籍信息表: ```sql CREATE TABLE books ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, publisher VARCHAR(255) NOT NULL, publish_date DATE NOT NULL, price DECIMAL(7, 2) NOT NULL, isbn VARCHAR(20) NOT NULL, summary TEXT, status ENUM('available', 'borrowed') NOT NULL DEFAULT 'available' ); ``` 创建借阅记录表: ```sql CREATE TABLE borrow_records ( id INT AUTO_INCREMENT PRIMARY KEY, book_id INT NOT NULL, borrower_name VARCHAR(255) NOT NULL, borrow_date DATE NOT NULL, return_date DATE, FOREIGN KEY (book_id) REFERENCES books(id) ); ``` 插入一些书籍信息: ```sql INSERT INTO books (title, author, publisher, publish_date, price, isbn, summary) VALUES ('The Great Gatsby', 'F. Scott Fitzgerald', 'Charles Scribner\'s Sons', '1925-04-10', 12.99, '978-0743273565', 'The story primarily concerns the young and mysterious millionaire Jay Gatsby and his quixotic passion and obsession for the beautiful former debutante Daisy Buchanan.'), ('To Kill a Mockingbird', 'Harper Lee', 'J. B. Lippincott & Co.', '1960-07-11', 10.99, '978-0061120084', 'The novel is renowned for its warmth and humor, despite dealing with serious issues of rape and racial inequality.'), ('1984', 'George Orwell', 'Secker and Warburg', '1949-06-08', 9.99, '978-0451524935', 'The novel is set in Airstrip One, a province of the superstate Oceania in a world of perpetual war, omnipresent government surveillance, and public manipulation.'), ('Pride and Prejudice', 'Jane Austen', 'T. Egerton', '1813-01-28', 7.99, '978-0486284736', 'The novel follows the character development of Elizabeth Bennet, the dynamic protagonist of the book who learns about the repercussions of hasty judgments and comes to appreciate the difference between superficial goodness and actual goodness.'), ('One Hundred Years of Solitude', 'Gabriel García Márquez', 'Harper & Row', '1967-05-30', 14.99, '978-0060883287', 'The novel tells the multi-generational story of the Buendía family, whose patriarch, José Arcadio Buendía, founds the town of Macondo, the metaphoric Colombia.'); ``` 借阅一本书: ```sql INSERT INTO borrow_records (book_id, borrower_name, borrow_date) VALUES (1, 'John Smith', '2021-01-01'); ``` 归还一本书: ```sql UPDATE borrow_records SET return_date = '2021-01-10' WHERE book_id = 1; ``` 查找所有可借阅的书籍: ```sql SELECT * FROM books WHERE status = 'available'; ``` 查找某个作者的所有书籍: ```sql SELECT * FROM books WHERE author = 'Jane Austen'; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值