hust 1045 Task Arrangement II

题目描述

There are N tasks and M resources, each task is asked to use some resources and each resource can only be used by at most one task. You are asked to find the best arrangement to maximum the total number of the compatible tasks.

输入

There are multi test cases. Each case contains a number of lines. The first line is N and M, 1 <= N, M <= 50. In the following N lines, each task contains one line with many integers separated by exactly one space, which specify the required resources. The index of the resources starts from 0.

输出

For each test case, output one line containing the maximum number of the compatible tasks.

样例输入

5 5
0 3 1
1 4
2 1
0
3 2
3 3
1 0
0 2
1 2

样例输出

3
1

又是一道经典题目,先说说我对这个题的感觉吧!一开始认为是一道二分图匹配的题目,可是怎么弄都不清楚,后来又想是不是网络流的题目,唉!还是做不出来,在后来就直接搜索,肯定超时,怎么办,于是想到是不是最大团,结果还真是,两个没有公共资源的任务便可以连一条边,这样不就是求最大团吗?真的很经典
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f

using namespace std;

const double pi=acos(-1.0);
const double eps=1e-8;
typedef pair<int,int>pii;
vector<int>task[51];

void init(int n)
{
     for (int i=0;i<=n;i++)
     task[i].clear();
}

struct max_clique
{
    static const int N=100;
    bool G[N][N];
    int n,Max[N],Alt[N][N],ans;

    bool DFS(int cur,int tot)
    {
        if (cur==0)
        {
            if (tot>ans)
            {
                ans=tot;
                return true;
            }
            return false;
        }
        for (int i=0;i<cur;i++)
        {
            if (cur-i+tot<=ans) return false;
            int u=Alt[tot][i];
            if (Max[u]+tot<=ans) return false;
            int nxt=0;
            for (int j=i+1;j<cur;j++)
            if (G[u][Alt[tot][j]]) Alt[tot+1][nxt++]=Alt[tot][j];
            if (DFS(nxt,tot+1)) return true;
        }
        return false;
    }

    int maxclique()
    {
        ans=0;
        memset(Max,0,sizeof(Max));
        for (int i=n-1;i>=0;i--)//编号从0开始
        {
            int cur=0;
            for (int j=i+1;j<n;j++) if (G[i][j]) Alt[1][cur++]=j;
            DFS(cur,1);
            Max[i]=ans;
        }
        return ans;
    }
};

max_clique T;

void read(int n)
{
     getchar();
     char str[1000],str1[10];
     for (int i=0;i<n;i++)
     {
          gets(str);
          int k=0,j=0;
          int L=strlen(str);
          while(k<L)
          {
               if (str[k]==‘ ‘)
               {
                    str1[j]=NULL;
                    task[i].push_back(atoi(str1));
                    j=0;
               }
               else
               {
                    str1[j]=str[k];
                    j++;
               }
               k++;
          }
          str1[j]=NULL;
          task[i].push_back(atoi(str1));
     }
}
void sovle(int n)
{
     bool vis[51];
     T.n=n;
     for (int i=0;i<n;i++)
     for (int j=0;j<n;j++)
     T.G[i][j]=1;
     for (int i=0;i<n;i++)
     {
          for (int j=0;j<n;j++)
          {
               memset(vis,0,sizeof(vis));
               for (int k=0;k<task[i].size();k++)
               {
                    int v=task[i][k];
                    vis[v]=true;
               }
               bool cut=true;
               for (int k=0;k<task[j].size();k++)
               {
                    int v=task[j][k];
                    if (vis[v])
                    {
                         cut=false;
                         break;
                    }
               }
               if (!cut) T.G[i][j]=0;
          }
     }
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
         init(n);
         read(n);
         sovle(n);
         printf("%d\n",T.maxclique());
    }
    //fclose(stdin);
    return 0;
}

作者 chensunrise

hust 1045 Task Arrangement II

时间: 2024-11-06 22:46:40

hust 1045 Task Arrangement II的相关文章

[leetcode-667-Beautiful Arrangement II]

Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|

Beautiful Arrangement II

这是一个中等题 题目: 思路: 我是创建一个新列表,列表最开始按一个最低,一个最高排列,如果p==k-1了,那么就停止,把剩下元素依次排序,比如n=10,k=3,我就先排1,然后把10加进去,这时p=1,再把2加进去,这时p=2,满足了p=k-1,然后把剩下元素按2,3,4,5,6,7,8,9排序加入列表中.之所以这么做是因为他的差值不会重复,1和10的差最大了,2和10的次大....... 代码: class Solution(object): def constructArray(self,

667. Beautiful Arrangement II

Given two integers n and k, you need to construct a list which contains n different positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|

【leetcode】667. Beautiful Arrangement II

题目如下: Given two integers n and k, you need to construct a list which contains ndifferent positive integers ranging from 1 to n and obeys the following requirement: Suppose this list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio

HUST 1341 A - A Simple Task(哈理工 亚洲区选拔赛练习赛)

A - A Simple Task Time Limit:1000MS    Memory Limit:131072KB    64bit IO Format:%lld & %llu SubmitStatusPracticeHUST 1341 Description As is known to all, lots of birds are living in HUST. A bird has s units of food on the first day, and eats k units

微软职位内部推荐-Software Engineer II

微软近期Open的职位: Job Title: Software Engineer II Location: Beijing, China Document Understanding and Task (DUT) team in STCA focuses on semantic understanding and answer recommendation platform and features for Bing, Cortana and Office. &nbsp Responsibil