Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)
#include<bits/stdc++.h>
using namespace std;
vector<int>g[2007];
int fa[2007],vis[2007],num[2007];
char s[2007][2007];
int find_(int x){
if(fa[x]==x)
return x;
return fa[x]=find_(fa[x]);//合并
}
void dfs(int u){
vis[u]=-1;//正数说明有环,0说明需要dfs,so将其置为负数
++num[u];//从链尾dfs每次给链尾+1
for(auto&i:g[u]){
num[i]=max(num[i],num[u]);//链尾以后的数字至少大于链尾(可能在别的链中数字较大),先让它们都和链尾相同以后再通过dfs++
if(!--vis[i])
dfs(i);
}
}
void addedge(int u,int v){
g[u].push_back(v);//建图
vis[v]++;//打上标记
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n+m;i++)
fa[i]=i;//初始化
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
for(int j=1;j<=m;j++)
if(s[i][j]=='=')
fa[find_(i)]=find_(n+j);//缩点
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(s[i][j]=='<')
addedge(find_(i),find_(n+j));//大的放后面
else if(s[i][j]=='>')
addedge(find_(n+j),find_(i));//大的放后面
}
int flag=0;
for(int i=1;i<=n+m;i++)
if(find_(i)==i&&!vis[i])//找一个代表与它同值的所有点的点并且没有被拜访过即链尾
dfs(i);//dfs
for(int i=1;i<=n+m;i++)
if(find_(i)==i&&vis[i]>0){//正值说明有环
flag=1;
printf("No");//显然有环是不能安排的
break;
}
if(!flag){
printf("Yes\n");
for(int i=1;i<=n;i++)
printf("%d ",num[find_(i)]);//找到代表它的点的num值
printf("\n");
for(int i=1;i<=m;i++)
printf("%d ",num[find_(i+n)]);//找到代表它的点的num值
}
return 0;
}
Codeforces Round #541 (Div. 2)D(并查集(dsu),拓扑排序)的更多相关文章
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
- Codeforces Round #286 (Div. 2) B 并查集
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)
传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...
- Codeforces Round #541 (Div. 2)
Codeforces Round #541 (Div. 2) http://codeforces.com/contest/1131 A #include<bits/stdc++.h> us ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Codeforces 1131 F. Asya And Kittens-双向链表(模拟或者STL list)+并查集(或者STL list的splice()函数)-对不起,我太菜了。。。 (Codeforces Round #541 (Div. 2))
F. Asya And Kittens time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #541 (Div. 2)题解
不知道该更些什么 随便写点东西吧 https://codeforces.com/contest/1131 ABC 太热了不写了 D 把相等的用并查集缩在一起 如果$ x<y$则从$ x$往$y$ ...
- Codeforces Round #541 (Div. 2) (A~F)
目录 Codeforces 1131 A.Sea Battle B.Draw! C.Birthday D.Gourmet choice(拓扑排序) E.String Multiplication(思路 ...
随机推荐
- [转载]python的range()函数用法
使用python的人都知道range()函数很方便,今天再用到他的时候发现了很多以前看到过但是忘记的细节.这里记录一下range(),复习下list的slide,最后分析一个好玩儿的冒泡程序. 这里记 ...
- [cinder] volume type 使用简记
cinder type-create sharecinder type-key share set volume_backend_name=GLUSTERFScinder type-create lo ...
- Struts2中web.xml里面struts-cleanup作用
struts2.1.3之后的版本均不需要配置该过滤器 参考: struts-cleanup作用 升级到struts-2.3.14.3之后涉及的改动以及ActionContextCleanUp,Stru ...
- python ConfigParser 读取配置文件
- python 继承进阶
继承进阶 面向对象 1.类:具有相同属性和方法 的一类事物 类名可以实例化一个对象 类名可以调用类属性,(静态属性 和(方法)动态属性) 2.对象:也就是实例 对象名:调用对象属性 调用方法 3 ...
- java线程面试题及答案
1)2017Java面试题及答案:什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序员可以通过它进行多处理器编程,你可以使用多线程对运算密集型任务 ...
- 用UltraISO把硬盘文件制作成ISO格式
转自:https://wenku.baidu.com/view/0052c88dcc22bcd126ff0cbf.html 用UltraISO把硬盘文件制作成ISO格式方法: 制作硬盘ISO文件步骤一 ...
- Android添加Menu菜单
在安卓中添加Menu菜单十分简单. 步骤: 1.在menu文件夹中的main.xml文件中添加要添加的项目. <menu xmlns:android="http://schemas.a ...
- Python操作远程机器
操作远程机器主要使用的有paramiko,WMI(Windows Management Instrumentation),SMBConnection. paramiko paramiko使用SSH2协 ...
- C++中cin.get(),cin.getline(),cin>>,gets(),cin.clear()使用总结
1.cin.get() 实质:类istream所定义对象cin的重载成员函数 用于读取单字符 istream& get(char&) int get(void) 用于读取字符 ...