Educational Codeforces Round 25 E. Minimal Labels&&hdu1258
这两道题都需要用到拓扑排序,所以先介绍一下什么叫做拓扑排序。
这里说一下我是怎么理解的,拓扑排序实在DAG中进行的,根据图中的有向边的方向决定大小关系,具体可以下面的题目中理解其含义
Educational Codeforces Round 25 E. Minimal Labels
题目链接:http://codeforces.com/contest/825/problem/E
题意:给你一个有向无环图(DAG),有n个顶点,m条边,顶点的序号分别是1-n,现在给你1-n的数对n个顶点进行赋值,赋值有一定的要求让如果有一条边是x->y,那么x的权值小于y,最后,如果有多种赋值方式输出字典序最小的方式,这个题的图可以是由几个连通块组成。
一开始不会做,后来听说是拓扑排序裸题,所以就去学了一下,还是很简单的,关键在于这道题需要反向建图,直接上代码
//Author: xiaowuga
#include <bits/stdc++.h>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
const long long N=+;
using namespace std;
typedef long long LL;
vector<int>q[N];
priority_queue<int>p;//优先队列,用来维护字典序
int main() {
int n,m;
scanf("%d%d",&n,&m);
int in[N]={};
for(int i=;i<m;i++){
int x,y;
scanf("%d%d",&x,&y);
q[y].push_back(x);//反向建图
in[x]++;//记录每个点出度
}
for(int i=;i<=n;i++) if(in[i]==) p.push(i);//在剪边前,把出度为0(叶子节点)的点压入堆
int now=n,ans[N];
//由于优先队列是大顶堆,每次回选出当前堆里最大的元素,出来赋值,保证了字典序最小
while(!p.empty()){
int t=p.top();p.pop();//
ans[t]=now--;
for(int i=;i<q[t].size();i++){
if(--in[q[t][i]]==) p.push(q[t][i]);//剪边,将剪完边以后出度为0的顶点压入堆
}
}
for(int i=;i<=n;i++){
printf("%d ",ans[i]);
}
cout<<endl;
return ;
}
这道题的关键是在于需要反向建图,原因是它需要一个点所有边的弧尾都标完号才能确定这个点的标号,意思就是每个点的后续节点都赋值了以后我才能对这个点赋值(因为这个点的赋值要比他的所有后续节点要小)。所以后续节点决定他前序节点的赋值,所以我们通过反向建图,从后往前拓扑排序,用优先队列维护字典序,并一路赋值,就可以AC了。
HDU1285
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285
题意:中文问题,懒的解释,自己看吧,题目已经写得很清楚了。与上一道题不同的是这道题正向建图,直接上代码。
//Author: xiaowuga
#include <bits/stdc++.h>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
const long long N=;
using namespace std;
typedef long long L;
int p[N][N];
int in[N];
priority_queue<int,vector<int>,greater<int> > q;
int main() {
ios::sync_with_stdio(false);cin.tie();
int n,m;
while(cin>>n>>m){
memset(p,,sizeof(p));
memset(in,,sizeof(in));
while(!q.empty()) q.pop();
for(int i=;i<m;i++){
int x,y;
cin>>x>>y;
if(p[x][y]==){
p[x][y]=;
in[y]++;//记录点的入度
}
}
for(int i=;i<=n;i++) if(in[i]==) q.push(i);//将入度为0的点压入堆
int ct=;
while(!q.empty()){
int t=q.top();q.pop();
if(ct!=n){
cout<<t<<" ";
ct++;
}
else cout<<t<<endl;
for(int i=;i<=n;i++){
if(p[t][i]==) continue;
if(--in[i]==) q.push(i);//自树根到树叶的剪边
}
}
}
return ;
}
这道题需要正向建图原因是
Educational Codeforces Round 25 E. Minimal Labels&&hdu1258的更多相关文章
- Educational Codeforces Round 25 E. Minimal Labels 拓扑排序+逆向建图
E. Minimal Labels time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 25 Five-In-a-Row(DFS)
题目网址:http://codeforces.com/contest/825/problem/B 题目: Alice and Bob play 5-in-a-row game. They have ...
- Educational Codeforces Round 25 A,B,C,D
A:链接:http://codeforces.com/contest/825/problem/A 解题思路: 一开始以为是个进制转换后面发现是我想多了,就是统计有多少个1然后碰到0输出就行,没看清题意 ...
- Educational Codeforces Round 25 C. Multi-judge Solving
题目链接:http://codeforces.com/contest/825/problem/C C. Multi-judge Solving time limit per test 1 second ...
- Educational Codeforces Round 25 B. Five-In-a-Row
题目链接:http://codeforces.com/contest/825/problem/B B. Five-In-a-Row time limit per test 1 second memor ...
- Educational Codeforces Round 25
A 题意:给你一个01的字符串,0是个分界点,0把这个字符串分成(0的个数+1)个部分,分别求出这几部分1的个数.例如110011101 输出2031,100输出100,1001输出101 代码: # ...
- Educational Codeforces Round 25 D - Suitable Replacement(贪心)
题目大意:给你字符串s,和t,字符串s中的'?'可以用字符串t中的字符代替,要求使得最后得到的字符串s(可以将s中的字符位置两两交换,任意位置任意次数)中含有的子串t最多. 解题思路: 因为知道s中的 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
随机推荐
- Spring学习9-MyEclipse中Spring工程使用@Resource注释的问题
在MyEclipse 的Spring工程中,有时候要使用@Resource注释来驱动Spring配置.但是在MyEclipse添加Spring开发能力的操作中,并没有 把相关的库添加到工程的class ...
- Python 数据驱动ddt 使用
准备工作: pip install ddt 知识点: 一,数据驱动和代码驱动: 数据驱动的意思是 根据你提供的数据来测试的 比如 ATP框架 需要excel里面的测试用例 代码驱动是必须得写代码 ...
- Java Mail(一):telnet实现发送收取邮件
http://blog.csdn.net/ghsau/article/details/8602076 ******************************* 最近要做一个解析邮件的东东,就顺便 ...
- 解决:ubuntu 里文件夹带锁
sudo chown -R <user-name> <folder-name> /* 其中-R的意思是recursive,你懂的,chown --help可以查看帮助信息 */ ...
- oracle db_*和v$*表
dba_开头 dba_users 数据库用户信息 dba_segments 表段信息 dba_extents 数据区信息 dba_objects 数据库对象信息 ...
- centos7 /etc/rc.local需要chmod +x /etc/rc.d/rc.local
Centos 7.0设置/etc/rc.local无效问题解决 安装centos7以后按照以往习惯修改rc.local添加开机启动命令,但重启后发现无效,再次重启发现依然如故 检查系统rc.local ...
- HeadFirst jsp 08 无脚本JSP
web页面设计人员真的必须懂 java ? web页面人员可以很快学习 EL 语言. 目前不知道 EL 应用前景如何, 但是我们香港系统没有使用 EL. include 指令 include指令告诉容 ...
- Android Studio多渠道打包的使用
项目地址 https://github.com/mcxiaoke/gradle-packer-plugin 项目介绍 gradle-packer-plugin 是Android多渠道打包工具Gradl ...
- Linux - Ubuntu开启SSH服务
SSH分客户端openssh-client和openssh-server. 如果你只是想登陆别的机器的SSH只需要安装openssh-client(ubuntu有默认安装,如果没有则sudo apt- ...
- 在XML中用于注释的符号是。(选择1项)
A.<!– –> B.<?– –?> C.<% %> D.<!– –!> 解答:A