codeforces 1131D-Gourmet choice
传送门:QAQQAQ
题意:有两个数组,一个数组有n个数,另一个数组有m个数。s[i][j]表示第一个数组第i个数和第二个数组第j个数的大小关系,要求构造出一种方案,使条件成立。
先考虑没有等于号的情况,那么就是裸的拓扑排序,现在多了一个等于号,就先进行并查集再拓扑就行了。
•注意点:1.在进行拓扑排序时一定要全部用father[x]作为节点
2.一定要全部并查集做完以后再连边,否则可能刚连完father[x]就变了
#include<bits/stdc++.h>
using namespace std;
const int N=;
typedef pair<int,int> pii; int p[N],a[N];
int n,m,judge=;
int t[N],ans[N],b[N];
char s[][];
vector <int> v[]; int f(int x)
{
if(p[x]==x) return x;
return p[x]=f(p[x]);
} void unite(int x,int y)
{
int xx=f(x),yy=f(y);
p[yy]=p[xx];
} int main()
{
memset(b,,sizeof(b));
memset(t,,sizeof(t));
scanf("%d %d",&n,&m);
for(int i=;i<=n+m;i++) p[i]=i;
for(int i=;i<=n;i++)
{
scanf("%s",s[i]+);
for(int j=;j<=m;j++)
{
if(s[i][j]=='=')
{
unite(i,j+n);
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(s[i][j]=='>') v[f(j+n)].push_back(f(i)),t[f(i)]++;
else if(s[i][j]=='<') v[f(i)].push_back(f(j+n)),t[f(j+n)]++;
}
}
priority_queue<pii> q;
for(int i=;i<=n+m;i++) if(f(i)==i) judge++;
for(int i=;i<=n+m;i++)
{
if(!t[p[i]]&&!b[p[i]])
{
q.push(make_pair(p[i],));
b[p[i]]=;
}
}
int sum=,num=;
while(!q.empty())
{
int now=q.top().first,w=q.top().second;
q.pop(); sum++;
ans[now]=w;
for(int i=;i<v[now].size();i++)
{
int pos=v[now][i];
t[pos]--;
if(!t[pos])
{
q.push(make_pair(pos,w+));
}
}
}
if(sum<judge)
{
puts("No");
return ;
}
puts("Yes");
for(int i=;i<=n;i++) printf("%d ",ans[p[i]]); puts("");
for(int i=n+;i<=m+n;i++) printf("%d ",ans[p[i]]); puts("");
return ;
}
codeforces 1131D-Gourmet choice的更多相关文章
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- coderfoces D. Gourmet choice
D. Gourmet choice time limit per test 2 seconds memory limit per test 256 megabytes 题目链接: https: ...
- D. Gourmet choice并查集,拓扑结构
D. Gourmet choice time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- codeforces #541 D. Gourmet choice(拓扑+并查集)
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the wo ...
- Codeforces 1154C Gourmet Cat
题目链接:http://codeforces.com/problemset/problem/1154/C 题目大意: 主人有一只猫.周一&周四&周日:吃鱼周二&周六:吃兔子周三 ...
- Codeforces 589F Gourmet and Banquet
A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...
- 【CF #541 D】 Gourmet choice
link:https://codeforces.com/contest/1131 题意: 给定一些大小比较,输出排名. 思路: 这道题我用的是拓扑排序,又因为有等于号的存在,我用了并查集. 结束后这道 ...
- codeforces 589F. Gourmet and Banquet 二分+网络流
题目链接 给你n种菜, 每一种可以开始吃的时间不一样, 结束的时间也不一样. 求每种菜吃的时间都相同的最大的时间.时间的范围是0-10000. 看到这个题明显可以想到网络流, 但是时间的范围明显不允许 ...
- CF1131D Gourmet choice
题目链接 题意 有两组菜,第一组有\(n\)种,第二组有\(m\)种.给出一个\(n\times m\)的矩阵,第\(i\)行第\(j\)列表示第一组中的第\(i\)种菜与第二组中的第\(j\)种菜好 ...
- CF1131D Gourmet choice(并查集,拓扑排序)
这题CF给的难度是2000,但我感觉没这么高啊…… 题目链接:CF原网 题目大意:有两个正整数序列 $a,b$,长度分别为 $n,m$.给出所有 $a_i$ 和 $b_j(1\le i\le n,1\ ...
随机推荐
- jboss未授权Getshell
一.jboss未授权访问Getshell 1.jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss.system:type=Server ...
- js数组方法 slice()和splice()
说实在我之前都不怎么分的清这个两个函数,因为这两个函数名字那么像,经常我就弄混了,平常使用的时候都先查一下我需要使用的实际是哪个函数.这样不说很浪费时间,但是也是影响了开发效率,所以我决定今天就彻底区 ...
- error while loading shared libraries: lib*.so: cannot open shared object file: No such file or directory
动态库的搜索路径搜索的先后顺序是: 1.编译目标代码时指定的动态库搜索路径; 2.环境变量LD_LIBRARY_PATH指定的动态库搜索路径: 比如export LD_LIBRARY_PATH=/us ...
- git连接gitee笔记
#首先参照 https://blog.csdn.net/zhangyu4863/article/details/80427289 #然后需要注意,在办公室无法使用 git remote add ori ...
- 廖雪峰Java16函数式编程-2Stream-5filter
1.filter简介 Stream.filter()是一个转换方法,把一个Stream转换为另一个Stream. 所谓filter操作,就是对一个Stream的所有元素进行测试,不满足条件的元素就被过 ...
- hdu多校第三场 1007 (hdu6609) Find the answer 线段树
题意: 给定一组数,共n个,第i次把第i个数扔进来,要求你删掉前i-1个数中的一些(不许删掉刚加进来这个数),使得前i个数相加的和小于m.问你对于每个i,最少需要删掉几个数字. 题解: 肯定是优先删大 ...
- VS2010-MFC(菜单:VS2010菜单资源详解)
转自:http://www.jizhuomi.com/software/210.html 上一节讲了标签控件Tab Control以后,常用控件的内容就全部讲完了,当然并没有包括所有控件,主要是一些很 ...
- centos7 将home目录空间扩容到根目录
[root@localhost ~]# umount /home/ [root@localhost ~]# lvremove /dev/mapper/centos-home Do you really ...
- docker 可持续集成及日志管理及监控报警
- 第四周——重新clone项目后maven问题
重新clone项目后,一直报错,"类重复..." clean后install也无效果. 原因是idea在重启项目时会更改maven为默认的idea自带的maven配置,要重新设置