Gym 100952E E. Arrange Teams dfs、剪枝

来源:互联网 发布:淘宝到家是干嘛的 编辑:程序博客网 时间:2024/05/16 11:53

E - E
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice Gym 100952E

Description

standard input/output
Statements

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

Sample Input

Input
21 3 211 22 2 421 21 3
Output
2impossible

Hint

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.


Source

UESTC 2016 Summer Training #21

Gym 100952E


My Solution

dfs、剪枝

首先用 pai[][]布尔数组双向的记录 那些 pair

然后void dfs(int k) 表示当前正在处理 队伍 k, 然后遍历 所有 i, j 如果没有访问过, 并且满足条件 则dfs(k + 1)

直到顺利的把所以的t个队伍都填进去了, 那一个分枝才ans++; return;

剪枝以后的复杂度不大算的出来, 嘿嘿, 但看数据大小 (1  ≤  n,m  ≤  11) and (1  ≤  t  ≤  10) 这样做一般可以pass the tests

#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int maxn = 16;int n, m, t, ans, vis[maxn][maxn];bool pai[maxn][maxn];void dfs(int k){    if(k == t + 1) {ans++; return;}    for(int i = 1; i <= n; i++){        for(int j = 1; j <= m; j++){            if(vis[i][j]) continue;            if(!pai[vis[i-1][j]][k] && !pai[vis[i+1][j]][k]                && !pai[vis[i][j-1]][k] && !pai[vis[i][j+1]][k]){                    vis[i][j] = k;                    dfs(k + 1);                    vis[i][j] = 0;            }        }    }}int main(){    #ifdef LOCAL    freopen("a.txt", "r", stdin);    //freopen("b.txt", "w", stdout);    #endif // LOCAL    int T, q, x, y;    scanf("%d", &T);    while(T--){        ans = 0;        memset(pai, false, sizeof pai);        memset(vis, 0, sizeof vis);        scanf("%d%d%d%d", &n, &m, &t, &q);        //!前面少打了一个 %d  然后运行了60多秒才输出结果, 关键是有些答案还是正确的, 又是一条智障的经验啊 (┬_┬)        for(int i = 0; i < q; i++){            scanf("%d%d", &x, &y);            pai[x][y] = true;            pai[y][x] = true;        }        dfs(1);        if(ans != 0) printf("%d\n", ans);        else printf("impossible\n");    }    return 0;}


  Thank you!

                                                                                                                                               ------from ProLights

0 0