Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice
Time Limit: 2000 mSec
Problem Description

Input

Output
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 mm integers — evaluations of dishes from the second set.
Sample Input
3 4
>>>>
>>>>
>>>>
Sample Output
Yes
2 2 2
1 1 1 1
题解:一看就是拓扑排序,一通敲之后发现第三个样例过不去,原因是没有妥善处理等号的问题,其实很容易解决,相等的节点缩成一个,用并查集很容易实现,之后再拓扑排序就没问题了。
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 1; i <= (n); i++)
#define sqr(x) ((x) * (x)) const int maxn = + ;
const int maxm = + ;
const int maxs = + ; typedef long long LL;
typedef pair<int, int> pii;
typedef pair<double, double> pdd; const LL unit = 1LL;
const int INF = 0x3f3f3f3f;
const LL mod = ;
const double eps = 1e-;
const double inf = 1e15;
const double pi = acos(-1.0); int n, m;
string str[maxn];
vector<int> G[maxn];
int deg[maxn], ans[maxn];
int fa[maxn];
int Min, tot; int findn(int x)
{
return x == fa[x] ? x : fa[x] = findn(fa[x]);
} void merge(int x, int y)
{
int fx = findn(x), fy = findn(y);
if (fx != fy)
{
fa[fx] = fy;
}
} bool toposort()
{
queue<int> que;
Min = ;
int cnt = ;
for (int i = ; i < n + m; i++)
{
if (findn(i) == i && !deg[i])
{
que.push(i);
cnt++;
ans[i] = ;
}
}
while (!que.empty())
{
int u = que.front();
que.pop();
for (auto v : G[u])
{
int fv = findn(v);
if (fv == v)
{
deg[fv]--;
if (!deg[fv])
{
cnt++;
que.push(fv);
ans[fv] = ans[u] - ;
Min = min(ans[fv], Min);
}
}
}
}
return cnt == tot;
} void output()
{
cout << "YES" << endl;
for (int i = ; i < n; i++)
{
cout << ans[findn(i)] - Min + ;
if (i != n - )
{
cout << " ";
}
else
{
cout << endl;
}
}
for (int i = n; i < n + m; i++)
{
cout << ans[findn(i)] - Min + ;
if (i != n - )
{
cout << " ";
}
else
{
cout << endl;
}
}
} void premanagement()
{
for (int i = ; i < n + m; i++)
{
if (findn(i) == i)
{
tot++;
}
}
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n >> m;
for(int i = ; i < n + m; i++)
{
fa[i] = i;
}
for (int i = ; i < n; i++)
{
cin >> str[i];
}
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
if (str[i][j] == '=')
{
merge(i, n + j);
}
}
}
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
if (str[i][j] == '>')
{
int fi = findn(i), fj = findn(n + j);
G[fi].push_back(fj);
deg[fj]++;
}
else if (str[i][j] == '<')
{
int fi = findn(i), fj = findn(n + j);
G[fj].push_back(fi);
deg[fi]++;
}
}
}
premanagement();
bool ok = toposort();
if (!ok)
{
cout << "NO" << endl;
}
else
{
output();
}
return ;
}
Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)的更多相关文章
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线
hdu 1811 Rank of Tetris Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- 拓扑排序 - 并查集 - Rank of Tetris
Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...
- HDU 1811 Rank of Tetris(拓扑排序+并查集)
题目链接: 传送门 Rank of Tetris Time Limit: 1000MS Memory Limit: 32768 K Description 自从Lele开发了Rating系统, ...
- LA 4255 (拓扑排序 并查集) Guess
设这个序列的前缀和为Si(0 <= i <= n),S0 = 0 每一个符号对应两个前缀和的大小关系,然后根据这个关系拓扑排序一下. 还要注意一下前缀和相等的情况,所以用一个并查集来查询. ...
- Rank of Tetris(hdu1811拓扑排序+并查集)
题意:关于Rating的信息.这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rati ...
- LA4255/UVa1423 Guess 拓扑排序 并查集
评分稍微有一点过分..不过这个题目确确实实很厉害,对思维训练也非常有帮助. 按照套路,我们把矩阵中的子段和化为前缀和相减的形式.题目就变成了给定一些前缀和之间的大小关系,让你构造一组可行的数据.这个东 ...
- hdu 1811 Rank of Tetris - 拓扑排序 - 并查集
自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...
随机推荐
- gulp+tp5配置
优化了文件过滤,更改文件只会重新生成修改的文件 项目目录构建: 在入口文件public下,创建html目录,作为前台静态资源目录 gulp.js文件 /*! * gulp * $ npm instal ...
- FFmpeg开发实战(六):使用 FFmpeg 将YUV数据编码为视频文件
本文中实现的一个小功能是把一个YUV原始视频数据(时间序列图像)经过h264编码为视频码流,然后在使用mp4封装格式封装. 编码&封装的流程图如下: 使用ffmpeg编码流程: 1.首先使用a ...
- 死磕 java集合之ConcurrentHashMap源码分析(三)
本章接着上两章,链接直达: 死磕 java集合之ConcurrentHashMap源码分析(一) 死磕 java集合之ConcurrentHashMap源码分析(二) 删除元素 删除元素跟添加元素一样 ...
- NavUtils【底部虚拟导航栏工具类】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 获取底部虚拟导航栏的高度值 效果图 代码分析 checkDeviceHasNavigationBar(Context context ...
- linux下安装libcurl及开源库的一般安装步骤
前言 总有人说:要多看源代码!那么源代码去哪找呢?找到了又该怎么安装呢?本票博客不介绍如何使用和学习,只要讲获取和安装,以后会将curl和libevent的使用和学习. 一.开源库常用安装步骤 1.开 ...
- stylus 详解与引入
Stylus介绍及特点Stylus 是一个基于Node.js的CSS的预处理框架,诞生于2010年,比较年轻,可以说是一种新型语言,其本质上做的事情与 Sass/LESS 等类似, 可以以近似脚本的方 ...
- Python generator和yield介绍
Python生成器(generator)并不是一个晦涩难懂的概念.相比于MetaClass和Closure等概念,其较为容易理解和掌握.但相对于程序结构:顺序.循环和分支而言其又不是特别的直观.无论学 ...
- Spring基础系列-Web开发
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996902.html SpringBoot基础系列-web开发 概述 web开发就是集成 ...
- WebSocketSharp 的使用
Server 端示例代码: class Program { static void Main(string[] args) { var wssv = new WebSocketServer(" ...
- 在表单提交之前做校验-利用jQuery的submit方法
点击表单中的提交按钮,默认就会提交表单,如果要在表单提交之前做一些校验,那么就可以用jQuery的submit方法. 也就是jQuery的submit的方法执行顺序,在表单的提交之前.用法如下: $( ...