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,'.'是空位子: 枚举所有的点,判断是否合法即可 ...
随机推荐
- HDU1754(线段树)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 流媒体开发之--HLS--M3U8解析(2): HLS草案
目录 1 简介 2 2 概述 2 3 播放列表文件 3 3.1 介绍 3 3.2新标签 4 3.2.1 EXT-X-TARGETDURATION 4 3.2.2 EXT-X-MEDIA-SEQUENC ...
- Android应用自动更新功能的实现!!!
自动更新功能的实现原理,就是我们事先和后台协商好一个接口,我们在应用的主Activity里,去访问这个接口,如果需要更新,后台会返回一些数据(比如,提示语:最新版本的url等).然后我们给出提示框,用 ...
- hadoop python and Twitter
http://www.wubiaoblog.com/archives/1159 http://blog.csdn.net/anbo724 http://f.dataguru.cn/forum.php? ...
- Java生成登陆时使用的图片验证码
package com.ws.frame.utils; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; i ...
- 检查DISPLAY设置时Xlib出现No protocol specified错误
退出到root用户,执行xhost +命令后,再次切换到Oralce用户,执行runInstaller命令,错误消失
- UILabel + 导入字体
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 100)]; 1.设置文字颜色 label.textC ...
- iOS中json解析出现的null,nil,NSNumber的问题
在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式. 问题现象 但是几个项目下来一直遇到一个坑爹的问题,程序在获取某些数据之后莫名崩溃.其实很早就发现了原因:由于 ...
- Java File 类的使用方法详解(转)
转自:http://www.codeceo.com/article/java-file-class.html Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对J ...
- opals 开发记录
1:开发需要的文件 下载地址:http://www.geo.tuwien.ac.at/opals/html/index.html 注意只能在release下才能通过. 我自己整理好的(64位的),以备 ...