noip模拟赛(10.4) 字典序(dictionary)
来源:互联网 发布:淘宝密令红包怎么抢 编辑:程序博客网 时间:2024/06/05 16:31
【题目描述】
你需要构造一个1~n的排列,使得它满足m个条件,每个条件形如(ai,bi),表示ai必须在bi前面。在此基础上,你需要使它的字典序最小。
【输入数据】
第一行两个正整数n,m。接下来m行每行两个数ai,bi。
【输出数据】
输出一行n个整数表示答案。如果不存在这样的排列,输出-1。
【样例读入】
5 4
5 4
5 3
4 2
3 2
【样例输出】
1 5 3 4 2
【数据范围】
对于20%的数据,n,m<=10。
对于40%的数据,n,m<=200。
对于60%的数据,n,m<=1000。
对于100%的数据,n,m<=100000。
【题解】
按字典序的题目可以想到建图。要保证题目要求的先后顺序则在点ai和bi间建一条指向bi的有向边,并将无入度的点插入一个大根堆。每次弹出堆顶的元素,按弹出的先后顺序在ans数组中,并且更新该点的出度,将该点的出边全部删除,若删除产生了其他无入度点,则将其也插入堆中,如此往复。
#include<queue>#include<vector>#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;const int N=101111;inline int read(){int x=0,c=getchar(),f=1;while(c<48||c>57){if(c=='-')f=-1;c=getchar();}while(c>47&&c<58)x=x*10+c-48,c=getchar();return x*f;}struct edge{int v,nxt;}e[N];int n,m,ect,sum,fst[N],ind[N],ans[N];priority_queue<int,vector<int>,greater<int> > q;int main(){n=read(),m=read();while(m--){int u=read(),v=read();e[++ect].nxt=fst[u];fst[u]=ect;e[ect].v=v;ind[v]++;}for(int i=1;i<=n;i++)if(!ind[i]){q.push(i);sum++;}while(!q.empty()){int x=q.top();q.pop();ans[++ans[0]]=x;for(int i=fst[x];i;i=e[i].nxt){ind[e[i].v]--;if(!ind[e[i].v]){q.push(e[i].v);sum++;}}}if(sum<n)puts("-1");elsefor(int i=1;i<=n;i++)printf("%d ",ans[i]);return 0;}
1 0
- noip模拟赛(10.4) 字典序(dictionary)
- Python字典(Dictionary)
- swift-字典(Dictionary)
- Swift-字典(Dictionary)
- python dictionary(字典)
- 字典(dictionary)的设计
- Python中的字典(Dictionary)
- POJ 1750 Dictionary(模拟)
- noip模拟赛(10.4) 背包(pack)
- noip模拟赛(10.4) 序列(sequence)
- [noip模拟赛]祖先(dp)
- [noip模拟赛]旅行Pod(spfa)
- [noip模拟赛]二进制(dp)
- [noip模拟赛]求和(快速幂)
- [noip模拟赛]中位数(乱搞)
- [noip模拟赛]邮递员送信(spfa)
- [noip模拟赛]敲砖块(dp)
- noip模拟赛(一)魔法树
- 如何对示波器中的参数和图片进行处理心得整理
- 百度云的产品及使用场景分析
- Java-IO之CharArrayReader
- 使用FutureTask和Callable创建可获取返回值的多线程
- axure学习笔记
- noip模拟赛(10.4) 字典序(dictionary)
- getApplication()和getApplicationContext()区别
- [4] ffmpeg + SDL2 实现的有杂音的音频播放器
- android 列表ListView和可扩展列表ExpandableListView的实现
- Java-IO之CharArrayWriter(字符数组输出流)
- Bundle类传递可序列化对象
- Java Web测试题(三)
- axure7.0教程_多值单变量的页面传值
- stm32通用定时器产生pwm时 模式1和模式2有什么区别