Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams
Syrian Collegiate Programming Contest (SCPC) is the qualified round for the Arab Collegiate Programming Contest. Each year SCPC organizers face a problem that wastes a lot of time to solve it, it is about how should they arrange the teams in the contest hall.
Organizers know that they have t teams and n*m tables allocated in n rows and m columns in the contest hall. They don't want to put teams in a random way. Each year SCPC chief judge puts a list of paired teams (list of a,b teams) that should not sit next to each other (because they are so good or so bad!).
if pair (a,b) is in chief judge list, this means that:
- team number a should not sit in-front of team number b
- team number b should not sit in-front of team number a
- team number a should not sit right to team number b
- team number b should not sit right to team number a
Organizers wastes a lot of time to find a good team arrangement that satisfy all chief judge needs. This year they are asking you to write a program that can help them.
First line contains number of test cases. The first line in each test case contains three numbers: (1 ≤ n,m ≤ 11) and (1 ≤ t ≤ 10). Second line in each test case contains (0 ≤ p ≤ 40) number of pairs. Then there are p lines, each one of them has two team numbers a and b (1 ≤ a,b ≤ t) where a is different than b.
For each test case, print one line contains the total number of teams arrangements that satisfy all chief judge needs (We guarantee that it will be less than 9,000,000 for each test case). If there is no suitable arrangements print "impossible".
2
1 3 2
1
1 2
2 2 4
2
1 2
1 3
2
impossible
In test case 1 there are 2 teams and 3 tables in one row at the contest hall. There are only one pair (1,2), so there are 2 solutions:
team1 then empty table then team2
team2 then empty table then team1
In test case 2 there are 4 tables in 2 rows and 2 columns, and there are 4 teams. There is no arrangement that can satisfy chief judge needs.
题目链接:http://codeforces.com/gym/100952/problem/E
分析:dfs、剪枝
首先用dp[vis[a][b]][k]布尔数组双向的记录那些vis[i][j-1],vis[i][j+1],vis[i-1][j],vis[i+1][j];a=i-1,i+1,i,i;b=j,j,j-1,j+1;
然后inline void dfs(int k)表示当前正在处理队伍k,然后遍历所有i, j如果没有访问过,并且满足条件则dfs(k + 1)
直到顺利的把所以的t个队伍都填进去了,那一个分枝才ans++; return;
剪枝以后的复杂度不大算的出来,但看数据大小 (1 ≤ n,m ≤ 11) and (1 ≤ t ≤ 10) 这样做一般可以AC
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
int n,m,t,ans;
int vis[][];
bool dp[][];
inline void DFS(int k)
{
if(k==t+)
{
ans++;
return;
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(vis[i][j])
continue;
if(!dp[vis[i-][j]][k]&&!dp[vis[i+][j]][k]&&!dp[vis[i][j-]][k]&&!dp[vis[i][j+]][k])
{
vis[i][j]=k;
DFS(k+);
vis[i][j]=;
}
}
}
}
int main()
{
int T,q,x,y;
T=read();
while(T--)
{
ans=;
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
n=read();
m=read();
t=read();
q=read();
for(int i=;i<q;i++)
{
x=read();
y=read();
dp[x][y]=;
dp[y][x]=;
}
DFS();
if(ans!=)
write(ans),printf("\n");
else printf("impossible\n");
}
return ;
}
Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】的更多相关文章
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】
J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...
- Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】
I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...
- Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】
G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...
- Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...
- Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】
B. New Job time limit per test:1 second memory limit per test:64 megabytes input:standard input outp ...
- Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】
A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...
随机推荐
- 【python】列表
>>> mix = [2,3.4,"abc",'中国',True,['ab',23]]>>> mix[2, 3.4, 'abc', '中国', ...
- linux命令(shell)
1.cat查看一个文件,linux默认bash 2.echo回显命令 3.ls 4.history历史记录,查看使用过的命令 5.根目录下文件目录 6.bin目录下内容多为应用程序和命令 7.boot ...
- 记录一笔关于PHPEXCEL导出大数据超时和内存溢出的问题
通过查阅资料可以找到PHPEXCEL本身已经有通过缓存来处理大数据的导出了.但是昨晚一直没有成功,这可捉急了.最后想来想去就替换了phpExcel的版本了.最后就成功了.话不多说,代码附上 <? ...
- Linux下磁盘监控及系统版本-CPU-内存等查看
1.磁盘IO监控工具 iotop 输入命令:iotop 主要查看程序使用的磁盘IO的信息 安装:yum -y install iotop 第一行:10:01:23 — 当前系统时间126 days ...
- django xdmin使用
我们来看看我们原先django给我们自带的admin后台是什么样子的呢 有人说,你的界面怎么那么丑,我说这个还叫丑吗,他说丑,我说你来,我看看你的,上图 看到登录界面后,我说别看了,我去修改,修改,我 ...
- Tomcat在windows系统中的防火墙设置
在Win7下安装Tomcat后,其他机器无法访问到Tomcat服务,需要修改防火墙设置. 控制面板->window防火墙->允许程序通过Windows防火墙通信 将Tomcat目录下\bi ...
- 4、公司经营的业务来源 - CEO之公司管理经验谈
公司经营的业务来源为公司的运作资金提供了帮助,一般来说,整个公司的领导层为公司的经营做管理,而业务员就为公司的业务提供来源,然后建设部为业务开展做建设. 一.总经理: 公司的总经理主要负责公司运作经营 ...
- thinkphp 3.1.3 redis 只能读取 无法写入的问题
找到thinkphp的目录 thinkphp\Extend\Driver\Cache 下面的Redis 大概在81行足有 // if(is_int($expire)) { // redis ...
- mysql 数据表字段修改sql 语句
1 新增字段 alter table bulletin add citycode varchar(6) not null default 0 [after `id`]; # 城市代码 2 修改字段 a ...
- XCopy命令实现增量备份
xcopy XCOPY是COPY的扩展,可以把指定的目录连文件和目录结构一并拷贝,但不能拷贝系统文件:使用时源盘符.源目标路径名.源文件名至少指定一个:选用/S时对源目录下及其子目录下的所有文件进行C ...