CF1131D Gourmet choice
题意
有两组菜,第一组有\(n\)种,第二组有\(m\)种。给出一个\(n\times m\)的矩阵,第\(i\)行第\(j\)列表示第一组中的第\(i\)种菜与第二组中的第\(j\)种菜好吃程度的比较。
如果为\('>'\)表示第一组中的第\(i\)种菜比第二组种的第\(j\)种菜更好吃。
如果为\('<'\),表示第二组种的第\(j\)种菜比第一组中的第\(i\)种菜更好吃。
如果为\('='\),表示两种菜同样好吃。
现在要给每种菜打上一个评分,要求好吃的菜的评分一定要比不好吃的更高。同样好吃的两种菜评分要相同。
第一行输出\("YES"\)表示可以完成。并在下面两行分别输出两组菜的评分。
如果无法完成,输出一行"NO"
思路
并查集+拓扑排序
首先把所有的相等的菜放到一个并查集里面去。
然后从不好吃的菜向好吃的连边。然后开始拓扑排序。
对于每道菜,比他更好吃的菜的评分都是他的评分\(+1\),如果无法拓扑,说明存在环,输出\("NO"\)即可。
代码
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<cstring>
#include<queue>
#include<bitset>
using namespace std;
typedef long long ll;
const int N = 3010;
ll read() {
ll x=0,f=1;char c=getchar();
while(c<'0'||c>'9') {
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
char s[N][N];
int vis[N],ans[N];
queue<int>q;
struct node {
int v,nxt;
}e[N * N];
int fa[N];
int head[N],ejs;
void add(int u,int v) {
e[++ejs].v = v;e[ejs].nxt = head[u];head[u] = ejs;
}
int du[N];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void uni(int x,int y) {
x = find(x),y = find(y);
if(rand() & 1) swap(x,y);
fa[x] = y;
}
int main() {
int n = read(),m = read();
for(int i = 1;i <= n + m;++i) fa[i] = i;
for(int i = 1;i <= n;++i) {
scanf("%s",s[i] + 1);
for(int j = 1;j <= m;++j) {
if(s[i][j] == '=') uni(i,j + n);
}
}
for(int i = 1;i <= n;++i) {
for(int j = 1;j <= m;++j) {
int x = find(i),y = find(j + n);
if(s[i][j] == '<') {
if(x == y) {
puts("NO");
return 0;
}
add(x,y);
du[y]++;
}
else if(s[i][j] == '>') {
if(x == y) {
puts("NO");return 0;
}
add(y,x);
du[x]++;
}
}
}
int tot = 0;
for(int i = 1;i <= n + m;++i) {
int x = find(i);if(vis[x]) continue;
tot++;
vis[x] = 1;
if(!du[x]) q.push(x),ans[x] = 1;
}
while(!q.empty()) {
int u = q.front();q.pop();tot--;
for(int i = head[u];i;i = e[i].nxt) {
int v = e[i].v;du[v]--;
if(!du[v]) q.push(v),ans[v] = ans[u] + 1;
}
}
if(tot) {
puts("NO");return 0;
}
puts("YES");
for(int i = 1;i <= n;++i)
printf("%d ",ans[find(i)]);
puts("");
for(int i = 1;i <= m;++i)
printf("%d ",ans[find(i + n)]);
return 0;
}
CF1131D Gourmet choice的更多相关文章
- CF1131D Gourmet choice(并查集,拓扑排序)
这题CF给的难度是2000,但我感觉没这么高啊…… 题目链接:CF原网 题目大意:有两个正整数序列 $a,b$,长度分别为 $n,m$.给出所有 $a_i$ 和 $b_j(1\le i\le n,1\ ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- coderfoces D. Gourmet choice
D. Gourmet choice time limit per test 2 seconds memory limit per test 256 megabytes 题目链接: https: ...
- D. Gourmet choice并查集,拓扑结构
D. Gourmet choice time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
- 【CF #541 D】 Gourmet choice
link:https://codeforces.com/contest/1131 题意: 给定一些大小比较,输出排名. 思路: 这道题我用的是拓扑排序,又因为有等于号的存在,我用了并查集. 结束后这道 ...
- CF - 1131 D Gourmet choice
题目传送门 先把 = 的人用并查集合并在一起. 然后 < > 的建边, 跑一遍 toposort 之后就好了. 入度为0点的值肯定为1, 然后就是因为这个是按照时间线走过来的,所以一个点的 ...
- CF#541 D. Gourmet choice /// BFS 拓扑
题目大意: 给定n m 第一行有n个数 第二行有m个数 接下来n行每行m列 有 = < > 位于 i j 的符号表示 第一行第i个数与第二行第j个数的大小关系 1.将n+m个数 当做按顺序 ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
随机推荐
- SVN下载与安装
首先打开浏览器上百度搜索“SVN”如下图: 或者点击:https://tortoisesvn.net/downloads.html 打开后链接后选择Downloads,选择需要下载的版本,比如我电脑是 ...
- go语言打造个人博客系统(二)
go语言打造个人博客系统(二) 在上篇文章go语言打造个人博客系统(一)中,我们了解了go语言的优点和go语言的数据库操作,本次我们会完成博客系统的后端开发. 博客系统后端接口开发 路由测试 ht ...
- CFS调度器(1)-基本原理
首先需要思考的问题是:什么是调度器(scheduler)?调度器的作用是什么?调度器是一个操作系统的核心部分.可以比作是CPU时间的管理员.调度器主要负责选择某些就绪的进程来执行.不同的调度器根据不同 ...
- phpstorm设置编码格式
phpstorm设置编码格式 默认: utf-8格式 设置方法: file -> settings -> Editor -> file encodng -> project e ...
- Kafka 安装配置 windows 下
Kafka 安装配置 windows 下 标签(空格分隔): Kafka Kafka 内核部分需要安装jdk, zookeeper. 安装JDK 安装JDK就不需要讲解了,安装完配置下JAVA_HOM ...
- Bootstrap -- 缩略图、进度条、列表组、面板
Bootstrap -- 缩略图.进度条.列表组.面板 1. 缩略图 大多数站点都需要在网格中布局图像.视频.文本等.Bootstrap 通过缩略图为此提供了一种简便的方式.使用 Bootstrap ...
- GitHub的初级使用
最近准备学习一个GitHub的使用 一.账号创建 1.百度找到GitHub官方网站(https://github.com/ ) 2.点击Sign up注册GitHub账号 下图为注册页面 第一步:填写 ...
- .net 添加api不能访问的问题
在一个.netmvc项目中,本身没有提供api后来想添加api就会出现问题.会发生添加的apicontrol不能访问的情况.这种情况一般是因为,global文件中,application_start( ...
- Ambari Metrics 详解
Ambari Metrics 原理 Ambari Metrics System 简称为 AMS,它主要为系统管理员提供了集群性能的监察功能.Metrics 一般分为 Cluster.Host 以及 S ...
- c# 日期函数DateTime.ToString()日期的各种格式
//c# datetime 格式化 DateTime dt = DateTime.Now; //2017/11/14 10:46:56 label1.Text = dt.ToString();//20 ...