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 ...
随机推荐
- Vagrant安装完lnmp后,配置linux和windows共享文件并配置虚拟主机访问项目
虚拟机目录下的Vagrantfile文件是vagrant的配置文件,如果想把虚拟机当作一台服务器,可以通过ip访问,需要修改配置文件进行配置. (1)第一步:打开虚拟机目录下的Vagrantfile文 ...
- 网关 php-cgi fastcgi phpfpm
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/88 关于网关的理解,一句话就是:作为一种翻译器,抽象出了一种能够 ...
- 一道叉姐的AC自动机鬼题
题面描述丢失了... 给n个串模板串,然后再给你m个串,对于这m个串的每个串,问在[L,R]的模板串中,在多少个串中出现过; 这题的正解是对于后m个串建AC自动机,然后离线,在fail树上树链求并. ...
- c# 了解c# 面向对象
C#是微软公司发布的一种面向对象的.运行于.NET Framework之上的高级程序设计语言.并定于在微软职业开发者论坛(PDC)上登台亮相.C#是微软公司研究员Anders Hejlsberg的最新 ...
- js、jQuery、layer实现弹出层的打开、关闭
打开layer layer.open({ type: 2, title: '新增收货地址', shadeClose: true,//点击阴影关闭 shade: 0.8, area: ['900px', ...
- 搭建subversion 服务器,并自动部署项目
1 subversion目录文件说明: *dav目录:是提供apache与mod_dav_svn使用的目录,让他们存储内部数据*db目录:就是所有版本控制的数据存放文件*hooks目录:放置hook脚 ...
- Kill 进程
动态杀各种进程,谨慎操作:事例 status='sleeping' --AUTHOR KiNg --DATE 2016-05-30 DECLARE @SPID INT ...
- Ant Design Pro 学习一 安装
安装: 直接 clone git 仓库 $ git clone --depth=1 https://github.com/ant-design/ant-design-pro.git my-projec ...
- 固定表头,单元格td宽度自适应,多内容出现-横向纵向滚动条数据表格的<前世今生>
固定表头,单元格td宽度自适应,多内容出现-横向纵向滚动条数据表格的<前世今生> 先上图例 & 无论多少数据--都完美! 背景:由于我司行业方向,需要很多数据报表,则t ...
- 【转】Tableau 9.3.8 desktop for Mac 中文破解
tableau破解版本下载地址 安装步骤: 1. 编辑hosts 文件 在终端输入:sudo nano /etc/hosts 添加如下内容: 127.0.0.1 licensing.tableauso ...