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. .Net和C#的理解

    .Net是一个微软出品的开发平台,不仅仅是适用于Windows操作系统,还适用于mono,Linux等等, .Net库包括两部分:部分库定义了一些基本类型.CTS(common Type System ...

  2. Objective-C 谈谈深浅拷贝,copy和mutable copy都不是完全拷贝

    (一)字符串中的指针赋值,copy和mutablecopy NSString和NSString (1)指针赋值 肯定指向同一个字符串地址. (2)copy(和直接指向一样) NSString *str ...

  3. 基本概率分布Basic Concept of Probability Distributions 7: Uniform Distribution

    PDF version PDF & CDF The probability density function of the uniform distribution is $$f(x; \al ...

  4. hibernate-聚合函数分组统计数据查询

    聚合函数: 实例: package Test; import static org.junit.Assert.*; import java.util.List; import org.hibernat ...

  5. C++ Virtual

    摘自:http://www.cnblogs.com/xd502djj/archive/2010/09/22/1832912.html namespace QCAR { /// Area is the ...

  6. HD1556Color the ball(树状数组)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. python不同模式打开文件的完全列表

    模式 描述 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. rb 以二进制格式打开一个文件用于只读.文件指针将会放在文件的开头.这是默认模式. r+ 打开一个文件用于读写.文件指 ...

  8. BZOJ4668: 冷战

    并查集,按秩合并,树高log,暴力查询. 果然bzoj新挂的题中过的人多的全是sb题. 写了一发秒WA,发现姿势不对.(@_@) 然后过了50min,开始怀疑人生.(*_*) 这么长时间我lct都写完 ...

  9. UI学习之常用方法(续)

    UIView 1.    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(100,100, 100, 30)]; [vv.layer setB ...

  10. 过滤字符串的Html标记 c#函数 .

    .public static string StripHTML(string strHtml) . { . string[] aryReg ={ . @"<script[^>]* ...