欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

7710: MicroRNA Ranking(拓扑排序+优先队列)

程序员文章站 2024-03-19 09:46:46
...

7710: MicroRNA Ranking

时间限制: 2 Sec  内存限制: 128 MB
提交: 121  解决: 40
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Ahlaam is a computer science student, doing her master thesis on a bioinformatics project about MicroRNAs, special molecule types found in cells. During her thesis, she wants to find microRNAs relevant to a specific health factor in human beings.
Ahlaam has designed k microRNA ranking algorithms, each of which ranks microRNAs from a specific point of view.
There are n microRNAs numbered 1 through n, and each algorithm produces one permutation of these n microRNAs. In the permutation produced by each algorithm, the first microRNA is inferred by the algorithm as the most relevant one to the health factor, and the last microRNA is inferred as the least relevant one. 
Ahlaam wants to report a consensus ranking on microRNAs. In a consensus ranking, if microRNA i is ranked before another mircroRNA j, then at least half of the algorithms should have ranked i before j. Write a program to help Ahlaam find a consensus ranking.

 

输入

There are multiple test cases in the input. The first line of each test contains two space-separated integers n (1 ⩽ n ⩽ 1000) and k (1 ⩽ k ⩽ 200), the number of microRNAs and the number of ranking algorithms, respectively. Then, there are k lines, where the i-th line contains a permutation of n numbers 1, . . . , n, representing the output of the i-th ranking algorithm. The input terminates with a line containing 0 0 which should not be processed.

 

输出

For each test case, print a single line containing a permutation of n numbers 1, . . . , n, representing a possible consensus ranking. If there are more than one correct consensus rankings, print the first one in lexicographic order (a sequence a1 , . . . , an is lexicographically less than a sequence b1 , . . . , bn iff there exists a positive integer j such that ai = bi for all 1 ⩽ i ⩽ j   1 and aj < bj ) . If no such a ranking exists, write “No solution” instead.

 

样例输入

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

 

样例输出

2 4 1 5 3
1 2 3 4 5
No solution

 

来源/分类

Tehran2016 

 

按照题目给定的k个序列然后找出字典序最小序列。

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+10;
int cnt;
int n,k;
int flag;
int a[maxn][maxn];
int mapp[maxn][maxn];
int head[maxn];
int inv[maxn];
int ans[maxn];
struct edge{
    int v,next;
}edge[1000*maxn];
void add(int u,int v)
{
    edge[cnt].v = v;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
void trop()
{
    priority_queue < int , vector<int> , greater<int> > q;
    for(int i = 1;i <= n;i++)
    {
        if(inv[i] == 0)
        q.push(i);
    }
    while(!q.empty())
    {
        flag++;
        int u = q.top();
        ans[flag] = u;
        //cout<<u<<endl;
        q.pop();
        for(int i = head[u];~i;i = edge[i].next)
        {
            inv[edge[i].v]--;
            if(inv[edge[i].v] == 0)
            q.push(edge[i].v);
        }
    }
}
int main()
{
 
    while(~scanf("%d%d",&n,&k)&&n!=0&&k!=0)
    {
        memset(head,-1,sizeof(head));
        memset(inv,0,sizeof(inv));
        memset(mapp,0,sizeof(mapp));
        cnt = 0;flag = 0;
        for(int i = 0;i < k;i++)
            for(int j = 0;j < n;j++)
            scanf("%d",&a[i][j]);
        for(int i = 0; i < k;i++)
        {
            for(int j = 0;j < n;j++)
            {
                for(int k = j+1;k < n;k++)
                    mapp[a[i][j]][a[i][k]]++;
            }
        }
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
             
                if(mapp[i][j] > k/2)
                {
                    add(i,j);
                    inv[j]++;
                }
            }
             
        }
        trop();
        if(flag != n)
            printf("No solution\n");
        else
            {
                for(int i = 1;i <= n;i++)
                printf("%d%c",ans[i],i == n?'\n':' ');
            }   
    }
    return 0;
}

 

上一篇: 生活记录

下一篇: Range Optimization