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(拓扑排序+并查集)的更多相关文章

  1. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  2. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  3. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  4. 拓扑排序 - 并查集 - Rank of Tetris

    Description 自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球 ...

  5. HDU 1811 Rank of Tetris(拓扑排序+并查集)

    题目链接: 传送门 Rank of Tetris Time Limit: 1000MS     Memory Limit: 32768 K Description 自从Lele开发了Rating系统, ...

  6. LA 4255 (拓扑排序 并查集) Guess

    设这个序列的前缀和为Si(0 <= i <= n),S0 = 0 每一个符号对应两个前缀和的大小关系,然后根据这个关系拓扑排序一下. 还要注意一下前缀和相等的情况,所以用一个并查集来查询. ...

  7. Rank of Tetris(hdu1811拓扑排序+并查集)

    题意:关于Rating的信息.这些信息可能有三种情况,分别是"A > B","A = B","A < B",分别表示A的Rati ...

  8. LA4255/UVa1423 Guess 拓扑排序 并查集

    评分稍微有一点过分..不过这个题目确确实实很厉害,对思维训练也非常有帮助. 按照套路,我们把矩阵中的子段和化为前缀和相减的形式.题目就变成了给定一些前缀和之间的大小关系,让你构造一组可行的数据.这个东 ...

  9. hdu 1811 Rank of Tetris - 拓扑排序 - 并查集

    自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...

随机推荐

  1. cmd命令重定向到剪切板

    Windows下 使用系统自带的 clip 命令. # 位于 C:\Windows\system32\clip.exe. 示例: # 将字符串 Hello 放入 Windows 剪贴板 echo He ...

  2. Storm入门(十)Twitter Storm: Transactional Topolgoy简介

    作者: xumingming | 可以转载, 但必须以超链接形式标明文章原始出处和作者信息及版权声明网址: http://xumingming.sinaapp.com/736/twitter-stor ...

  3. kali linux中的yum、rpm常见的问题

    事件起因:今天我在kali里面使用yum命令来部署Linux环境时,出现了错误: bash:yum command not found,然后就百度,找到一片好的文章,链接:http://www.pia ...

  4. Spring Boot 2.2 增加了一个新功能,启动飞起~

    前几天栈长分享了一个好玩的框架:一个比Spring Boot快44倍的Java框架!,是不是感觉 Spring Boot 略慢?今天讲一下 Spring Boot 添加的这个新特性,可以大大提升 Sp ...

  5. 10分钟详解Spring全家桶7大知识点

    Spring框架自2002年诞生以来一直备受开发者青睐,它包括SpringMVC.SpringBoot.Spring Cloud.Spring Cloud Dataflow等解决方案.有人亲切的称之为 ...

  6. 读Cassandra源码之并发

    java 并发与线程池 java并发包使用Executor框架来进行线程的管理,Executor将任务的提交与执行过程分开,直接使用Runnable表示任务.future获取返回值.ExecutorS ...

  7. Openresty编写Lua代码一例

    1.前段时间纠结了很久,一直弄不清lua和tomcat的联系.一直认为是lua调用tomcat的接口才可使用,后面才明白过来,进入了一个误区,lua本身就是一门独立的脚本语言.在openresty里面 ...

  8. Floor报错原理分析

    最近开始打ctf了,发现好多sql注入都忘了,最近要好好复习一下. 基础知识: floor(): 去除小数部分 rand(): 产生随机数 rand(x): 每个x对应一个固定的值,但是如果连续执行多 ...

  9. 自定义GridControl编辑器

    本文版权归博主 惊梦无痕 所有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作.SourceLink 鉴于网上的针对GridControl的一些代码比较凌乱,且功能分散,故将整理过的代码分享出来 ...

  10. Activity与DialogFragment交互的方法

    今天我们来讨论一下如何在Activity与DialogFragment交互的方法,这里包括了DialogFragment的启动以及Activity方法的调用. DialogFragment与Dialo ...