【CF #541 D】 Gourmet choice
link:https://codeforces.com/contest/1131
题意:
给定一些大小比较,输出排名。
思路:
这道题我用的是拓扑排序,又因为有等于号的存在,我用了并查集。
结束后这道题惨遭fst,因为我拓扑排序本应只放入一个集合的代表点,但是我放入了多次。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e8;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = 1e3+;
char mp[maxn][maxn];
int fa[maxn*];
int find(int x){
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
void uni(int x,int y){
int fx = find(x);
int fy = find(y);
if(fx == fy) return ;
fa[fx] = fy;
}
int ans[maxn*],tmp[maxn*];
int du[maxn*],vis[maxn*];
vector<int>G[maxn*];
int main(){
int n,m;
scanf("%d%d", &n, &m);
rep(i, , n) scanf("%s", mp[i] + );
rep(i, , n+m) fa[i] = i; rep(i, , n) {
rep(j, , m) {
if(mp[i][j] == '=') uni(i, j+n);
}
} rep(i, , n) {
rep(j, , m) {
if(mp[i][j] == '=') continue;
int u = find(i), v = find(j+n);
if(mp[i][j] == '>'){
du[u]++;
G[v].pb(u);
}
else {
du[v]++;
G[u].pb(v);
}
}
}
queue<pii>que;
for(int i=; i<=n+m; i++){
if(du[find(i)] == && vis[find(i)] == )
{
que.push(pii(find(i),)),vis[find(i)] = ,tmp[find(i)] = ;
}
} while(!que.empty()){
int u = que.front().fi;int s = que.front().se; que.pop(); for(int i=; i<G[u].size(); i++){
int v = G[u][i];
du[v] --;
if(du[v] == && vis[v] == ) {
tmp[v] = s+;
que.push(pii(v, s+));
vis[v] = ;
}
}
} int flag = ;
for(int i=; i<=n+m; i++) if(vis[find(i)] == ) flag = ;
if(flag == ) puts("No");
else {
puts("Yes");
for(int i=; i<=n; i++) printf("%d ", tmp[find(i)]);
puts("");
for(int i=; i<=m; i++) printf("%d ", tmp[find(i+n)]);
puts("");
}
return ;
}
【CF #541 D】 Gourmet choice的更多相关文章
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- 【39.68%】【CF 714 C】Filya and Homework
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【Cf #503 B】The hat(二分)
为什么Cf上所有的交互题都是$binary \; Search$... 把序列分成前后两个相等的部分,每一个都可以看成一条斜率为正负$1$的折线.我们把他们放在一起,显然,当折线的交点的横坐标为整数时 ...
- 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...
- UOJ30——【CF Round #278】Tourists
1.感谢taorunz老师 2.题目大意:就是给个带权无向图,然后有两种操作, 1是修改某个点的权值 2是询问,询问一个值,就是u到v之间经过点权的最小值(不可以经过重复的点) 操作数,点数,边数都不 ...
- 【CF刷题】14-05-12
Round 236 div.1 A:只需要每个点连接所有比他大的点,知道边用完为止. //By BLADEVIL #include <cmath> #include <cstdio& ...
- UOJ #30. 【CF Round #278】Tourists
Description Cyberland 有 n 座城市,编号从 1 到 n,有 m 条双向道路连接这些城市.第 j 条路连接城市 aj 和 bj.每天,都有成千上万的游客来到 Cyberland ...
- UOJ #30【CF Round #278】Tourists
求从$ x$走到$ y$的路径上可能经过的最小点权,带修改 UOJ #30 $ Solution:$ 如果两个点经过了某个连通分量,一定可以走到这个连通分量的最小值 直接构建圆方树,圆点存原点的点权 ...
- 【Cf #449 C】Willem, Chtholly and Seniorious(set维护线段)
这里介绍以个小$trick$,民间流传为$Old Driver Tree$,实质上就是$set$维护线段. 我们将所有连续一段权值相同的序列合并成一条线段,扔到$set$里去,于是$set$里的所有线 ...
随机推荐
- Zabbix 中使用 Percona Monitoring Plugins 监控 MySQL
1.先安装agent客户端 tar zxvf zabbix-3.2.6.tar.gz cd zabbix-3.2.6 ./configure --prefix=/data/zabbix --enabl ...
- 面试必问之ArrayList
ArrayList概述 (1)ArrayList 是一种变长的集合类,基于定长数组实现. (2)ArrayList 允许空值和重复元素,当往 ArrayList 中添加的元素数量大于其底层数组容量时, ...
- 安装MySQL5.7 安装环境:CentOS7 64位 MINI版,
安装环境:CentOS7 64位 MINI版,安装MySQL5.7 1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo ...
- 安装yarn实况
[**前情提要**]最近在gayhub上面得到一个开源项目,遂准备研究一下源码,当然第一步就是要把项目运行起来.然后看了一下技术栈,发现包管理工具是使用yarn,以前也听说过yarn但是也没有具体使用 ...
- 还在为垂直居中苦恼?CSS 布局利器 flexbox 轻轻松松帮你搞定
传统的 CSS 布局方式是基于盒模型(它是根据盒子与父盒子以及兄弟盒子的关系确定大小和位置的算法),实现时依赖于 block, inline, table, position, float 这些属性, ...
- 弹性盒子---CSS3布局方式
1.弹性盒子/伸缩盒子 如果要使用弹性盒子属性,首先要将父级元素变成弹性盒子 Flex-direction 设置伸缩盒子的内部元素的排列方式 Row 从左到右安行排列 Column 从上到下按 ...
- Spark 系列(十五)—— Spark Streaming 整合 Flume
一.简介 Apache Flume 是一个分布式,高可用的数据收集系统,可以从不同的数据源收集数据,经过聚合后发送到分布式计算框架或者存储系统中.Spark Straming 提供了以下两种方式用于 ...
- 『深度应用』NLP机器翻译深度学习实战课程·零(基础概念)
0.前言 深度学习用的有一年多了,最近开始NLP自然处理方面的研发.刚好趁着这个机会写一系列NLP机器翻译深度学习实战课程. 本系列课程将从原理讲解与数据处理深入到如何动手实践与应用部署,将包括以下内 ...
- 使用文本框TextView/EditText的开源库清单
在实际的开发中TextView和EditText是非常基本的控件.这两个控件的使用也是十分简单.而TextView/EditText的功能其实也是非常强大,例如简单的图文就可以使用TextView配合 ...
- TDH 安装 TDH-Client
1. TDH-Client 下载 (下载分享:链接:https://pan.baidu.com/s/1ZmP4BUCiuRypCtsoAuvKRA 提取码:xsbl ) tar -vxf td ...