CF 274D Lovely Matrix 拓扑排序,缩点 难度:2
http://codeforces.com/problemset/problem/274/D
这道题解题思路:
对每一行统计,以小值列作为弧尾,大值列作为弧头,(-1除外,不连弧),对得到的图做拓扑排序即可.
但本题数据较大,所以需要进行缩点,把相同数值的点缩在一起,成为一个新的大点,原先的小值列向大点连接,再由大点向大值列连接,可以减少边数
举例来说,原本取值为1的有4个点,取值为2的有5个点,
不缩点,就需要20条边
缩点,只需要4+1+5=10条边
(不过我还是觉得这个方法有点投机取巧??)
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn=2e5+3;
typedef pair<int,int> P;
P a[maxn];
int deg[maxn];
bool used[maxn];
int ans[maxn];
vector <int >e[maxn];
queue<int> que;
int n,m,last,flast; int main(){
scanf("%d%d",&n,&m);
flast=m+1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&a[j].first);
a[j].second=j+1;
}
sort(a,a+m); last=flast;
for(int j=0;j<m;){
if(a[j].first==-1){j++;continue;}
int k=j;
while(a[k].first==a[j].first){
e[a[k].second].push_back(last);
deg[last]++;
if(last>flast){
e[last-1].push_back(a[k].second);
deg[a[k].second]++;
}
k++;
}
last++;
j=k;
}
flast=last;
}
for(int i=1;i<=m;i++){
if(deg[i]==0){
que.push(i);
}
}
int len=0;
while(!que.empty()&&len<m){
int s=que.front();que.pop();
if(used[s])continue;
used[s]=true;
if(s<=m)ans[len++]=s;
for(int i=0;i<e[s].size();i++){
int t=e[s][i];
if(!used[t]){
deg[t]--;
if(deg[t]==0){
que.push(t);
}
}
}
}
if(len<m){
puts("-1");
}
else for(int i=0;i<len;i++){
printf("%d%c",ans[i],i==len-1?'\n':' ');
}
return 0;
}
CF 274D Lovely Matrix 拓扑排序,缩点 难度:2的更多相关文章
- 2-sat 输出任意一组可行解&拓扑排序+缩点 poj3683
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8170 Accept ...
- CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF 213A Game(拓扑排序)
传送门 Description Furik and Rubik love playing computer games. Furik has recently found a new game tha ...
- 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP
1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 787 Solved: 318[Submit][Stat ...
- Going from u to v or from v to u?_POJ2762强连通+并查集缩点+拓扑排序
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Description I ...
- POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)
这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...
- POJ 2762 Going from u to v or from v to u? (强连通分量缩点+拓扑排序)
题目链接:http://poj.org/problem?id=2762 题意是 有t组样例,n个点m条有向边,取任意两个点u和v,问u能不能到v 或者v能不能到u,要是可以就输出Yes,否则输出No. ...
- poj 2762 Going from u to v or from v to u?(强连通分量+缩点重构图+拓扑排序)
http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: ...
- POJ 2762推断单个联通(支撑点甚至通缩+拓扑排序)
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14789 ...
随机推荐
- [SQL] 让特定的数据 排在最前
MYSQL目前常用的两种方法,如下: 让值为"张三" 的数据排在最前. -- 方法一 end asc -- 方法二 select * from tableName where co ...
- SpringCloud 进阶之Hystrix(断路器)
1. Hystrix 断路器 Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败, 比如超时,异常等,Hystrix能够保证在一个依赖出问题的情况 ...
- [Luogu4331][Baltic2004]数字序列
原题请见<左偏树的特点及其应用>BY 广东省中山市第一中学 黄源河 luogu 题意 给出序列\(a[1...n]\),要求构造一个不下降序列\(b[1...n]\)使得\(\sum_{i ...
- 通过pd.to_sql()将DataFrame写入Mysql
循环创建表,并且创建主键.外键 import pandas as pd from sqlalchemy import create_engine from sqlalchemy.types impor ...
- Checkpoint的运行原理和源码实现
引言 Checkpoint 到底是什么和需要用 Checkpoint 解决什么问题: Spark 在生产环境下经常会面临 Transformation 的 RDD 非常多(例如一个Job 中包含1万个 ...
- Eclipse中没有javax.servlet和javax.servlet.http包的处理办法
使用Eclips开发JSP也需要这两个包:javax.servlet和javax.servlet.http:若提示没有javax.servlet包则安装如下处理办法解决: 如果你装了Tomacat,那 ...
- PAT 1044 Shopping in Mars[二分][难]
1044 Shopping in Mars(25 分) Shopping in Mars is quite a different experience. The Mars people pay by ...
- 2017-2018-2 20165207实验二《Java面向对象程序设计》实验报告
2017-2018-2 20165207实验二<Java面向对象程序设计>实验报告 课程:Java程序设计 班级:1652 姓名:李天林 学号:20165207 实验日期:2018年4月1 ...
- consul 配置
Eureka 2.0 开源工作宣告停止,对于注册中心来说 Consul 是个更好的选择. 在本场 Chat 中你可以学到的: 了解和搭建 Consul 服务:Spring Cloud Consul 服 ...
- 前端学习笔记之CSS浮动浅析
很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能力差,也可能是没能遇到一篇通俗的教程. 前些天小菜终于搞懂了浮动的基本原理,迫不及待的分享给大家. 写在前面的话: 由于CSS内容比较多 ...