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高手排行榜, ...
随机推荐
- Could not load file or assembly……
今天在运行一个ASP.NET Core项目的时候发现这样的错误: 我一开始觉得这是个很简单的问题,很明显,出错的原因是项目中某些地方还保留了对Njt.MvcAuthLib这个库的引用,而现在我不需要了 ...
- Amqp整合com.rabbitmq.client.ShutdownSignalException: channel error; protocol method异常处理
java.io.IOException at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:126) at com.rabbitmq ...
- C#类型(一)
1.System.Object C#的所有类型都是派生自System.Object 也就是说下面的两个类型定义完全一致 // 隐式派生自Object public class Person{ } { ...
- Mac电脑上一款非常时尚高清的动态壁纸Living Wallpaper HD
很多朋友Mac电脑上都喜欢用动态壁纸,Living Wallpaper HD是本人尝试的一款非常不错的高清动态壁纸.有时钟.天气等各种组建,非常时尚美观. Living Wallpaper HD下载地 ...
- 用ASP.NET Core 2.1 建立规范的 REST API -- 保护API和其它
本文介绍如何保护API,无需看前边文章也能明白吧. 预备知识: http://www.cnblogs.com/cgzl/p/9010978.html http://www.cnblogs.com/cg ...
- 【最短路径Floyd算法详解推导过程】看完这篇,你还能不懂Floyd算法?还不会?
简介 Floyd-Warshall算法(Floyd-Warshall algorithm),是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似.该算法名称以 ...
- 深入学习ThreadLocal原理
上文我们学习了ThreadLocal的基本用法以及基本原理,ThreadLocal中的方法并不多,基本用到的也就get.set.remove等方法,但是其核心逻辑还是在定义在ThreadLocal内部 ...
- Docker进阶之二:Docker内部组件
Docker内部组件 一.Namespaces 命名空间,Linux内核提供的一种对进程资源隔离的机制,例如进程,网络,挂载点等资源. docker run -d busybox ping ba ...
- Spring Boot(六):如何优雅的使用 Mybatis
*:first-child{margin-top: 0 !important}.markdown-body>*:last-child{margin-bottom: 0 !important}.m ...
- Django-restframework 之权限源码分析
Django-restframework 之权限源码分析 一 前言 上篇博客分析了 restframework 框架的认证组件的执行了流程并自定义了认证类.这篇博客分析 restframework 的 ...