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的更多相关文章

  1. 2-sat 输出任意一组可行解&拓扑排序+缩点 poj3683

    Priest John's Busiest Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8170   Accept ...

  2. CF Fox And Names (拓扑排序)

    Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. CF 213A Game(拓扑排序)

    传送门 Description Furik and Rubik love playing computer games. Furik has recently found a new game tha ...

  4. 【BZOJ-1924】所驼门王的宝藏 Tarjan缩点(+拓扑排序) + 拓扑图DP

    1924: [Sdoi2010]所驼门王的宝藏 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 787  Solved: 318[Submit][Stat ...

  5. 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 ...

  6. POJ2762 Going from u to v or from v to u?(判定单连通图:强连通分量+缩点+拓扑排序)

    这道题要判断一张有向图是否是单连通图,即图中是否任意两点u和v都存在u到v或v到u的路径. 方法是,找出图中所有强连通分量,强连通分量上的点肯定也是满足单连通性的,然后对强连通分量进行缩点,缩点后就变 ...

  7. 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. ...

  8. 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:  ...

  9. POJ 2762推断单个联通(支撑点甚至通缩+拓扑排序)

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14789 ...

随机推荐

  1. 两个提高工作效率的神器-Restlet Client和fe助手

    首先是要FQ,百度***或者直接下载蓝灯. 然后安装第一个WEB前端助手:

  2. 流畅的python 读书笔记 第二章 序列构成的数组 列表推导

    列表推导是构建列表(list)的快捷方式,而生成器表达式则可以用来创建其他任何类型的序列.如果你的代码里并不经常使用它们,那么很可能你错过了许多写出可读性更好且更高效的代码的机会. 2.2.1 列表推 ...

  3. 模仿Masonary写一个计算器

    1.CaculatorMaker @interface CaculatorMaker : NSObject @property(nonatomic,assign)int result; -(Cacul ...

  4. Exception in thread "main" java.lang.NoClassDefFoundError: scala/Product$class

    在使用spark sql时一直运行报这个错误,最后仔细排查竟然是引入了两个scala library .去除其中一个scala的编译器即可 Exception in thread "main ...

  5. 跟我学Makefile(三)

    紧接着跟我学Makefile(二)继续学习:变量高级用法 (1)变量值的替换 :替换变量中的共有的部分,其格式是“$(var:a=b)”或是“${var:a=b}”,把变量“var”中所有以“a”字串 ...

  6. t分布, 卡方x分布,F分布

    T分布:温良宽厚 本文由“医学统计分析精粹”小编“Hiu”原创完成,文章采用知识共享Attribution-NonCommercial-NoDerivatives 4.0国际许可协议(http://c ...

  7. 运行.xcworkspace项目后报错:'React/RCTBundleURLProvider.h’ file not found

    情况:根据https://github.com/rebeccahughes/react-native-device-info添加依赖库,运行.xcworkspacea项目后报错 解决:Delete n ...

  8. cocos代码研究(10)ActionEase子类学习笔记

    理论部分 缓动动作的基类,继承自 ActionInterval类.ActionEase本身是一个抽象的概念父类,开发者最好不要在代码中直接创建它的对象,因为它没有具体的执行效果,这一类的子类速度变化大 ...

  9. JavaScript性能优化小窍门汇总(含实例)

    在众多语言中,JavaScript已经占有重要的一席之地,利用JavaScript我们可以做很多事情 , 应用广泛.在web应用项目中,需要大量JavaScript的代码,将来也会越来越多.但是由于J ...

  10. java8中接口中的default方法

    在java8以后,接口中可以添加使用default或者static修饰的方法,在这里我们只讨论default方法,default修饰方法只能在接口中使用,在接口种被default标记的方法为普通方法, ...