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. Python开发【笔记】:sort排序大法

    浅谈排序 程序中经常用到排序函数,Python 提供了 sort 和 sorted 函数,一个原地排序,一个返回排序后的新结果 1.参数 函数原型: sort([cmp[, key[, reverse ...

  2. js-jquery-从SweetAlert到SweetAlert2

    原文地址:https://github.com/limonte/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2 1. IE supp ...

  3. [py]django的manytomany字段和后台搜索过滤功能

    我本来想搞下Django之select_related和prefetch_related的区别,看到这里有djangoapi的知识, 之前搞过django restfulapi,http://blog ...

  4. 1107 Social Clusters[并查集][难]

    1107 Social Clusters(30 分) When register on a social network, you are always asked to specify your h ...

  5. JavaScript Ajax上传文件miniupload.js

    用到jquery和layer.js (function ($) { $.fn.miniupload = function (options, callback) { var jqDom = $(thi ...

  6. Java 对比Vector、ArrayList、LinkedList

    ①引言 在日常生活中能高效的管理和操作数据是非常重要的.Java提供了强大的集合框架,大大提高了开发者的生产力,今天就了解一下有关集合框架方面的问题. Vector.ArrayList.LinkedL ...

  7. 常用php操作redis命令整理(二)哈希类型

    HSET将哈希表key中的域field的值设为value;如果field是哈希表中的一个新建域,并且值设置成功,返回1;如果哈希表中域field已经存在且旧值已被新值覆盖,返回0. <?php ...

  8. P1291 [SHOI2002]百事世界杯之旅(概率)

    P1291 [SHOI2002]百事世界杯之旅 设$f(n,k)$表示共n个名字,剩下k个名字未收集到,还需购买饮料的平均次数 则有: $f(n,k)=\frac{n-k}{n}*f(n,k) + \ ...

  9. SSH Secure File Transfer Client连接远程设备报“algorithm negotiation failed”错的解决方法

    SSH Secure File Transfer Client连接远程设备报"algorithm negotiation failed"错的解决方法 ssh client 报 al ...

  10. 'workspace' in VS Code

    What is a 'workspace' in VS Code? You can save settings at the workspace level and you can open mult ...