CUP 6348 Milking Order(二分+拓扑排序)

7-29补7-22:
http://exam.upc.edu.cn/problem.php?cid=1388&pid=2

题目描述
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

题目大意,给出观察的顺序,求一符合最多观察的字典序最小的序列

思路:

先二分,缩小搜索范围,在二分的范围内利用拓扑排序判断有没有环,有环向前二分,无环向后二分,直至找到一最大无环观察,根据这些观察确定最终的序列。
其中拓扑过程中可用优先队列装点,使其字典序最小。

有向图判环:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int cnt=0;
int vis[maxn];
 string a,b,c;
vector<int>vec[maxn];
bool flag=false;
map<string,int>p;
void dfs(int x) {
    vis[x]=-1;
    if(flag)return;
    for(int i=0; i<vec[x].size(); i++) {
        int v=vec[x][i];
        if(vis[v]==-1) {
            flag=true;//有环
            return;
        }
        else if(!vis[v])dfs(v);
    }
    vis[x]=1;
}
int main() {

    int n;
    scanf("%d\n",&n);
    while(n--) {
        cin >> a,
        cin >> b;
        cin >> c;
        if(!p[a])p[a]=++cnt;
        if(!p[c])p[c]=++cnt;//记录该是第几个进来的名字
        if(b[0]=='>')vec[p[a]].push_back(p[c]);
        else vec[p[c]].push_back(p[a]);
    }
    for(int i=1; i<=cnt; i++) {
        if(flag)break;
        if(!vis[i])dfs(i);
    }
    if(flag)printf("impossible\n");
    else printf("possible\n");
    return 0;
}

//二分查找,缩短查找范围,增加速度,
//拓扑排序看看有没有环,找无环的最大范围
#include <bits/stdc++.h>

using namespace std;
#define maxn 200005
typedef long long ll;
using namespace std;
int cnt=0;//作为下标使用
int head[maxn];//下标为值,不为空时,表明 在edge中存储的位置
int num[maxn];//统计入度不为零的个数
int vis[maxn];//记录是否被访问
vector<int> v[maxn];//每次 观察的序列
int n,m;
struct edge
{
    int to,next;
}edge[maxn];
void add(int u,int v){
 edge[++cnt].next=head[u];
 edge[cnt].to=v;
 head[u]=cnt;
//构造有向图
}
void build(int mid){
memset(edge,0,sizeof(edge));
memset(head,0,sizeof(head));
memset(num,0,sizeof(num));
for (int i=1;i<=mid;i++)
    for(int j=0;j<v[i].size()-1;j++)
       add(v[i][j],v[i][j+1]),num[v[i][j+1]]++;//记录这个数作为多少人的出度

}
int topsort(int x){

int sum=0;
priority_queue<int,vector<int>,greater<int> >q;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
    if(!num[i]){//如果入度为0,压入栈中,并且标记已访问
        sum++;
        q.push(i);
        vis[i]=1;
    }
while(!q.empty()){//如果队列不是空
    int t=q.top();
    q.pop();
    if(x)printf("%d ",t);//字典序 最小且入度为0的点

   for(int i=head[t];i;i=edge[i].next){
            //head[t]中存储的是最后一个出现的t,next中存储去前面哪个位置找另一后续
        num[edge[i].to]--;//作为t的后续,入度数减一
        if(num[edge[i].to]==0 && vis[edge[i].to]==0)
            {
                q.push(edge[i].to);
                sum++;
                vis[edge[i].to]=1;
            }

        /*if(num[edge[i].to]==0&&vis[edge[i].to]==0){
                //去掉前面t的箭头,入度为零了,并且没有被访问
           q.push(edge[i].to);
                sum++;
                vis[edge[i].to]=1;

        }*/

        //在优先队列中存储的总是经过前面的变化,入度为零的点
        //而我们 拿出一个字典序最小的就直接输出了,并没有存储
    }
}
         return sum==n;
}

int main()
{
    scanf("%d%d",&n,&m);

    for(int i=1;i<=m;i++)
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int x;
            scanf("%d",&x);
            v[i].push_back(x);
        }
    }

    int l=0,r=m+1,mid;
    while(l<=r)//二分
    {
        mid=l+r>>1;
        build(mid);//建立左半边的序列
        if(topsort(0))//对左半边进行拓扑
            l=mid+1;//无环,向右找
        else
            r=mid-1;//有环,向左找
    }
    build(r);//建立最终找到的最大的无环序列
    topsort(1);//输出字典序最小的序列
    cout<<endl;
    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、付费专栏及课程。

余额充值