Sicily 1718. University Rankings

1718. University Rankings

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

At present, the university rankings are very popular. They help senior high school students to choose universities for further study. For example, you can find the CHINESE UNIVERSITY RANKINGS at http://www.netbig.com/.


As we know, a university usually has many different departments, such as department of Computer Science (CS), department of Electronic Engineering (EE), and School of Foreign Languages (FLS). Some of them are quite good when comparing to the other universities, but others are not. So, most of the university rankings are composed of several ranking lists, each list for one department. 
But here comes a problem that sometimes it’s hard to determine which university is better, when comparing two universities with each other. Fortunately, Doctor Bob has advanced a new concept named “absolutely better”, with which the problem above can be partially solved.
Now, here is an example to explain the concept “absolutely better”: 
Assume that there are three universities (X, Y, Z) and every university has three departments: CS, EE and FLS. And the rankings of different departments are as followed:
The ranking of CS: X > Y > Z (X>Y means X have a better CS department than Y)
The ranking of EE: X > Z > Y
The ranking of FLS: Z > X >Y
Obviously, each department of University X is better than that of University Y. Then, it’s called that X is absolutely better than Y. Using the “absolutely better” concept, it becomes possible to compare some pairs of the universities.
Now Bob has the complete rankings of different departments of many universities, and he wants to find k universities (U1,…,Uk) such that Ui is absolutely better that Uj (for any i<j). Could you tell Bob the maximum value of k?

Input

The first line of the input is a positive integer C. C is the number of test cases followed.
The first line of each test case is two positive integer N,M (0<N,M <= 100), N is the number of universities and M is the number of departments. And then M lines follow. The i-th (1 <= i <= M) line contains N numbers Ui(1 <= i <= N, 1 <= Ui <= N), indicating the ranking of the i-th department. If University Ui precedes to University Uj in line k, then the k-th department of Ui is better than the k-th department of Uj.

Output

The output should consist of C lines, one line for each test case. Each line only contains one integer – the maximum value of k as described above. No redundant spaces are needed.

Sample Input

1
3 3
1 2 3
1 3 2
3 1 2

Sample Output

2
// Problem#: 1718
// Submission#: 3585200
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <string.h>
#include <stdio.h>
#include <math.h>

#define MAXN 110
#define MAXM 110

long N, M;
long rank[MAXN][MAXM];
bool b[MAXN][MAXN];
long outd[MAXN];

void input() {
    long i, j, k, x;
    scanf("%ld%ld", &N, &M);
    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            scanf("%ld", &x);
            x--;
            rank[x][i] = j;
        }
    }
    memset(b, 0, sizeof(b));
    memset(outd, 0, sizeof(outd));
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++) {
            for (k = 0; k < M; k++)
                if (rank[i][k] >= rank[j][k]) break;
            if (k >= M) {
                b[i][j] = 1;
                outd[i]++;
            }
        }
}
    
long solve() {
    long v[MAXN], flag = 1, answer = 0, i, j, t;
    memset(v, 0xff, sizeof(v));
    while (flag) {
        flag = 0;
        for (i = 0; i < N; i++)
            if (!outd[i] && v[i] < 0) {
                flag = 1;
                for (t = 0, j = 0; j < N; j++)
                    if (b[i][j] && v[j] > t) t = v[j];
                v[i] = t + 1;
                if (v[i] > answer) answer = v[i];
                for (j = 0; j < N; j++) 
                    if (b[j][i]) outd[j]--;
            }
    }
    return answer;
}

int main() {
    long C;
    scanf("%ld", &C);
    while (C--) {
        input();
        printf("%ld\n", solve());
    }
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值