David has containers and different types of balls, both of which are numbered from to . The distribution of ball types per container are described by an matrix of integers, , where each is the number of balls of type in container . For example, consider the following diagram for :
In a single operation, David can swap two balls located in different containers (i.e., one ball is moved from container to and the other ball is moved from to ).
For example, the diagram below depicts a single swap operation:
David wants to perform some number of swap operations such that both of the following conditions are satisfied:
- Each container contains only balls of the same type.
- No two balls of the same type are located in different containers.
You must perform queries where each query is in the form of a matrix, . For each query, print Possible
on a new line if David can satisfy the conditions above for the given matrix; otherwise, print Impossible
instead.
Input Format
The first line contains an integer denoting (the number of queries). The subsequent lines describe each query in the following format:
- The first line contains an integer denoting (the number of containers and ball types).
- Each line of the subsequent lines contains space-separated integers describing row in matrix .
Constraints
Scoring
- For of score, .
- For of score, .
Output Format
For each query, print Possible
on a new line if David can satisfy the conditions above for the given matrix; otherwise, print Impossible
instead.
Sample Input 0
2
2
1 1
1 1
2
0 2
1 1
Sample Output 0
Possible
Impossible
Explanation 0
We perform the following queries:
- The diagram below depicts one possible way to satisfy David's requirements for the first query:
Thus, we printPossible
on a new line. - The diagram below depicts the matrix for the second query:
No matter how many times we swap balls of type and between the two containers, we'll never end up with one container only containing type and the other container only containing type . Thus, we printImpossible
on a new line.
这个题一开始不知道怎么弄得,意思半路当成了种类1的球只能待在容器1里面,导致一直wa.....QAQ。
当然意思是只要每种种类的球全待在一个容器里,而且每个容器只有一种种类的球就可以了。
先说一下比较重要的一点就是能不能交换成功,这个其实从大面上想就很简单了,比如说种类1的球有n个,那么至少有一个容器里面有n个球,才能完成交换,不必管它怎么交换的,因为如果你没有一个容器有n个球,那么纵然不可能成功变成题目要求的那样。而只要都能找到相对应的,那么最后必定存在能交换成功的方法。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
using namespace std;
int n,m,q;
const int MAXN=1e5+7;
const int mod=1e9+7;
long long ball[110];
long long tong[110];
long long tu[110][110];
int main()
{
int i,j;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;++i)
for(j=0;j<n;++j)scanf("%I64d",&tu[i][j]);
memset(tong,0,sizeof(tong));
memset(ball,0,sizeof(ball));
for(i=0;i<n;++i)
for(j=0;j<n;++j)
{
tong[i]+=tu[i][j];
ball[j]+=tu[i][j];
}
int flag=1;
sort(tong,tong+n);
sort(ball,ball+n);
for(i=0;i<n;++i)
{
if(ball[i]!=tong[i])
{
flag=0;
break;
}
}
if(flag)puts("Possible");
else puts("Impossible");
}
}