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 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
随机推荐
- MySQL如何判别InnoDB表是独立表空间还是共享表空间
InnoDB采用按表空间(tablespace)的方式进行存储数据, 默认配置情况下会有一个初始大小为10MB, 名字为ibdata1的文件, 该文件就是默认的表空间文件(tablespce file ...
- centos7中/tmp文件保存天数
不要在/tmp目录下保存文件,该目录会定期清理文件 /tmp默认保存10天 /var/tmp默认保存30天 配置文件:/usr/lib/tmpfiles.d/tmp.conf 默认配置文件:# Thi ...
- EF ORM
//新增 UserInfo userInfo = new UserInfo(); userInfo.UserName = "YANG"; userInfo.UserPass = & ...
- iOS 设置View阴影
iOS 设置View投影 需要设置 颜色 阴影半径 等元素 UIView *shadowView = [[UIView alloc] init]; shadowView.frame = CGRectM ...
- php $$可变变量理解
//在变量前面加上两个$$,如$$name,这表示可变变量,可以动态的设置和使用,先设置一个普通变量,一个可变变量会获取了一个普通变量的值作为这个可变变量的变量名 $a = 'b'; $b = 'c' ...
- php分页实现
<?php header("content-type:text/html;charset=utf8"); include 'conn.php'; //每页显示的数据条数 $p ...
- javascript基础之函数
javascript的函数定义与python有很大的区别,的记住格式就好,下面请看代码 // 函数 // 简单定义 function func() { console.log('hello word' ...
- SQL NOW() 函数
NOW() 函数 NOW 函数返回当前的日期和时间. 提示:如果您在使用 Sql Server 数据库,请使用 getdate() 函数来获得当前的日期时间. SQL NOW() 语法 SELECT ...
- ubuntu18.04搭建hive
hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成本低,可以通过 ...
- SQL 增删改语句
阅读目录 一:插入数据 二:更新数据 三:删除数据 回到顶部 一:插入数据 把数据插入表中的最简单方法是使用基本的 INSERT 语法.它的要求是需要我们指定表名和插入到新行中的值. 1.1 插入完整 ...