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.

题意:

Farmer John的 NN 头奶牛( 1 \leq N \leq 10^51≤N≤105 ),仍然编号为 1 \ldots N1…N ,正好闲得发慌。因此,她们发展了一个与Farmer John每天早上为她们挤牛奶的时候的排队顺序相关的复杂的社会阶层。

经过若干周的研究,Farmer John对他的奶牛的社会结构总计进行了 MM 次观察( 1 \leq M \leq 50,0001≤M≤50,000 )。每个观察结果都是他的某些奶牛的一个有序序列,表示这些奶牛应该以与她们在序列中出现的顺序相同的顺序进行挤奶。比方说,如果Farmer John的一次观察结果是序列2、5、1,Farmer John应该在给奶牛5挤奶之前的某个时刻给奶牛2挤奶,在给奶牛1挤奶之前的某个时刻给奶牛5挤奶。

Farmer John的观察结果是按优先级排列的,所以他的目标是最大化 XX 的值,使得他的挤奶顺序能够符合前 XX 个观察结果描述的状态。当多种挤奶顺序都能符合前 XX 个状态时,Farmer John相信一个长期以来的传统——编号较小的奶牛的地位高于编号较大的奶牛,所以他会最先给编号最小的奶牛挤奶。更加正式地说,如果有多个挤奶顺序符合这些状态,Farmer John会采用字典序最小的那一个。挤奶顺序 xx 的字典序比挤奶顺序 yy 要小,如果对于某个 jj , x_i = y_ixi​=yi​ 对所有 i < ji<j 成立,并且 x_j < y_jxj​<yj​ (也就是说,这两个挤奶顺序到某个位置之前都是完全相同的,在这个位置上 xx 比 yy 要小)。

请帮助Farmer John求出为奶牛挤奶的最佳顺序。

思路:

如果Farmer John的一次观察结果是序列2、5、1,Farmer John应该在给奶牛5挤奶之前的某个时刻给奶牛2挤奶,在给奶牛1挤奶之前的某个时刻给奶牛5挤奶。

看了下拓扑排序的定义,这里是符合的

那么

Farmer John的观察结果是按优先级排列的,所以他的目标是最大化 X的值,使得他的挤奶顺序能够符合前 X个观察结果描述的状态

这里就要考虑二分了,

后面还要求字典序,那么优先队列维护

 

#include<bits/stdc++.h>
using namespace std;
struct node{
    int x;
    node(int b):x(b){}
    bool operator < (const node &a) const
    {
        return a.x < x;
    }
};
const int L=50005;
int n,m,l,r,ind[L*2],ans;
vector<int>p[L*2],Map[L*2];
priority_queue<node>g;
void build(int x)
{
    memset(ind,0,sizeof(ind));
    memset(Map,0,sizeof(Map));
    for(int i=1;i<=x;i++)
    {
        for(int j=0;j<p[i].size()-1;j++)
        {
            Map[p[i][j]].push_back(p[i][j+1]);
            ind[p[i][j+1]]++;
        }
    }
}
int topo()
{
    int num=0;
    for(int i=1;i<=n;i++)
    {
        if(!ind[i])
        {
            g.push(i);
            num++;
        }
    }
 
    int temp;
    while(!g.empty())
    {
        temp=g.top().x;
        g.pop();
        for(int i=0;i<Map[temp].size();i++)
        {
            ind[Map[temp][i]]--;
            if(!ind[Map[temp][i]])
            {
                g.push(Map[temp][i]);
                num++;
            }
        }
    }
    if(num==n)
        return 1;
    return 0;
}
int find_ans(int x)
{
    build(x);
    for(int i=1;i<=n;i++)
        if(!ind[i])
            g.push(i);
    int temp;
    while(!g.empty())
    {
        temp=g.top().x;
        printf("%d ",temp);
        g.pop();
        for(int i=0;i<Map[temp].size();i++)
        {
            ind[Map[temp][i]]--;
            if(!ind[Map[temp][i]])
            {
                g.push(Map[temp][i]);
            }
        }
    }
}
int check(int x)
{
    build(x);
    return topo();
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int t;
        scanf("%d",&t);
        for(int j=1;j<=t;j++)
        {
            int q;
            scanf("%d",&q);
            p[i].push_back(q);
        }
    }
    l=0,r=m+1;
    while(r>=l)
    {
        int mid=(l+r)>>1;
        if(check(mid)==1)
        {
            l=mid+1;
            ans=mid;
        }
        else
            r=mid-1;
    }
    find_ans(ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Based on the following story, continue the story by writing two paragraphs, paragraph 1 beginning with "A few weeks later, I went to the farm again. " and paragraph 2 beginning with "I was just about to leave when the hummingbird appeared."respectively with 150 words. I was invited to a cookout on an old friend's farm in western Washington. I parked my car outside the farm and walked past a milking house which had apparently not been used in many years.A noise at a window caught my attention,so I entered it. It was a hummingbird,desperately trying to escape. She was covered in spider-webs and was barely able to move her wings. She ceased her struggle the instant I picked her up. With the bird in my cupped hand, I looked around to see how she had gotten in. The broken window glass was the likely answer. I stuffed a piece of cloth into the hole and took her outside,closing the door securely behind me. When I opened my hand, the bird did not fly away; she sat looking at me with her bright eyes.I removed the sticky spider-webs that covered her head and wings. Still, she made no attempt to fly.Perhaps she had been struggling against the window too long and was too tired? Or too thirsty? As I carried her up the blackberry-lined path toward my car where I kept a water bottle, she began to move. I stopped, and she soon took wing but did not immediately fly away. Hovering,she approached within six inches of my face. For a very long moment,this tiny creature looked into my eyes, turning her head from side to side. Then she flew quickly out of sight. During the cookout, I told my hosts about the hummingbird incident. They promised to fix the window. As I was departing, my friends walked me to my car. I was standing by the car when a hummingbird flew to the center of our group and began hovering. She turned from person to person until she came to me. She again looked directly into my eyes, then let out a squeaking call and was gone. For a moment, all were speechless. Then someone said, “She must have come to say good-bye.”
02-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值