中石油第三套个人训练赛 C.Milking Order(二分+拓扑排序)

问题 C: Milking Order

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

题目描述

Farmer John's N cows (1≤N≤105), numbered 1…N as always, happen to have too much time on their hooves. As a result, they have worked out a complex social hierarchy related to the order in which Farmer John milks them every morning.
After weeks of study, Farmer John has made M observations about his cows' social structure (1≤M≤50,000). Each observation is an ordered list of some of his cows, indicating that these cows should be milked in the same order in which they appear in this list. For example, if one of Farmer John's observations is the list 2, 5, 1, Farmer John should milk cow 2 sometime before he milks cow 5, who should be milked sometime before he milks cow 1.

Farmer John's observations are prioritized, so his goal is to maximize the value of X for which his milking order meets the conditions outlined in the first X observations. If multiple milking orders satisfy these first X conditions, Farmer John believes that it is a longstanding tradition that cows with lower numbers outrank those with higher numbers, so he would like to milk the lowest-numbered cows first. More formally, if multiple milking orders satisfy these conditions, Farmer John would like to use the lexicographically smallest one. An ordering x is lexicographically smaller than an ordering y if for some j, xi=yi for all i<j and xj<yj (in other words, the two orderings are identical up to a certain point, at which x is smaller than yy).

Please help Farmer John determine the best order in which to milk his cows.

输入

The first line contains N and M. The next M lines each describe an observation. Line i+1 describes observation i, and starts with the number of cows mi listed in the observation followed by the list of mimi integers giving the ordering of cows in the observation. The sum of the mi's is at most 200,000.

输出

Output N space-separated integers, giving a permutation of 1…N containing the order in which Farmer John should milk his cows.

样例输入

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

样例输出

1 4 2 3

提示

Here, Farmer John has four cows and should milk cow 1 before cow 2 and cow 2 before cow 3 (the first observation), cow 4 before cow 2 (the second observation), and cow 3 before cow 4 and cow 4 before cow 1 (the third observation). The first two observations can be satisfied simultaneously, but Farmer John cannot meet all of these criteria at once, as to do so would require that cow 1 come before cow 3 and cow 3 before cow 1.

This means there are two possible orderings: 1 4 2 3 and 4 1 2 3, the first being lexicographically smaller.

题意:有m种序列,若构造一个序列,使得最多的前x个序列是其子序列,求x

题解:二分答案+拓扑排序找环即可,其中拓扑过程中可用优先队列装点,使其字典序最小。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#include<bitset>
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const long long INF=0x7FFFFFFFFFFFFFFFLL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
const int maxn=1e6+5;
int n,m,cnt,head[maxn],rd[maxn],vis[maxn];
vector<int>v[maxn];
struct edge
{
    int to,next;
}edge[maxn];

void add(int u,int v)
{
    edge[++cnt].next=head[u];
    edge[cnt].to=v;
    head[u]=cnt;
}

void build(int mid)
{
    mem(edge,0);
    mem(head,0);
    mem(rd,0);
    for(int i=1;i<=mid;i++)
        for(int j=0;j<v[i].size()-1;j++)
            add(v[i][j],v[i][j+1]),rd[v[i][j+1]]++;
}

int topsort(int x)
{
    int sum=0;
    priority_queue<int,vector<int>,greater<int> >q;
    mem(vis,0);
    for(int i=1;i<=n;i++)
        if(!rd[i])
        {
            sum++;
            q.push(i);
            vis[i]=1;
        }
    while(!q.empty())
    {
        int t=q.top();
        q.pop();
        if(x) printf("%d ",t);
        for(int i=head[t];i;i=edge[i].next)
        {
            rd[edge[i].to]--;
            if(rd[edge[i].to]==0 && vis[edge[i].to]==0)
            {
                q.push(edge[i].to);
                sum++;
                vis[edge[i].to]=1;
            }
        }
    }
    return sum==n;
}

int main()
{
    cin>>n>>m;

    for(int i=1;i<=m;i++)
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int x;
            scanf("%d",&x);
            v[i].push_back(x);
        }
    }

    int l=0,r=m+1,mid;
    while(l<=r)
    {
        mid=l+r>>1;
        build(mid);
        if(topsort(0))
            l=mid+1;
        else
            r=mid-1;
    }
    build(r);
    topsort(1);
    cout<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/81163137