poj 1469 COURSES 题解

本文探讨了一种经典的图论问题——二分图最大匹配问题,并通过一个具体竞赛题目进行了阐述。介绍了如何利用邻接矩阵表示学生与课程之间的偏好关系,并实现匈牙利算法来求解最大匹配数。
COURSES
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21515 Accepted: 8455

Description

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions: 

  • every student in the committee represents a different course (a student can represent a course if he/she visits that course) 
  • each course has a representative in the committee 

Input

Your program should read sets of data from the std input. The first line of the input contains the number of the data sets. Each data set is presented in the following format: 

P N 
Count1 Student 1 1 Student 1 2 ... Student 1 Count1 
Count2 Student 2 1 Student 2 2 ... Student 2 Count2 
... 
CountP Student P 1 Student P 2 ... Student P CountP 

The first line in each data set contains two positive integers separated by one blank: P (1 <= P <= 100) - the number of courses and N (1 <= N <= 300) - the number of students. The next P lines describe in sequence of the courses �from course 1 to course P, each line describing a course. The description of course i is a line that starts with an integer Count i (0 <= Count i <= N) representing the number of students visiting course i. Next, after a blank, you抣l find the Count i students, visiting the course, each two consecutive separated by one blank. Students are numbered with the positive integers from 1 to N. 
There are no blank lines between consecutive sets of data. Input data are correct. 

Output

The result of the program is on the standard output. For each input data set the program prints on a single line "YES" if it is possible to form a committee and "NO" otherwise. There should not be any leading blanks at the start of the line.

Sample Input

2
3 3
3 1 2 3
2 1 2
1 1
3 3
2 1 3
2 1 3
1 1

Sample Output

YES
NO

Source

——————————————————————————我是分割线——————————————————————————————

二分图最大匹配。

邻接矩阵map[i][j]表示j号喜欢i号课程,然后对课程进行寻找匹配,匹配成功则标记,之后统计匹配数目即可。

这题丧心病狂卡时,我改了两天,最后发现I/O超时了。。。 取消同步后的流还是略微慢一些,用标准输入输出可以正好卡过。TAT

 1 /*
 2     Problem:
 3     OJ:     
 4     User:   
 5     Time:   
 6     Memory:  
 7     Length:  
 8 */
 9 #include<iostream>
10 #include<cstdio>
11 #include<cstring>
12 #include<cmath>
13 #include<algorithm>
14 #include<queue>
15 #include<cstdlib>
16 #include<iomanip>
17 #include<cassert>
18 #include<climits>
19 #include<vector>
20 #include<list>
21 #include<map>
22 #define maxn 1001
23 #define F(i,j,k) for(int i=j;i<=k;i++)
24 #define M(a,b) memset(a,b,sizeof(a))
25 #define FF(i,j,k) for(int i=j;i>=k;i--)
26 #define inf 0x7fffffff
27 #define maxm 2016
28 #define mod 1000000007
29 //#define LOCAL
30 using namespace std;
31 int read(){
32     int x=0,f=1;char ch=getchar();
33     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
34     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
35     return x*f;
36 }
37 int n,m,p;
38 bool kc[maxn][maxn];
39 bool vis[maxn];
40 int pp[maxn];
41 inline int path(int u)
42 {
43     int a,b,temp;
44     F(i,1,n){
45         if(kc[u][i]&&!vis[i]){
46             vis[i]=true;
47             temp=pp[i];
48             pp[i]=u;
49             if(temp==-1||path(temp)) return 1;
50             pp[i]=temp;
51         }
52     }
53     return 0;
54 }
55 inline int solve()
56 {
57     int a,b,ans=0;
58     M(pp,-1);
59     F(i,1,p){
60         M(vis,0);
61 //        ans+=path(i);
62 if(path(i)) ans++;
63         if(ans==p) break;
64     }
65     return ans;
66 }
67 int main()
68 {
69     int t;cin>>t;
70     while(t--){
71         scanf("%d", &p);scanf("%d", &n);
72         M(kc,0);
73         int num,cnt;
74         F(i,1,p){
75             scanf("%d", &num);
76             F(j,1,num){
77                 scanf("%d", &cnt);
78                 kc[i][cnt]=true;
79             }
80         }
81         if(solve()==p) printf("YES\n");
82         else printf("NO\n");
83     }
84     return 0;
85 }
View Code

 

转载于:https://www.cnblogs.com/SBSOI/p/5916326.html

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值