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 ...
随机推荐
- Day01 html详解
day01 html详解 1.html的简介 1.1 什么是html? - HyperText Markup Language:超文本标记语言,网页语言 ...
- DZNEmptyDataSet 使用
gitHub地址:https://github.com/dzenbot/DZNEmptyDataSet 效果图: 代码: #import "UIScrollView+EmptyDataSet ...
- android Dialog官方demo
1.普通的Dialog AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("今天 ...
- elasticsearch-hadoop使用
elasticsearch-hadoop是一个深度集成Hadoop和ElasticSearch的项目,也是ES官方来维护的一个子项目,通过实现Hadoop和ES之间的输入输出,可以在Hadoop里面对 ...
- CentOS 7 SSH 免密登录的方法
先决条件 3 台 CentOS 7 HOSTNAME IP ROLE server1 10.8.26.197 Master server2 10.8.26.196 Slave1 server3 10. ...
- 数据挖掘-关联分析 Apriori算法和FP-growth 算法
•1.关联分析概念 关联分析是从大量数据中发现项集之间有趣的关联和相关联系. •定义:1.事务:每一条交易称为一个事务,如上图包含5个事务.2.项:交易的每一个物品称为一个项,例如豆奶,啤酒等. ...
- php根据路径获取文件名
<?php // 根据路径返回文件名 $path = 'J:\abc\defg\hijk\一个文件夹\lmn\opq'; $path = iconv("UTF-8", &qu ...
- 1 :2 Strust2—Demo
=============================================================== Demo基础包:
- VS2010/MFC编程入门之十九(对话框:颜色对话框)
鸡啄米在上一节中为大家讲解了字体对话框的使用方法,熟悉了字体对话框,本节继续讲另一种通用对话框--颜色对话框. 颜色对话框大家肯定也不陌生,我们可以打开它选择需要的颜色,简单说,它的作用就是用来选择颜 ...
- Web安全之BurpSuite抓取HTTPS请求
出现了问题,第一步要干什么呢? 当然是要去官方网站去找FAQ和help,先来练习一下英语 https://portswigger.net/burp/help/proxy_options_install ...