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

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

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. Tomcat热部署方法（3种）【转】

热部署是指在你修改项目BUG的时候对JSP或JAVA类进行了修改在不重启WEB服务器前提下能让修改生效.但是对配置文件的修改除外! 1.直接把项目web文件夹放在webapps里. 2.在tomcat ...

2. test md

[TOC] Glossary SUT SYSTEM UNDER TEST CUT CLASS UNDER TEST MUT METHOD UNDER TEST Tests without Use of ...

3. day1（变量、常量、注释、用户输入、数据类型）

一.变量 name = "SmallNine" 等号前面是变量名(标识符),等号后面是变量值 变量的主要作用:就是把程序运算的中间结果临时存到内存里,已备后面的代码继续调用. 变量 ...

4. 洛谷P1593 因子和

题目描述 输入两个正整数a和b,求a^b的因子和.结果太大,只要输出它对9901的余数. 输入输出格式 输入格式: 仅一行,为两个正整数a和b(0≤a,b≤50000000). 输出格式: a^b的因 ...

5. 队列Queue和栈

1.队列Queue是常用的数据结构,可以将队列看成特殊的线性表,队列限制了对线性表的访问方式,只能从线性表的一段添加(offer)元素, 从另一段取出(poll)元素,队列遵循先进先出的原则. 2.J ...

6. leetcode — interleaving-string

/** * Source : https://oj.leetcode.com/problems/interleaving-string/ * * * Given s1, s2, s3, find wh ...

7. JavaScript中常见的10个BUG及其修复方法

如今网站几乎100%使用JavaScript.JavaScript看上去是一门十分简单的语言,然而事实并不如此.它有很多容易被弄错的细节,一不注意就导致BUG. 1. 错误的对this进行引用 在闭包 ...

8. php分享二十七：批量插入mysql

一:思考 1:如果插入的某个字段大于数据库定义的长度了,数据库会怎么处理? 1>如果数据库引擎是myisam,则数据库会截断后插入,不报错 2>如果数据库引擎是innodb,则数据库会报 ...

9. Thinkphp动态切换主题

'DEFAULT_THEME' => '2014', 'TMPL_DETECT_THEME' => true, // 自动侦测模板主题 'THEME_LIST' => '2012,2 ...

10. Java门派的风险

Java门派的风险 正在看周思博(www.joelonsoftware.com)的新文章.这次是疯狂攻击Java.主要论点是:Java不够难,作为工业语言不错,但作为学校的教学语言,就忒差了.学校应该 ...