4271: Love Me, Love My Permutation

Description

Given a permutation of n: a[0], a[1] ... a[n-1], ( its elements range from 0 to n-1, For example: n=4, one of the permutation is 2310) we define the swap operation as: choose two number i, j ( 0 <= i < j <= n-1 ), take a[i] out and then insert a[i] to the back of a[j] of the initial permutation to get a new permutation :a[0], a[1], ..., a[i-1], a[i+1], a[i+2] ... a[j-1], a[j], a[i], a[j+1], ..., a[n-1]. For example: let n = 5 and the permutation be 03142, if we do the swap operation be choosing i = 1, j = 3, then we get a new permutation 01432, and if choosing i = 0, j = 4, we get 31420.

Given a confusion matrix of size n*n which only contains 0 and 1 (ie. each element of the matrix is either 0 or 1), the confusion value of a permutation a[0],a[1], ..., a[n-1] can be calculated by the following function:

int confusion()
{
int result = 0;
for(int i = 0;i < n-1; i++)
for(int j = i+1; j < n; j++)
{
result = result + mat[a[i]][a[j]];
}
return result;
}

Besides, the confusion matrix satisfies mat[i][i] is 0 ( 0 <= i < n) and mat[i][j] + mat[j][i] = 1 ( 0 <= i < n, 0 <= j < n, i != j ) given the n, the confusion matrix mat, you task is to find out how many permutations of n satisfies: no matter how you do the swap function on the permutation(only do the swap function once), its confusion value does not increase.

Input

The first line is the number of cases T(1 <= T <= 5), then each case begins with a integer n (2 <= n <= 12), and the next n line forms the description of the confusion matrix (see the sample input).

Output

For each case , if there is no permutation which satisfies the condition, just print one line “-1”, or else, print two lines, the first line is a integer indicating the number of the permutations satisfy the condition, next line is the Lexicographic smallest permutation which satisfies the condition.

Sample Input

2
2
0 1
0 0
3
0 0 0
1 0 1
1 0 0

Sample Output

1
0 1
1
1 2 0 题解:DFS
从后向前搜索,放进去的一位要和后面的每一段(n~n-1,n~n-2...n~0)保持1的个数大于0的个数,如果成立,则这一位可以放这个数,在向前继续搜索。
因为只能前面的数插入到后面,所以不需要考虑新加进去的数会不会对后面已经排好的短造成影响。也是这个原因要从后向前搜索。 AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std; int ans[25];
int num[25][25];
int out[25];
int n;
int ct; void print()
{
//cout<<'?'<<endl;
int flag = 1;
for(int i = n-1;i>=0;i--)
{
if(ans[i]<out[i]) {flag = 0; break;}
if(ans[i]>out[i]) break;
}
if(!flag)
for(int i = n-1;i>=0;i--)
out[i] = ans[i];
ct++;
} void dfs(int pos)
{
if(pos>=n)
{
print();
return;
}
if(pos == 0)
{
for(int i = 0;i<n;i++)
{
ans[pos] = i;
dfs(pos+1);
}
}
else
{
for(int i = 0;i<n;i++)
{
int cnt = 0;
int flag = 1;
for(int j = pos-1;j>=0;j--)
{ if(num[i][ans[j]]==0) {flag = 0;break;}
cnt += num[i][ans[j]];
//cout<<i<<' '<<ans[j]<<' '<<num[i][ans[j]]<<' '<<cnt<<endl;
if(cnt<0) {flag = 0;break;}
}
if(flag) {ans[pos] = i; dfs(pos+1);}
}
}
return;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ct = 0;
scanf("%d",&n);
M(num,0);
M(ans,0);
M(out,0x3f);
for(int i = 0;i<n;i++)
for(int j = 0;j<n;j++)
{
scanf("%d",&num[i][j]);
if(i == j) num[i][j] = 0;
else if(num[i][j]==0)
num[i][j] = -1;
}
dfs(0);
if(ct==0) {puts("-1"); continue;}
printf("%d\n",ct);
for(int j = n-1;j>0;j--)
printf("%d ",out[j]);
printf("%d\n",out[0]);
}
return 0;
}

  

 

soj4271 Love Me, Love My Permutation (DFS)的更多相关文章

  1. Tree and Permutation dfs hdu 6446

    Problem Description There are N vertices connected by N−1 edges, each edge has its own length.The se ...

  2. codeforces 691D D. Swaps in Permutation(dfs)

    题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...

  3. codeforces 691D Swaps in Permutation DFS

    这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...

  4. HNU Joke with permutation (深搜dfs)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=13341&courseid=0 Joke with pe ...

  5. 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

  7. codeforces 285 D. Permutation Sum 状压 dfs打表

    题意: 如果有2个排列a,b,定义序列c为: c[i] = (a[i] + b[i] - 2) % n + 1 但是,明显c不一定是一个排列 现在,给出排列的长度n (1 <= n <= ...

  8. leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

  9. 【HDOJ6628】permutation 1(dfs)

    题意:求1到n的排列中使得其差分序列的字典序为第k大的原排列 n<=20,k<=1e4 思路:爆搜差分序列,dfs时候用上界和下界剪枝 #include<bits/stdc++.h& ...

随机推荐

  1. django 提示ImportError: cannot import name json_response

    from json_response import JsonResponse, json_response as json_resp 使用的语句如上,其实并不是没有安装,只是需要升级一下 pip in ...

  2. python列表、元组、字典(四)

    列表 如:[11,22,33,44,44].['TangXiaoyue', 'bruce tang'] 每个列表都具备如下功能: class list(object): ""&qu ...

  3. mongo&node

    /////  node install $ sudo apt-get install python-software-properties $ curl -sL https://deb.nodesou ...

  4. python chinese code

    http://blog.csdn.net/inte_sleeper/article/details/6676351 编码的历史 1.     ASCII ASCII(American Standard ...

  5. mac系统的一些操作常识

    mac系统如何显示和隐藏文件 苹果Mac OS X操作系统下,隐藏文件是否显示有很多种设置方法,最简单的要算在Mac终端输入命令.显示/隐藏Mac隐藏文件命令如下(注意其中的空格并且区分大小写): 显 ...

  6. datatable group by

    对datatable 里面的数据按某一特定的栏位进行分组并且按照某一规则 var query = from t in rate.AsEnumerable()   group t by new { t1 ...

  7. DB服务器中的参数优化

    1.swappiness 禁止系统使用swap空间,配置/etc/sysctl.conf中的vm.swappiness=0 2.Scheduler调度 Scheduler调度,指的是磁盘的IO调度算法 ...

  8. Iframe 在项目中的使用总结

    参考:http://www.cnblogs.com/MaxIE/archive/2008/08/13/1266597.html 问题一:首先我们用iframe加载页面,第一个需要解决的问题是高度自适应 ...

  9. 获取ip的ip138.com

    代码: function get_onlineip() { $ch = curl_init('http://iframe.ip138.com/ic.asp'); curl_setopt($ch, CU ...

  10. xhprof安装使用

    安装: 到pecl官网下载xhprof的最新版:http://pecl.php.net/package/xhprof wget http://pecl.php.net/get/xhprof-0.9.4 ...