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高手排行榜, ...
随机推荐
- 关于OSError: [WinError 10038] 在一个非套接字上尝试了一个操作。
在使用socket的时候,写了一个while循环,就报错了.结果如下: OSError: [WinError 10038] 在一个非套接字上尝试了一个操作. 代码 import socket impo ...
- vue 中 vue-router、transition、keep-alive 怎么结合使用?
<transition :name="name" mode="out-in" name="fade"> <keep-ali ...
- this指针的初运用
this一般运用场景: 1.位于函数中,谁调用指向谁 var make = "Mclaren"; var model = "720s" function ful ...
- AI应用开发实战 - 从零开始配置环境
AI应用开发实战 - 从零开始配置环境 与本篇配套的视频教程请访问:https://www.bilibili.com/video/av24421492/ 建议和反馈,请发送到 https://gith ...
- shell编写小技巧整理
1. if和else语句可以进行嵌套.if的条件判断部分可能会变得很长,可以使用逻辑运算符将它变得简洁一些. [ condition ] && action :如果condition为 ...
- 每日分享!~ JavaScript数组去重
数组去重 数组去重在很多面试的过程中,都是大题出现!网络上出现了很多数组去重的方式.多数的达到了12种以上. 今天我只给大家介绍两种我比较喜欢,比较认可!入手简单的-能解决自己的问题就可以了 好了 , ...
- JAVA 中的接口(interface)
我们一般将一个抽象类中所有方法都是抽象方法的抽线类定义为接口,接口时由常量和抽象方法组成的特殊类,即接口里面连构造方法也没有.接口通常用“interface”关键字来声明,一个类通过继承接口的方式,从 ...
- HTTP 缓存相关
网络中数据传输是很耗时的,数据要在漫长的路径中奔波,客户端在数据完整到达前只能等待.如果能够复用已经请求过的资源,势必会让整个页面加载高效许多.这可以通过合理地设置服务器的缓存,与浏览器的缓存机制配合 ...
- 前端CSS学习-Background背景相关
在CSS中 背景属性用于定义HTML元素的背景. background主要设置一下五个属性: background-color // 设置元素的背景颜色. background-image // 把 ...
- SQL优化 MySQL版 - 索引分类、创建方式、删除索引、查看索引、SQL性能问题
SQL优化 MySQL版 - 索引分类.创建方式.删除索引.查看索引.SQL性能问题 作者 Stanley 罗昊 [转载请注明出处和署名,谢谢!] 索引分类 单值索引 单的意思就是单列的值,比如说有 ...