hdu 5506 GT and set(dfs爆搜)
You should divide the sets into L parts.
And each part should have at least one number in common.
If there is at least one solution,print YES,otherwise print NO.
For each teatcase:
In the first line there are two numbers N and L.
In the next N lines,each line describe a set.
The first number is Ai,and then there are Ai distict numbers stand for the elements int the set.
The numbers in the set are all positive numbers and they're all not bigger than 300.
1≤N≤30,1≤L≤5,1≤Ai≤10,1≤L≤N
You'd better print the enter in the last line when you hack others.
You'd better not print space in the last of each line when you hack others.
2 1
1 1
1 2
3 2
3 1 2 3
3 4 5 6
3 2 5 6
YES
For the second test,there are three sets:{1,2,3},{4,5,6},{2,5,6}
You are asked to divide into two parts.
One possible solution is to put the second and the third sets into the same part,and put the first in the other part.
The second part and the third part have same number 6.
Another solution is to put the first and the third sets into the same part,and put the second in the other part.
题意:
有n个集合,问你能否在L次内把所有集合都删去,如果两个或者更多集合内含有一个相同的数,则这些集合可以同时删除
每个集合中的元素小于10,L<=5,n<=30
分析:
由于数据范围都比较小,很容易想到搜索
很容易想到可以枚举当前集合应该删除哪个数,如果下一个集合中已经出现过了这个数,则跳过下一个集合,可以选择的删除的数不超过5个
每个集合中最多有10个数能被选择,所以时间复杂度也就为10^5*n,乘以n是因为要判断需不需要从当前这个集合中删除一个数
首先贴上第一个代码,这个代码在oj上能AC,但是题目给出的样例中的第一个样例却过不来。和下面贴出的第二个样例写得差不多,但就是样例有一个过不了,不懂什么原因。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 36
#define M 306
#define inf 1e12
int n,L;
vector<int> g[N];
int s[N];
int vis[M];
int flag;
bool judge(int num){
for(int i=;i<s[num];i++){
int val=g[num][i];
if(vis[val]){
return true;
}
}
return false;
}
void dfs(int num,int d){
if(num>=n){
flag=;
return;
}
if(judge(num)){
dfs(num+,d);
}
if(flag){
return;
} if(d>L){
return;
} for(int i=;i<s[num];i++){
int val=g[num][i];
vis[val]=;
dfs(num+,d+);
if(flag){
return;
}
vis[val]=;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&L);
for(int i=;i<=n;i++){
g[i].clear();
}
for(int i=;i<n;i++){
scanf("%d",&s[i]);
for(int j=;j<s[i];j++){
int c;
scanf("%d",&c);
g[i].push_back(c);
}
}
memset(vis,,sizeof(vis));
flag=;
dfs(,);
if(flag==){
printf("YES\n");
}else{
printf("NO\n");
} }
return ;
}
第二个代码,正确。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 36
#define M 306
#define inf 1e12
int n,L;
vector<int> g[N];
int s[N];
int vis[M];
int flag;
bool judge(int num){
for(int i=;i<s[num];i++){
int val=g[num][i];
if(vis[val]){
return true;
}
}
return false;
}
bool dfs(int num,int d){ if(num>=n){
//flag=1;
return true;
}
if(judge(num)){
return dfs(num+,d);
}
//if(flag) return;
if(d>=L) return false;
for(int i=;i<s[num];i++){
int val=g[num][i];
vis[val]=;
if(dfs(num+,d+)) return true;
//if(flag){
// return;
// }
vis[val]=;
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&L);
for(int i=;i<=n;i++){
g[i].clear();
}
for(int i=;i<n;i++){
scanf("%d",&s[i]);
for(int j=;j<s[i];j++){
int c;
scanf("%d",&c);
g[i].push_back(c);
}
}
memset(vis,,sizeof(vis));
/*flag=0;
dfs(0,0);
if(flag==1){
printf("YES\n");
}else{
printf("NO\n");
}
*/
printf("%s\n", dfs(, ) ? "YES" : "NO"); }
return ;
}
hdu 5506 GT and set(dfs爆搜)的更多相关文章
- HDU 4403 A very hard Aoshu problem(dfs爆搜)
http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...
- HDU 4277 USACO ORZ(DFS暴搜+set去重)
原题代号:HDU 4277 原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4277 原题描述: USACO ORZ Time Limit: 5000/1 ...
- hdu 4770(枚举 + dfs爆搜)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 思路:由于最多只有15个".",可以直接枚举放置的位置,然后判断是否能够全部 ...
- hdu 1426:Sudoku Killer(DFS深搜,进阶题目,求数独的解)
Sudoku Killer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 5506 GT and set dfs+bitset优化
GT and set Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Probl ...
- 【BZOJ-1060】时态同步 树形DP (DFS爆搜)
1060: [ZJOI2007]时态同步 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2101 Solved: 595[Submit][Statu ...
- POJ3185 The Water Bowls(反转法or dfs 爆搜)
POJ3185 The Water Bowls 题目大意: 奶牛有20只碗摆成一排,用鼻子顶某只碗的话,包括左右两只在内的一共三只碗会反向,现在给出碗的初始状态,问至少要用鼻子顶多少次才能使所有碗都朝 ...
- hdu 4277 USACO ORZ (dfs暴搜+hash)
题目大意:有N个木棒,相互组合拼接,能组成多少种不同的三角形. 思路:假设c>=b>=a 然后枚举C,在C的dfs里嵌套枚举B的DFS. #include <iostream> ...
- Ancient Go---hdu5546(dfs爆搜CCPC题目)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 题意就是两个人下围棋,问在下一颗x是否能杀死o,'.'是空位子: 枚举所有的点,判断是否合法即可 ...
随机推荐
- poj1833 排列
...
- error LNK2019
error LNK2019: 无法解析的外部符号 "public: virtual __thiscall Fruit::~Fruit(void)" (??1Fruit@@UAE@X ...
- SHDP--Working With HBase (二)之HBase JDBC驱动Phoenix与SpringJDBCTemplate的集成
Phoenix:Phoenix将SQL查询语句转换成多个scan操作,并编排执行最终生成标准的JDBC结果集. Spring将数据库访问的样式代码提取到JDBC模板类中,JDBC模板还承担了资源管 ...
- tomcat建立虚拟主机
WEB浏览器与WEBserver建立连接后,除了将请求URL中的资源路径发送给WEBserver外,还会将URL中的主机名部分作为HTTP请求消息的Host头发送给WEBserver.比如,在浏览器地 ...
- Android launcher3 开发初始篇
版本号:1.0 日期:2014.8.26 2014.8.27 2014.11.10 版权:© 2014 kince 转载注明出处 好久没有写博客,也是由于工作比較忙的关系.当然这不是理 ...
- hdu 4750 Count The Pairs (2013南京网络赛)
n个点m条无向边的图,对于q个询问,每次查询点对间最小瓶颈路 >=f 的点对有多少. 最小瓶颈路显然在kruskal求得的MST上.而输入保证所有边权唯一,也就是说f[i][j]肯定唯一了. 拿 ...
- nginx : TCP代理和负载均衡的stream模块
一直以来,Nginx 并不支持tcp协议,所以后台的一些基于TCP的业务就只能通过其他高可用负载软件来完成了,比如Haproxy. 这算是一个nginx比较明显的缺憾.不过,在1.90发布后这个认知将 ...
- CAS SSO
1. CAS 简介 1.1. What is CAS ? CAS ( Central Authentication Service ) 是 Yale 大学发起的一个企业级的.开源的项目,旨 ...
- 设置Proxy Server和SQL Server实现互联网上的数据库安全
◆首先,我们需要了解一下SQL Server在WinSock上定义协议的步骤: 1. 在”启动”菜单上,指向”程序/Microsoft Proxy Server”,然后点击”Microsoft Man ...
- 伪元素”:after” , “:before"
伪元素就是源码html中不存在,而视觉上又存在的元素 简单用法: blockquote:before { content: open-quote; // 其他样式 } // ...