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 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
随机推荐
- Jmeter设置代理,抓包之app请求
步骤: 1. Jmeter选择测试计划,添加线程组,添加http请求,添加监听器-察看结果树 2. 添加http代理服务器,右键添加非测试元件-添加http代理服务器 3. 端口改为8889,目标控制 ...
- Linux查杀stopped进程
在Linux系统下面,top命令可以查看查看stopped进程.但是不能查看stopped进程的详细信息.那么如何查看stopped 进程,并且杀掉这些stopped进程呢? ps -e j | ...
- insert into select的实际用法
INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table ...
- (生活)Photoshop入门(不定时更新)
我可能是想找个工作以外的事情做一下. 目标:我要自学网PhotoShop商业修图. 笔记: .图层 .1总结: 1.1.1图层就好像画画的一张纸,但是每一层又互不影响. 1.1.2图层蒙版(覆盖一层玻 ...
- kernel笔记——内核同步与锁
内核同步 内核同步解决并发带来的问题,多个线程对同一数据进行修改,数据会出现不一致的情况,同步用于保护共享数据等资源. 有两种形式的并发: 同时进行式并发,在不同cpu上执行的进程同时访问共享数据 二 ...
- LoadRunner 11 error:Cannot initialize driver dll
LoadRunner 11 error:Cannot initialize driver dll 这个错误很容易解决,使用win7系统时,有些程序要以管理员身份才能运行. 解决方案:右键选择:“以管理 ...
- java中 & ^ ~ 的运算
java运算符 与(&).非(~).或(|).异或(^) 最近看HashMap源码,遇到了这样一段代码: 1 static final int hash(Object key) { 2 i ...
- 【题解】P2324 [SCOI2005]骑士精神
·有关IDA* 是带有估值函数的迭代加深搜索,表现出出色的效率. 估值函数可以简单的定义为「已经到位的骑士的个数」. 然后就是普通的迭代加深了. 算法酷炫不一定赢,搜索好才是成功. ——Loli Co ...
- day23--面向对象之封装、继承、多态
面向对象的三大特性: 封装: 在类的内部(class内部)可以由属性和方法,外部代码可以通过直接调用实例变量的方法来操作数据,这样就隐藏了内部的逻辑,但是外部还是可以直接修改实例的属性,因此当需求中存 ...
- pyspider煎蛋无聊图爬取
命令行pyspider,启动pyspider. web预览界面太小,解决方法:找到pyspider的安装路径下的debug.min.css,修改css代码: 将其中的iframe{border-wid ...