码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-07-22 23:42:43      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:example   main   访问   cannot   and   namespace   cto   iter   long   

题目描述

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(二分答案+拓扑排序)

标签:example   main   访问   cannot   and   namespace   cto   iter   long   

原文地址:https://www.cnblogs.com/zyf3855923/p/9351710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!