http://codeforces.com/problemset/problem/274/D

这道题解题思路:

对每一行统计,以小值列作为弧尾,大值列作为弧头,(-1除外,不连弧),对得到的图做拓扑排序即可.

但本题数据较大,所以需要进行缩点,把相同数值的点缩在一起,成为一个新的大点,原先的小值列向大点连接,再由大点向大值列连接,可以减少边数

举例来说,原本取值为1的有4个点,取值为2的有5个点,

不缩点,就需要20条边

缩点,只需要4+1+5=10条边

(不过我还是觉得这个方法有点投机取巧??)

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6. const int maxn=2e5+3;
  7. typedef pair<int,int> P;
  8. P a[maxn];
  9. int deg[maxn];
  10. bool used[maxn];
  11. int ans[maxn];
  12. vector <int >e[maxn];
  13. queue<int> que;
  14. int n,m,last,flast;
  15.  
  16. int main(){
  17. scanf("%d%d",&n,&m);
  18. flast=m+1;
  19. for(int i=0;i<n;i++){
  20. for(int j=0;j<m;j++){
  21. scanf("%d",&a[j].first);
  22. a[j].second=j+1;
  23. }
  24. sort(a,a+m);
  25.  
  26. last=flast;
  27. for(int j=0;j<m;){
  28. if(a[j].first==-1){j++;continue;}
  29. int k=j;
  30. while(a[k].first==a[j].first){
  31. e[a[k].second].push_back(last);
  32. deg[last]++;
  33. if(last>flast){
  34. e[last-1].push_back(a[k].second);
  35. deg[a[k].second]++;
  36. }
  37. k++;
  38. }
  39. last++;
  40. j=k;
  41. }
  42. flast=last;
  43. }
  44. for(int i=1;i<=m;i++){
  45. if(deg[i]==0){
  46. que.push(i);
  47. }
  48. }
  49. int len=0;
  50. while(!que.empty()&&len<m){
  51. int s=que.front();que.pop();
  52. if(used[s])continue;
  53. used[s]=true;
  54. if(s<=m)ans[len++]=s;
  55. for(int i=0;i<e[s].size();i++){
  56. int t=e[s][i];
  57. if(!used[t]){
  58. deg[t]--;
  59. if(deg[t]==0){
  60. que.push(t);
  61. }
  62. }
  63. }
  64. }
  65. if(len<m){
  66. puts("-1");
  67. }
  68. else for(int i=0;i<len;i++){
  69. printf("%d%c",ans[i],i==len-1?'\n':' ');
  70. }
  71. return 0;
  72. }

  

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. Day01 html详解

      day01 html详解   1.html的简介     1.1 什么是html?         - HyperText Markup Language:超文本标记语言,网页语言         ...

  2. DZNEmptyDataSet 使用

    gitHub地址:https://github.com/dzenbot/DZNEmptyDataSet 效果图: 代码: #import "UIScrollView+EmptyDataSet ...

  3. android Dialog官方demo

    1.普通的Dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("今天 ...

  4. elasticsearch-hadoop使用

    elasticsearch-hadoop是一个深度集成Hadoop和ElasticSearch的项目,也是ES官方来维护的一个子项目,通过实现Hadoop和ES之间的输入输出,可以在Hadoop里面对 ...

  5. CentOS 7 SSH 免密登录的方法

    先决条件 3 台 CentOS 7 HOSTNAME IP ROLE server1 10.8.26.197 Master server2 10.8.26.196 Slave1 server3 10. ...

  6. 数据挖掘-关联分析 Apriori算法和FP-growth 算法

    •1.关联分析概念 关联分析是从大量数据中发现项集之间有趣的关联和相关联系. ​ •定义:1.事务:每一条交易称为一个事务,如上图包含5个事务.2.项:交易的每一个物品称为一个项,例如豆奶,啤酒等. ...

  7. php根据路径获取文件名

    <?php // 根据路径返回文件名 $path = 'J:\abc\defg\hijk\一个文件夹\lmn\opq'; $path = iconv("UTF-8", &qu ...

  8. 1 :2 Strust2—Demo

    =============================================================== Demo基础包:

  9. VS2010/MFC编程入门之十九(对话框:颜色对话框)

    鸡啄米在上一节中为大家讲解了字体对话框的使用方法,熟悉了字体对话框,本节继续讲另一种通用对话框--颜色对话框. 颜色对话框大家肯定也不陌生,我们可以打开它选择需要的颜色,简单说,它的作用就是用来选择颜 ...

  10. Web安全之BurpSuite抓取HTTPS请求

    出现了问题,第一步要干什么呢? 当然是要去官方网站去找FAQ和help,先来练习一下英语 https://portswigger.net/burp/help/proxy_options_install ...