Cosmetic Survey 建图bfs

问题 B: Cosmetic Survey

时间限制: 2 Sec  内存限制: 512 MB
提交: 166  解决: 12
[提交] [状态] [命题人:admin]

题目描述

ICP(International Cosmetic Perfection) Company plans to survey the preferences of new m cosmetics in order to know which cosmetic is the most preferred. For this survey, ICPC selected n people as evaluators. Each evaluator must submit an ordered list of the preferences on those m cosmetics. Each evaluator can rank the cosmetics as follows:

Cosmetic Survey 建图bfs_第1张图片

An evaluator can rank cosmetics with positive integer numbers, but the positive integers are not necessarily consecutive. The cosmetics with the smallest positive number are the most preferred ones, and the ones with the second smallest positive number are the second most preferred ones, and so on.
An evaluator can give the same preference to more than one cosmetic. This indicates that this evaluator has no preference among those cosmetics.
An evaluator may not rank some cosmetics. The unranked cosmetics are marked with number 0 in the ordered list. This indicates that this evaluator strictly prefers all ranked to all unranked ones, and has no preference among all unranked ones.
For example, the right figure shows an ordered list of the preferences on six cosmetics A, B, C, D, E, F that an evaluator submitted. The cosmetics A, C, E, F are ranked with nonconsecutive integers of 1, 4, 1, 3, respectively. The other two cosmetics B and D are not ranked, thus they are marked with 0 . A and E have the same preference, which are the most preferred cosmetics because their preference number is the smallest positive integer. The unranked B and D are less preferred than the ranked ones. As a result, the preference order of this evaluator is A = E ≻ F ≻ C ≻ B = D, where X ≻ Y means that X is strictly preferred to Y and X = Y means the same preference.

Which cosmetics are the most preferred ones from n evaluators in this preference survey? We must now define which one is preferred to another. Let d(X, Y) be the number of evaluators who strictly prefer X to Y. A path from X to Y is a sequence of distinct cosmetics C1, ..., Ck such that C1 = X, Ck = Y, and d(Ct, Ct+1) > d(Ct+1, Ct) for every t = 1, ... , k − 1. The preference index of this path is defined as the minimum of d(Ct, Ct+1) for 1 ≤ t < k. For two distinct cosmetics X and Y that are connected by a path from X to Y, the preference strength S(X,Y) is the maximum preference index over all paths from X to Y. If there is no path from X to Y, then S(X,Y) is defined as zero. Cosmetic X is one of the most preferred cosmetics from this survey if and only if S(X,Y) ≥ S(Y,X) for every Y other than X. You note that it has been known for this type of surveys that there always exists at least one cosmetic that is preferred the most.

Given preference lists of n evaluators for m cosmetics, write a program to output all the most preferred cosmetics.

 

输入

Your program is to read from standard input. The input starts with a line containing two integers,m and n(1≤m≤500,1≤n≤500) , where m is the number of cosmetics and  n  is the number of evaluators. The cosmetics are numbered from 1 to m, and the evaluators are numbered from 1 to n  . In the following  n  lines, the  ݅i-th line contains m nonnegative integers that represent the preference values for them cosmetics of the 
evaluator  ݅ . The preference values are ordered from the cosmetic 1 to the cosmetic m. The zero values in the list mean that the evaluator unranked the corresponding cosmetics. The preference values are no more than 106. 

 

输出

Your program is to write to standard output. Print exactly one line. The line should contain the numbers of the cosmetics that are preferred the most. Such cosmetic numbers must appear in increasing order. 
 

 

样例输入

复制样例数据

3 4
1 1 1
0 0 0
2 2 2
3 3 3

样例输出

1 2 3

 

[提交][状态]

题意比较难理解,语言表达能力不行,题意还是自行理解吧

数据比较小,按照题意先暴力计算出d[ i ][ j ],因为只有d[ x ][ y ] > d[ y ][ x ],才可以成为一条路其中的连接点

所以找出所有满足d[ i ][ j ] > d[ j ][ i ]的边建图

每条路上最小的d数组的值一定是最后一条边,比如x->a->b->y,那么这条路的偏好值就是d[ b ][ y ]

也就是说遍历到一点时,将该点s的值与遍历的结果求最大值即可

训练赛时没发现最后一条边最小,写了好久dfs。自闭

代码:

#include 

using namespace std;
typedef long long ll;
const int maxn = 550;
const int inf = 0x3f3f3f3f;
int a[maxn];
int d[maxn][maxn];
int vis[maxn];
int s[maxn][maxn];
int p[maxn];
vector mapp[maxn];

void bfs(int e) {
    vis[e] = 1;
    queue q;
    q.push(e);
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        for (int i = 0; i < mapp[x].size(); i++) {
            int y = mapp[x][i], c = d[x][y];
            if (!vis[y]) q.push(y), vis[y] = 1;
            s[e][y] = max(s[e][y], c);
        }
    }
}

int main() {
    int n, m;
    scanf("%d%d", &m, &n);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf("%d", &a[j]);
            if (a[j] == 0) a[j] = inf;
        }
        for (int j = 1; j <= m; j++) {
            for (int o = j + 1; o <= m; o++) {
                if (a[j] < a[o])
                    d[j][o]++;
                else if (a[j] > a[o])
                    d[o][j]++;     
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= m; j++) {
            if (i == j) continue;
            if (d[i][j] > d[j][i])
                mapp[i].push_back(j); //建图
        }
    }
    for (int i = 1; i <= m; i++) {
        memset(vis, 0, sizeof(vis));
        bfs(i);
    }
    int k = 0;
    for (int i = 1; i <= m; i++) {
        int flag = 1;
        for (int j = 1; j <= m; j++) {
            if (i == j) continue;
            if (s[i][j] < s[j][i]) {   //s[x][y]>=s[y][x]满足要求,即只要出现<的情况,就是不满足的
                flag = 0; 
                break;
            }
        }
        if (flag) p[k++] = i;
    }
    for (int i = 0; i < k; i++) {
        if (!i) printf("%d", p[i]);
        else printf(" %d", p[i]);
    }
    printf("\n");
    return 0;
}

 

你可能感兴趣的:(ACM)