upc 6348Milking Order【二分+拓扑排序】

6348: Milking Order

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

题意:有n头奶牛和m个给奶牛挤牛奶的顺序规则,规则中必须按顺序挤牛奶,要求使用前X个规则,X要尽可能大,输出字典序最小的挤牛奶的序列。

思路:使用前X个规则并且要求X最大,容易想到用二分求出,对于一个顺序关系(i,j),可以使用单向边i→j表示,把规则转换成有向图后易得输出的答案序列为拓扑排序的最小字典序,拓扑排序时使用优先队列保持最小字典序即可。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
using namespace std;
#define pb push_back
#define M(a,b) memset(a,b,sizeof(a))
typedef long long LL;
const int maxn = 100000+5;
const LL mod = 1000000007;
vector<int>g[maxn>>1];
vector<int>q[maxn];
int vis[maxn];
int ins[maxn];
int n,m;
int dfn[maxn],low[maxn];
int id[maxn];
stack<int>s;
int cnt,tot;
void dfs(int u,int pre) {
    dfn[u]=low[u]=++cnt;
    ins[u]=1;
    s.push(u);
    for (int i=0;i<q[u].size();++i) {
        int &v=q[u][i];
        if (!dfn[v]) {
            dfs(v,u);
            low[u]=min(low[u],low[v]);
        }
        else if (ins[v]) {
            low[u]=min(dfn[v],low[u]);
        }
    }
    if (dfn[u]==low[u]) {
        ++tot;
        int tmp;
        do{
            tmp=s.top();
            s.pop();
            id[tmp]=tot;
            ins[tmp]=0;
        }while(tmp!=u);
    }
}
int check(int x) {
    for (int i=1;i<=n;++i) {
        q[i].clear();
        vis[i]=0;
        dfn[i]=low[i]=0;
    }
    for (int i=1;i<=x;++i) {
        for (int j=0;j<g[i].size()-1;++j) {
            q[g[i][j]].pb(g[i][j+1]);
        }
    }
    cnt=tot=0;
    for (int i=1;i<=n;++i) {
        if (!dfn[i]) {
            dfs(i,-1);
        }
    }
    if (tot!=n) {
        return 0;
    }
    return 1;
}
int in[maxn]={0};
int main() {
 
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;++i) {
        int x,a;
        scanf("%d",&x);
        while(x--) {
            scanf("%d",&a);
            g[i].pb(a);
        }
    }
    int l=1,r=m;
    while(l<=r) {
        int mid = l+r>>1;
        if (check(mid)) {
            l=mid+1;
        }
        else {
            r=mid-1;
        }
    }
    if (!check(l)) {
        --l;
    }
    for (int i=1;i<=n;++i) {
        q[i].clear();
    }
    for (int i=1;i<=l;++i) {
        for (int j=0;j<g[i].size()-1;++j) {
            q[g[i][j]].pb(g[i][j+1]);
        }
    }
    for (int i=1;i<=n;++i) {
        for (int j=0;j<q[i].size();++j) {
            ++in[q[i][j]];
        }
    }
    priority_queue<int,vector<int>,greater<int> >mp;
    vector<int>ans;
    for (int i=1;i<=n;++i) {
        if (!in[i]) {
            in[i]=-1;
            mp.push(i);
        }
    }
    while(!mp.empty()) {
        int fr = mp.top();
        mp.pop();
        ans.pb(fr);
        for (int i=0;i<q[fr].size();++i) {
            int &v=q[fr][i];
            --in[v];
            if (!in[v]) {
                in[v]=-1;
                mp.push(v);
            }
        }
    }
    for (int i=0;i<ans.size();++i) {
        printf("%d%c",ans[i],i!=ans.size()-1?' ':'\n');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38000095/article/details/81232823
UPC