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高手排行榜, ...
随机推荐
- 【Teradata】TD Unicode编码格式下varchar定义测试
如下测试表,每个字段字符编码格式均为Unicode. 1.varchar(1)可以存储1个汉字,也只能存储1个ASCII字符. --创建表nc_test,每个字段编码格式均设定为Unicode[使用S ...
- 【死磕Java并发】-----Java内存模型之happens-before
在上篇博客([死磕Java并发]-–深入分析volatile的实现原理)LZ提到过由于存在线程本地内存和主内存的原因,再加上重排序,会导致多线程环境下存在可见性的问题.那么我们正确使用同步.锁的情况下 ...
- Scala的类层级讲解
Scala的类层级 Scala里,每个类都继承自通用的名为Any的超类. 因为所有的类都是Any的子类,所以定义在Any中的方法就是"共同的"方法:它们可以被任何对象调用. Sca ...
- 使用 shell / python 进行sql的excel报表导出
如果要求你进行一个表数据的导出,如果使用shell的话,很容易做到,即执行一下 select 语句就可以拿到返回结果了! 如下: /usr/bin/mysql -u"${username}& ...
- 网卡也能虚拟化?网卡虚拟化技术 macvlan 详解
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 01 macv ...
- Identity Server 4 - Hybrid Flow - Claims
前一篇 Identity Server 4 - Hybrid Flow - MVC客户端身份验证: https://www.cnblogs.com/cgzl/p/9253667.html Claims ...
- Python基础(数字,字符串方法)
数字: #二进制转十进制 a=' v=int(a,base=2) print(v) 进制转换 #当前数字的二进制至少有多少位 b=2 v2=b.bit_length() print(v2) 数值二进制 ...
- SpringBoot整合Netty并使用Protobuf进行数据传输(附工程)
前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会简单的介绍下用法,至于Netty在之前的文章中已经简单的介绍过了,这里就不 ...
- Java基础系列-Enum深入解析
原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755129.html 一.概述 枚举就是一个语法糖效果. 定义一个枚举,其实就是定义一个继承抽象类E ...
- 玩转Spring Cloud之熔断降级(Hystrix)与监控
本文内容导航目录: 前言:解释熔断降级一.搭建服务消费者项目,并集成 Hystrix环境 1.1.在POM XML中添加Hystrix依赖(spring-cloud-starter-netflix-h ...