6348: Milking Order(二分答案+拓扑排序)

Posted zyf3855923

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6348: Milking Order(二分答案+拓扑排序)相关的知识,希望对你有一定的参考价值。

题目描述

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行,我们就二分行数,得到答案。
#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
int a[maxn];
int ans[maxn];
vector<int> v[maxn/2];
struct edge
{
    int u,v,next;
}edge[maxn*2];
int head[maxn];
int cnt=0;
int ind[maxn];
void addedge(int u,int v)
{   ind[v]++;
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int n;
bool topsort()
{
    priority_queue<int,vector<int>,greater<int> >q;
    int tot=-1,i;
    for(i=1;i<=n;i++)
    {
        if(ind[i]==0) q.push(i);
    }
    while(!q.empty())
    {
        int u=q.top();
        ans[++tot]=u;
        q.pop();
        for(i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            ind[v]--;
            if(ind[v]==0) q.push(v);
        }
    }
    if(tot==n-1) return true;
    else return false;
}
using namespace std;
int main()
{
    int m,i,k,j;
    scanf("%d%d",&n,&m);
    for(i=0;i<m;i++)
    {
        scanf("%d",&k);
        for(j=1;j<=k;j++)
        {   int u;
            scanf("%d",&u);
            v[i].push_back(u);
        }
    }
    int l=0,r=m-1;
    int mm=0;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        cnt=0;
        memset(head,-1,sizeof(head));
        memset(edge,0,sizeof(edge));
        memset(ind,0,sizeof(ind));
        for(i=0;i<=mid;i++)
        {
            for(j=0;j<v[i].size()-1;j++)
            {
                addedge(v[i][j],v[i][j+1]);
            }
        }
        if(topsort())
        {
            l=mid+1;
            mm=max(mm,mid);
        }
        else
        {
            r=mid-1;
        }
    }
     memset(head,-1,sizeof(head));
        memset(edge,0,sizeof(edge));
        memset(ind,0,sizeof(ind));
        cnt=0;
    for(i=0;i<=mm;i++)
        {
            for(j=0;j<v[i].size()-1;j++)
            {
                addedge(v[i][j],v[i][j+1]);
            }
        }
        topsort();
            for(i=0;i<n-1;i++)
            {
                printf("%d ",ans[i]);
            }
            cout<<ans[i]<<endl;
    return 0;
}

  








以上是关于6348: Milking Order(二分答案+拓扑排序)的主要内容,如果未能解决你的问题,请参考以下文章

Milking Order

POJ2112:Optimal Milking(Floyd+二分图多重匹配+二分)

[Usaco2018 Open]Milking Order

POJ2112 Optimal Milking —— 二分图多重匹配/最大流 + 二分

poj2122 Optimal Milking

TOJ 1594 Optimal Milking(二分+最大流)