CF - 1131 D Gourmet choice
先把 = 的人用并查集合并在一起。
然后 < > 的建边, 跑一遍 toposort 之后就好了。
入度为0点的值肯定为1, 然后就是因为这个是按照时间线走过来的,所以一个点的最小值,就是在入队的那一刻确定的, 即入度为0的时候,值就是现在的值+1。
注意就是不要同一个点入队多次。
代码:
/*
code by: zstu wxk
time: 2019/02/24
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 2e3 + ;
int pre[N];
int ind[N];
int val[N];
int n, m;
char s[N][N];
vector<int> vc[N];
int Find(int x){
if(x == pre[x]) return x;
return pre[x] = Find(pre[x]);
}
queue<int> q;
void toposort(){
for(int i = ; i <= n+m; ++i){
// cout << i << ' ' << ind[i] << endl;
if(i == Find(i) && ind[i] == ) {
q.push(i), val[i] = ;
// cout << i << endl;
}
}
while(!q.empty()){
int x = q.front();
q.pop();
for(int v : vc[x]){
ind[v]--;
if(ind[v] == ){
val[v] = val[x] + ;
q.push(v); }
}
}
}
void Ac(){
for(int i = ; i <= n + m; ++i) pre[i] = i, ind[i] = ;
for(int i = ; i <= n; ++i){
scanf("%s", s[i]+);
for(int j = ; j <= m; ++j){
if(s[i][j] == '='){
int u = Find(i), v = Find(n+j);
if(u == v) continue;
pre[u] = v;
}
}
}
for(int i = ; i <= n; ++i){
for(int j = ; j <= m; ++j){
if(s[i][j] == '=' ) continue;
int u = Find(i), v = Find(n+j);
if(s[i][j] == '<'){
vc[u].pb(v);
ind[v]++;
}
else {
vc[v].pb(u);
ind[u]++;
}
}
}
toposort();
for(int i = ; i <= n+m; ++i){
if(!val[Find(i)]){
puts("No");
return ;
}
}
puts("Yes");
for(int i = ; i <= n; ++i){
printf("%d%c", val[Find(i)]," \n"[i==n]);
}
for(int i = ; i <= m; ++i){
printf("%d%c", val[Find(i+n)], " \n"[i==m]);
}
}
int main(){
while(~scanf("%d%d", &n, &m)){
Ac();
}
return ;
}
CF - 1131 D Gourmet choice的更多相关文章
- CF#541 D. Gourmet choice /// BFS 拓扑
题目大意: 给定n m 第一行有n个数 第二行有m个数 接下来n行每行m列 有 = < > 位于 i j 的符号表示 第一行第i个数与第二行第j个数的大小关系 1.将n+m个数 当做按顺序 ...
- 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 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- 【CF #541 D】 Gourmet choice
link:https://codeforces.com/contest/1131 题意: 给定一些大小比较,输出排名. 思路: 这道题我用的是拓扑排序,又因为有等于号的存在,我用了并查集. 结束后这道 ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
- CF1131D Gourmet choice(并查集,拓扑排序)
这题CF给的难度是2000,但我感觉没这么高啊…… 题目链接:CF原网 题目大意:有两个正整数序列 $a,b$,长度分别为 $n,m$.给出所有 $a_i$ 和 $b_j(1\le i\le n,1\ ...
- CF1131D Gourmet choice
题目链接 题意 有两组菜,第一组有\(n\)种,第二组有\(m\)种.给出一个\(n\times m\)的矩阵,第\(i\)行第\(j\)列表示第一组中的第\(i\)种菜与第二组中的第\(j\)种菜好 ...
- CF 1131 E. String Multiplication
E. String Multiplication 题意 分析: 从后往前考虑字符串变成什么样子. 设$S_i = p_1 \cdot p_2 \dots p_{i}$,最后一定是$S_{n - 1} ...
随机推荐
- react开发中的小细节
目前开始使用react余遇到的问题还不是很多,但还是希望总结一下. react中的属性prop: 在react中组件的父子组件的通信是基于prop的,当然对于底层的东西不是特别了解,但可以说一说它的基 ...
- HttpsUtils
package io.renren.modules.jqr.util; import java.io.BufferedReader; import java.io.InputStream; impor ...
- RocketMQ中Broker的启动源码分析(二)
接着上一篇博客 [RocketMQ中Broker的启动源码分析(一)] 在完成准备工作后,调用start方法: public static BrokerController start(Broker ...
- Hyper-V虚拟机上安装Ubuntu16.04/Ubuntu18.04.2LTS,搭建GitLab
我的电脑系统是win10,内存8g如下 一开始是装的Ubuntu18.04.2LTS, gitlab-ce_12.1.3-ce.0_amd64.deb,每次能够安装成功,但是修改完ip后,运行gitl ...
- Python基础总结之初步认识---class类的继承(下)。第十五天开始(新手可相互督促
年薪百万的步伐慢了两天hhhh严格意义是三天.最近买了新的玩具,在家玩玩玩!~~~~ 今天开始正式认识类的继承.类的继承是怎么继承呢?看下代码: class Animal(object): #父类 d ...
- js 数组对象深拷贝
js 数组对象深拷贝 结论:对象的拷贝不能采用直接赋值的方式. 背景 踩过的坑如下: formData本来是父组件传过来的,但是我不想直接用,于是我直接赋值给一个formDataCopy的对象. 但是 ...
- 转载 | SVG向下兼容优雅降级方法
本文引自:http://www.zhangxinxu.com/wordpress/2013/09/svg-fallbacks/ 1.svg image标签降级技术 <svg width=&quo ...
- JavaScript循环出现的问题——用闭包来解决
在for循环中,数组长度为3,我本来是想对每个循环的元素绑定一个点击事件的,结果点击后控制台输出全部为1. for (var i = 0; i < data.data.length; i++) ...
- Java——异常处理
1.java提供的异常不可能预见所有的问题,所以需要自己定义异常类,必须从已有的异常类继承,最好选择意思相近的异常类继承. class MyException extends Exception{} ...
- Vue 中使用 typescript
Vue 中使用 typescript 什么是typescript typescript 为 javaScript的超集,这意味着它支持所有都JavaScript都语法.它很像JavaScript都强类 ...