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 ...
随机推荐
- 管道相关函数(1)-pipe
定义: int pipe(int filedes[2]); 表头文件: #include<unistd.h> 说明: pipe()会建立管道, 并将文件描述词由参数filedes数组返回. ...
- Acquiring Heap Dumps
Acquiring Heap Dumps HPROF Binary Heap Dumps Get Heap Dump on an OutOfMemoryError One can get a HP ...
- 区别:Use MFC In A Shared DLL 和 Use MFC In A Static Library
摘自:Programming Windows with MFC, 2nd Edition Choosing Use MFC In A Shared DLL minimizes your applica ...
- linux查看匹配内容的前后几行(转)
linux系统中,利用grep打印匹配的上下几行 如果在只是想匹配模式的上下几行,grep可以实现. $grep -5 'parttern' inputfile //打印匹配行的前后5行 ...
- JSF中获得HTTP SESSION和Request
转载自:http://blog.sina.com.cn/s/blog_872758480100waew.html 为了保持向后兼容,我们有时可能会需要访问session对象.在JSF中可以通过如下方式 ...
- eclipse 编译tomcat8.0.26的源码
第一次写东西, 如果有不对的地方,请大神指正,我会尽快修正…… 参考:http://www.cnblogs.com/lanxuezaipiao/p/3640923.html 1.从tomcat官网(h ...
- Toad 补充与培训 & 常用菜单
Toad 常用菜单 新版本 toad 软件中, 比较有用的菜单 (toad10.6 版本) 下边菜单, 在日常工作中出现过的, 显示为 粉色 , 蓝色 表示次一级的重要 session 菜单 new ...
- EasyUI 创建Tree
tree可以被从标记创建.easyui tree应该定义在ul元素中.无序列表ul元素提供了基本tree结构.每一个li元素被产生一个tree节点,子ul元素产生父tree节点.例子: < ...
- 打印-print.js
//打印开始// strPrintName 打印任务名// printDatagrid 要打印的datagridfunction CreateFormPage(ctx,strPrintName, pr ...
- [android] android 获取网络连接信息
效果图: 工具类 /** * 获取网络连接信息 * * 根据NetworkInfo可以知道有很多的连接方式和信息 * * ① 当没有任何可用网络的时候,networkinfo为null 判断netw ...