codeforces1131D. Gourmet choice 建图

Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review — in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn’t like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer.
Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he’s confused about evaluating dishes.
The gourmet tasted a set of n
dishes on the first day and a set of m dishes on the second day. He made a table a of size n×m, in which he described his impressions. If, according to the expert, dish i from the first set was better than dish j from the second set, then aij is equal to “>”, in the opposite case aij is equal to “<”. Dishes also may be equally good, in this case aij is “=”.
Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if aij is “<”, then the number assigned to dish i from the first set should be less than the number of dish j from the second set, if aij is “>”, then it should be greater, and finally if aij is “=”, then the numbers should be the same.
Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible.

Input
The first line contains integers n and m (1≤n,m≤1000) — the number of dishes in both days.
Each of the next nlines contains a string of m symbols. The j-th symbol on i-th line is aij. All strings consist only of “<”, “>” and “=”.

Output
The first line of output should contain “Yes”, if it’s possible to do a correct evaluation for all the dishes, or “No” otherwise.
If case an answer exist, on the second line print n
integers — evaluations of dishes from the first set, and on the third line print m integers — evaluations of dishes from the second set.

Examples
Input

3 4
>>>>
>>>>
>>>>

Output

Yes
2 2 2 
1 1 1 1 

Input

3 3
>>>
<<<
>>>

Output

Yes
3 1 3 
2 2 2 

Input

3 2
==
=<
==

Output

No

美食家苹果在对一家餐厅进行评价时,有特殊的技巧。他在第一天去这个餐厅点N个菜,过几天再点M个,这两套中没有重复的菜品。然后他列出一个NxM的表格,表格中 aij表示第一次的第i道菜与第二次的第j道菜相比的感觉,只有 <(j比i好) >(i比j好) 和 =(i和j一样) 。
现在要根据这个表给每道菜一个具体的数值,要求最小值尽可能的小;如果不能给出具体数值,输出No。
因为首先要判断菜品之间关系是否有冲突,所以一开始是想用带权并查集来推算任意两点之间的关系。但是有时根据现有信息,不能确定两点之间关系,这样无法确定边的权值。
在这里插入图片描述
同样在这个图中我们也能发现,如果一组有向边组成了环,那么就会出现矛盾。
在这里插入图片描述
然后每个节点的入度表示有多少个节点比他大。这样可以一次拓扑排序得出每个节点的大小关系,再由这个关系赋值就得到答案了。
此外,还要注意=关系的处理。=关系表示这两道菜的评分一样,反映到图中就是这两个点的连接关系完全相同,所以我们可以用并查集来压缩相等的点。


#include <stdio.h>
#include <climits>
#include <cstring>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define STAR N
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return

#define getLen(name,index) name[index].size()
#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
struct Record{
    int index;
    int num;
};

int deg[2010],level[2010];
int par[2010];
char store[1005][1005];
vector<int> adj[2010];
queue<Record> Q;
int seekRoot(int x);
int main(){
    ios::sync_with_stdio(false);


    int N,M;
    cin>>N>>M;
    rep(i,1,N+1)
        rep(j,1,M+1)
            cin>>store[i][j];

    int all=N+M;
    rep(i,1,N+M+1){
        par[i]=i;
        deg[i]=0;
        level[i]=0;
    }
    rep(i,1,N+1)
        rep(j,1,M+1){
            if(store[i][j]=='='){
                int num=j+STAR;
                int rootA=seekRoot(num);
                int rootB=seekRoot(i);
                if(rootA==rootB){
                    ;
                }else{
                    par[rootA]=rootB;
                    all--;
                }
            }
        }
    rep(i,1,N+1){
        rep(j,1,M+1){
            int rootA=seekRoot(i);
            int rootB=seekRoot(j+STAR);
            switch(store[i][j]){
                case '<':
                    adj[rootB].Push(rootA);
                    deg[rootA]++;
                    break;
                case '>':
                    adj[rootA].Push(rootB);
                    deg[rootB]++;
                    break;
                default:
                    break;
            }
        }
    }

    //push in queue
    rep(i,1,N+M+1){
        if(par[i]==i && deg[i]==0)
            Q.push((Record){i,N+M+1});
    }
    int use=0;
    while(!Q.empty()){
        Record &Re=Q.front();
        int v=Re.index;
        level[v]=Re.num;
        use++;

        rep(i,0,getLen(adj,v)){
            int u=adj[v][i];
            deg[u]--;
            if(deg[u]==0){
                Q.push((Record){u,Re.num-1});
            }
        }

        Q.pop();
    }

    //finf arrange
    bool can=true;
    rep(i,1,N+M+1){
        if(deg[i]!=0){
            can=false;
            break;
        }
    }
    if(can){
        cout<<"Yes"<<endl;
        int minn=INT_MAX;
        rep(i,1,N+M+1){
            int root=seekRoot(i);
            minn=min(minn,level[root]);
        }
        minn--;
        rep(i,1,N+1){
            int root=seekRoot(i);
            cout<<level[root]-minn<<" ";
        }
        cout<<endl;
        rep(i,N+1,M+1+N){
            int root=seekRoot(i);
            cout<<level[root]-minn<<" ";
        }
        cout<<endl;
    }else{
        cout<<"No"<<endl;
    }

    re 0;
}
int seekRoot(int x){
    if(x==par[x])
        re x;
    else{
        int r=seekRoot(par[x]);
        re par[x]=r;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值