UVA - 12036 Stable Grid

來源:互聯網
上載者:User

標籤:思維

Description


 Stable Grid 

Consider a grid of size n x n where each cell contains a number. Let‘s call a grid stable if we canrearrange the numbers of each row so that every column of the resulting grid has no repeated values.

Mathematically, say, we have a grid G of sizen x n. We would like to permute the elements of eachrowGi (1in) so that the resulting grid has the following property:

For every column j, the valuesGi, j are all distinct for (1in).

As an example, consider a grid G of size 4 x 4 as shown below

2 1 1 3  
3 1 2 6  
2 6 10 3  
9 8 7 6  

We can permute each row to get G‘ as shown below

2 1 1 3  
1 3 6 2  
6 2 3 10  
9 8 7 6  

In G‘, there are no repeated values in any column. So, the given grid is stable.

In this problem, you will be given a grid of size nx n and you have to determine whether it is stableor not.

Input

Input starts with an integer T (500), denoting the number of test cases.

Each case starts with a line containing the value of n (0 <n < 100). The next n lines containnintegers each. The j-th integer of thei-th line represent the value of Gi, j. Consecutive integers in eachline are separated with space characters. All the integers in the grid are non-negative with magnitudenot greater than 100.

Output

For each case, output the case number first. If the given grid is stable, output `yes‘ otherwise output`no‘. Look at the samples for exact format.

Sample Input

342 1 1 33 1 2 62 6 10 39 8 7 631 1 21 1 12 2 231 2 32 3 13 1 2

Sample Output

Case 1: yesCase 2: noCase 3: yes題意:給定一個矩陣,是否可以對每行進行重排,使得每列的所有元素各不相同思路:記錄一個數出現的次數,大於n的話,那麼無論怎麼排都會有重複
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 110;int vis[maxn], n;int main() {int t, cas = 1;scanf("%d", &t);while (t--) {scanf("%d", &n);memset(vis, 0, sizeof(vis));int a;int flag = 0;for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {scanf("%d", &a);vis[a]++;if (vis[a] > n) flag = 1;}printf("Case %d: ", cas++);if (flag) printf("no\n");else printf("yes\n");}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.