E. Arrange Teams

time limit per test:2 seconds
memory limit per test:64 megabytes
input:standard input
output:standard output

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.

Input

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.

Output

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".

Examples
Input
2
1 3 2
1
1 2
2 2 4
2
1 2
1 3
Output
2
impossible
Note

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+剪枝】的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. http中的get和post(二)

    博客园精华区有篇文章< GET 和 POST 有什么区别?及为什么网上的多数答案都是错的 >,文中和回复多是对以下两个问题进行了深究: 长度限制 Url 是否隐藏数据 在我看来这两者都不是 ...

  2. JAVA Socket编程(一)之UDP通信

    常见的通讯协议有udp和tcp. --将数据及源.目的封装在数据包中,不需要建立连接: --每个数据包的大小限制在64k以内: --因无连接,是不可靠协议: --不需要建立连接,所以传输速度快,但是容 ...

  3. 在Ubuntu14.04下安装 ffmpeg-2.4.13(处理视频用,将视频保存为图片序列)

    首先在 http://www.ffmpeg.org/olddownload.html 下载 ffmpeg-2.4.13.tar.bz2 : 然后安装 yasm 和 libx264: apt-get i ...

  4. [置顶] Xamarin Android安装教程(2016最新亲测安装版)

    写这篇安装教程前要说的几句话 之前很多人想用Vs来开发Android项目,苦于这个环境的安装.的确这并不是一件简单的事情,并不是开发者都能在花一上午能装好,如果你花了一天时间,第一个Xamarin   ...

  5. Hadoop版本选择

    刚开始学习Hadoop时就曾经一直抱怨Hadoop的安装部署为什么这么麻烦,对于一个新手需要捯饬一天才能把分布式环境安装配置好.而对于一个自学Hadoop而周围又没人交流的菜鸟来说,我对Hadoop的 ...

  6. [经验分享]WebAPI中返回类型JsonMessage的应用

    这是一个绝无仅有的好类型,一个你爱不释手的好类型,好了,不扯了,直接上干货. 相信大家都知道,在调用接口的时候返回Json数据已经成为一种不成文的标准,因为它的解析快,易读等优秀的特性,所以被绝大多数 ...

  7. 多线程下QAxObject指针为NULL的解决办法

    项目中需要对Excel进行操作,把数据中的数据写入到Excel文件中.在数据量大的情况下,操作Excel是一件费时的操作. 但是执行到下列代码时就会出现空指针的情况: QAxObject *excel ...

  8. Python爬虫——爬豆瓣登录页面

    直接上代码 import urllib.request import http.cookiejar from lxml import etree # from spiderImg import get ...

  9. jquery 怎么判断当前按钮是否是disabled 属性

    https://zhidao.baidu.com/question/1733097469792964827.html 应该用 prop("disabled") 有则返回true吧? ...

  10. python中星号的意义(**字典,*列表或元组)

    传递实参和定义形参(所谓实参就是调用函数时传入的参数,形参则是定义函数是定义的参数)的时候,你还可以使用两个特殊的语法:*.** . 调用函数时使用* ,** test(*args)中 * 的作用:其 ...