codeforces 825E(拓扑)
来源:互联网 发布:开外眼角知乎 编辑:程序博客网 时间:2024/06/06 18:55
题意:要求输出字典序最小的拓扑序列。
思路:反向建个图,然后选取最大值进行拓扑。
PS :一直想不到这个反向,当初傻傻地交了一发直接图跑最小,然后回出现当选取的大的时候,会使得小的比原来的更小的情况。
#include<bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<int,int> P;#define fi first#define se second#define INF 0x3f3f3f3f#define clr(x,y) memset(x,y,sizeof x)#define PI acos(-1.0)#define ITER set<int>::iteratorconst int Mod = 1e9 + 7;const int maxn = 1e5 + 10;struct Edge{int to,next;}edge[maxn];int head[maxn];int edge_num;int in[maxn];int ans[maxn];void add_edge(int x,int y){edge[edge_num] = (Edge){y,head[x]};head[x] = edge_num ++;}void Init(){ clr(head,-1);edge_num = 0;clr(in,0);}priority_queue<int,vector<int>,less<int> >q;int main(){ int n,m; while( ~ scanf("%d%d",&n,&m)) { Init(); for(int i = 1;i <= m;i ++){int x,y;scanf("%d%d",&x,&y);add_edge(y,x);in[x] ++;} while(!q.empty())q.pop(); for(int i = 1; i <= n;i ++){if(!in[i]){q.push(i);}} int cnt = n; while(!q.empty()) { int u = q.top();q.pop(); ans[u] = cnt --; for(int i = head[u];i != -1;i = edge[i].next) { int v = edge[i].to;in[v] --; if(!in[v])q.push(v); } } for(int i = 1;i <= n;i ++)printf("%d%c",ans[i],i < n ? ' ':'\n'); } return 0;}
阅读全文
0 0
- codeforces 825E(拓扑)
- CodeForces 825E Educational #25 E:拓扑排序+优先队列
- [拓扑序] Educational Codeforces Round 25 825E. Minimal Labels
- codeforces 825E Minimum Label 拓扑排序+逆向思维贪心
- Codeforces 651E Table Compression 拓扑序
- HDU 4857 逃生 && Codeforces 825 E. Minimal Labels 逆向拓扑序+优先队列
- Educational Codeforces Round 25 E. Minimal Labels(拓扑排序)
- Educational Codeforces Round 25E. Minimal Labels(拓扑排序+思维)
- Codeforces 270E Flawed Flow【思维+类拓扑排序】
- cf 825E Minimal Labels 【拓扑】
- codeforces 825 E Minimal Labels
- Codeforces 825E Minimal Labels
- CodeForces #303E Div.2 Paths and Trees(最短路+拓扑排序)
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- codeforce 825E. Minimal Labels 拓扑排序 贪心思想
- Codeforces Round #410 (Div. 2) E. Mike and code of a permutation(拓扑序+线段树)
- codeforces 163E e-Government
- 【Codeforces 163E】E-Government
- 写给软件工程的你!
- 基础练习 01字串
- MFC树形控件
- 倍增
- 基础练习 字母图形
- codeforces 825E(拓扑)
- BZOJ4383: [POI2015]Pustynia
- 基础练习 数列特征
- 当一个程序员写不出代码了,该怎么办?
- 哎,一个被前妻勒索巨额财产而致死的程序员
- 程序员如何在当今就业市场中让自己脱颖而出
- 中国程序员真的过多?浑水摸鱼的该清醒了!
- IT应聘者的简历应该是怎么样的?
- 7 个新的热门 IT 工种