Milking Order(拓扑+二分+优先队列)

                                               Milking Order

                                                                         时间限制: 3 Sec  内存限制: 128 MB
                                                                                        提交: 127  解决: 44
                                                                         [提交] [状态] [讨论版] [命题人: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.

 

                                                                                 [提交]  [状态]

 

拓扑排序

题解

最小值最大(x)考虑二分,首先先把条件记录下来 方便以后建图用,然后以[1,M]为区间二分 mid

把前 mid 个条件建出图来

具体建图方法:从前一个点指向另一个点 有向图,再根据题目描述 判断是一个有向无环图 于是考虑拓扑

拓扑的目的很简单 就是判断有没有环,然后配合二分找到 临界的mid值,在拓扑一遍输出就可以了

因为是字典序 所以要用优先队列维护,定义小根堆忘了默认的排列顺序。

#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>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
 
 
int n,m;
vector<int>mp[MAX];
vector<int>g[MAX];
int du[MAX],L[MAX];
void build(int x)
{
    for(int i=0;i<MAX;i++)
        mp[i].clear();
    memset(du,0,sizeof(du));
 
    for(int i=1;i<=x;i++)//建立有向图
    {
        for(int j=0;j<g[i].size()-1;j++)
        {
            int p,q;
            p=g[i][j];
            q=g[i][j+1];
            mp[p].push_back(q);
            du[q]++;
        }
    }
 
}
int judge_topsort()
{
    int tot=0;
    priority_queue<int,vector<int>,greater<int> >Q;
    for(int i=1;i<=n;i++)
        if(!du[i])
            Q.push(i);
    while(!Q.empty())
    {
        int x=Q.top();
        Q.pop();
        L[tot++]=x;
        for(int j=0;j<mp[x].size();j++)
        {
            int t=mp[x][j];
            du[t]--;
            if(!du[t])
                Q.push(t);
        }
    }
    if(tot==n)
        return 1;
    return 0;
}
void solve(int x)
{
    build(x);
    int tot=0;
 
    priority_queue<int,vector<int>,greater<int> >Q;
    for(int i=1;i<=n;i++)
        if(!du[i])
            Q.push(i);
    while(!Q.empty())
    {
        int x=Q.top();
        Q.pop();
        L[tot++]=x;
        for(int j=0;j<mp[x].size();j++)
        {
            int t=mp[x][j];
            du[t]--;
            if(!du[t])
                Q.push(t);
        }
    }
    for(int i=0;i<n;i++)
        printf("%d ",L[i]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int p;
        scanf("%d",&p);
        while(p--)
        {
            int x;
            scanf("%d",&x);
            g[i].push_back(x);
        }
    }
 
    int l=0,mid,r=m;
    while(l<=r)//二分最终结果用r
    {
        mid=(l+r)/2;
        build(mid);
        if(judge_topsort())
            l=mid+1;
        else
            r=mid-1;
    }
    solve(r);
 
 
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值