codeforces #541 D. 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 nn dishes on the first day and a set of mm dishes on the second day. He made a table aa of size n×mn×m, in which he described his impressions. If, according to the expert, dish ii from the first set was better than dish jj from the second set, then aijaij is equal to ">", in the opposite case aijaij is equal to "<". Dishes also may be equally good, in this case aijaij 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 aijaij is "<", then the number assigned to dish ii from the first set should be less than the number of dish jj from the second set, if aijaij is ">", then it should be greater, and finally if aijaij 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.
The first line contains integers nn and mm (1≤n,m≤10001≤n,m≤1000) — the number of dishes in both days.
Each of the next nn lines contains a string of mm symbols. The jj-th symbol on ii-th line is aijaij. All strings consist only of "<", ">" and "=".
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 nn integers — evaluations of dishes from the first set, and on the third line print mmintegers — evaluations of dishes from the second set.
3 4
>>>>
>>>>
>>>>
Yes
2 2 2
1 1 1 1
3 3
>>>
<<<
>>>
Yes
3 1 3
2 2 2
3 2
==
=<
==
第一天有n个菜,第二天有m个菜。
给一个n*m矩阵,含有(或=、<),a(i,j)指的是:第一天的第i件菜的美味度>(或=、<)第二天的第j件菜的美味度。求出满足这个矩阵的n+m件菜的各自的美味度,要求使用的最大数字尽量小。
思路:如果是等号就用并查集连在一起(缩点) 其他的就拓扑排序
#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int n,m;
int f[];
char G[][];
vector<int> v[];
int ru[];
int ans[];
bool flag;
int find(int x){
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
void join(int x,int y){
int xx=find(x); int yy=find(y);
if(xx!=yy){
f[yy]=xx;
}
}
void toposort(){
queue<pair<int,int> > q;
for(int i=;i<=n+m;i++)
if(!ru[i]&&i==find(i)) q.push(make_pair(i,));
while(!q.empty()){
int x=q.front().first;
int y=q.front().second;
q.pop();
ans[x]=y;
int len=v[x].size();
for(int i=;i<len;i++){
int to=find(v[x][i]);
ru[to]--;
if(!ru[to]){
q.push(make_pair(to,y+));
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=;i<=n+m;i++) f[i]=i;
flag=;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
cin>>G[i][j];
if(G[i][j]=='=') //合并
join(i,j+n);
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
if(G[i][j]=='=') continue;
int xx=find(i); int yy=find(n+j);
if(xx==yy){
flag=;
break;
}
if(G[i][j]=='>') v[yy].push_back(xx),ru[xx]++; //计算入度
if(G[i][j]=='<') v[xx].push_back(yy),ru[yy]++;
if(!flag) break;
} if(!flag) cout<<"No"<<endl;
else{
toposort();
for(int i=;i<=n+m;i++)
if(ans[find(i)]==){
flag=;
break;
}
if(!flag) cout<<"No"<<endl;
else{
cout<<"Yes"<<endl;
for(int i=;i<=n;i++)
cout<<ans[find(i)]<<" ";
cout<<endl;
for(int i=;i<=m;i++)
cout<<ans[find(n+i)]<<" ";
cout<<endl;
}
}
return ;
}
codeforces #541 D. Gourmet choice(拓扑+并查集)的更多相关文章
- CF1131D Gourmet choice(并查集,拓扑排序)
这题CF给的难度是2000,但我感觉没这么高啊…… 题目链接:CF原网 题目大意:有两个正整数序列 $a,b$,长度分别为 $n,m$.给出所有 $a_i$ 和 $b_j(1\le i\le n,1\ ...
- codeforces #541 F Asya And Kittens(并查集+输出路径)
F. Asya And Kittens Asya loves animals very much. Recently, she purchased nn kittens, enumerated the ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces 766D. Mahmoud and a Dictionary 并查集 二元敌对关系 点拆分
D. Mahmoud and a Dictionary time limit per test:4 seconds memory limit per test:256 megabytes input: ...
- CodeForces Roads not only in Berland(并查集)
H - Roads not only in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces #345div1 C Table Compression (650C) 并查集
题意:给你一个n*m的矩阵,需要在不改变每一行和每一列的大小关系的情况下压缩一个矩阵,压缩后的矩阵所有数的总和尽量的小. 思路:我们有这样的初步设想:对于在一行或一列的数x,y,若x<y,则建立 ...
- hdu 1811 Rank of Tetris (拓扑 & 并查集)
Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- NOIp 2015信息传递【tarjan/拓扑/并查集】
一道好的NOIp题目,在赛场上总能用许多算法A掉.比如这道和关押罪犯. 题目传送门 法一:tarjan在有向图中跑最小环 有人从别人口中得知自己信息,等效于出现了一个环.于是 这就变成了一个有向图ta ...
随机推荐
- 导出数据之CSV
平常开发中,常见的需求就是导出数据为Excel,CSV格式的表格.所以,在此记录一下导出CSV数据的小方法 $fileName = 'demo.csv'; $data = [ ['id'=>1, ...
- Oracle RMAN备份与还原
RMAN在数据库服务器的帮助下实现数据库文件.控制文件.数据库文件与控制文件的映像副本.归档日志文件.数据库服务器参数文件的备份. RMAN的特点: (1) 支持增量备份:传统的exp与expdp备份 ...
- Python 基础之----网络编程
阅读目录 一 客户端/服务端架构 二 osi七层 三 socket层 四 socket是什么 五 套接字发展史及分类 六 套接字工作流程 七 基于TCP的套接字 八 基于UDP的套接字 九 粘包现象 ...
- java中间缓存变量机制
public static void main(String[] args){ int j = 0; for(int i = 0; i < 100; i++) j = j++; System.o ...
- Netty ByteBuf 和 String 转换
参考https://blog.csdn.net/o1101574955/article/details/81024102 参考http://youyu4.iteye.com/blog/2361959 ...
- python3 写的一个压测脚本(有待开发)
import requests import queue import threading import time status_code_list = [] exec_time = 0 class ...
- Delphi中带缓存的数据更新技术
一. 概念 在网络环境下,数据库应用程序是c/s或者是多层结构的模式.在这种环境下,数据库应用程序的开发应当尽可能考虑减少网络数据传输量,并且尽量提高并发度.基于这个目的,带缓存的数据更新技术应运而生 ...
- 猜数字游戏 在控制台运行--java详解!了;来玩
import java.util.Scanner;//导入包 import java.util.Scanner; 注意格式 符号的使用 public class Demo{ //猜数字游戏 练习 pu ...
- Python——数组模块(array)
一.模块说明 array模块是python中实现的一种高效的数组存储类型.它和list相似,但是所有的数组成员必须是同一种类型,在创建数组的时候,就确定了数组的类型. 二.代码
- 在 Web 页面使用 VLC 插件播放 m3u8 视频流 (360 极速模式)
1. 背景 公司有个旧项目需要添加在线播放 m3u8 视频流,但是该流不知道什么原因使用 Video.js 或 hls.js 均无法播放,最后找到解决方案可使用 VLC 插件播放(360 极速模式下) ...