Gym 100952E E. Arrange Teams dfs、剪枝
来源:互联网 发布:淘宝到家是干嘛的 编辑:程序博客网 时间:2024/05/16 11:53
Description
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
21 3 211 22 2 421 21 3
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
- Gym 100952E E. Arrange Teams dfs、剪枝
- Teams Gym-101028E
- Gym 101028E-Teams-求一组数的最大公约数
- Gym-101612E E
- GYM 100247 E. Of Groups and Rights(dfs)
- Gym 101246E Kidnapping 递推or dfs 优化
- dfs+剪枝(暑假集训第三周周测E)
- 8.17--练习赛E题--Scales(DFS背包加剪枝)
- Gym 100935E Pairs
- CodeForces Gym 100735E
- Codeforces gym 100685 E
- Gym 100712E 贪心
- codeforces [Gym-100814E]
- Codeforces gym 101243 E
- Gym 101161 E
- Gym 101243.E
- Gym 100935E E. Pairs (模拟)
- Gym 100187E - Two Labyrinths
- PL/SQL 程序包中的EXIT和RETURN
- 对于String类中的"abc"与new String("abc")的一些理解
- Hdu-5765 Bonds(状压)
- Android 多媒体中的音乐播放器编程
- 百度编辑器UEditor常用设置函数
- Gym 100952E E. Arrange Teams dfs、剪枝
- rename 表操作后index是否依然可用
- GLOBAL TEMPORARY TABLE
- 删除用户
- 大数据量、高并发量网站解决方案
- dbms_random.value生成随机数
- Android异常之Error:Execution failed for task ':app:transformClassesWithInstantRunSlicerForDebug'.
- 移植uboot第四步:设置NAND启动
- oracle table()函数用法