Codeforces 825E Minimal Labels - 拓扑排序 - 贪心
Posted 阿波罗2003
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 825E Minimal Labels - 拓扑排序 - 贪心相关的知识,希望对你有一定的参考价值。
You are given a directed acyclic graph with n vertices and m edges. There are no self-loops or multiple edges between any pair of vertices. Graph can be disconnected.
You should assign labels to all vertices in such a way that:
- Labels form a valid permutation of length n — an integer sequence such that each integer from 1 to n appears exactly once in it.
- If there exists an edge from vertex v to vertex u then labelv should be smaller than labelu.
- Permutation should be lexicographically smallest among all suitable.
Find such sequence of labels to satisfy all the conditions.
The first line contains two integer numbers n, m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105).
Next m lines contain two integer numbers v and u (1 ≤ v, u ≤ n, v ≠ u) — edges of the graph. Edges are directed, graph doesn\'t contain loops or multiple edges.
Print n numbers — lexicographically smallest correct permutation of labels of vertices.
3 3 1 2 1 3 3 2
1 3 2
4 5 3 1 4 1 2 3 3 4 2 4
4 1 2 3
5 4 3 1 2 1 2 3 4 5
3 1 2 4 5
题目大意 给定一个有向无环图,用1~n为所有顶点标号,每个顶点的标号互不相同,如果有有一条边从v连向u,则v的标号应比u小,输出字典序最小的标号方案。
poj有一道一样的题,题解请戳这里。
Code
1 /** 2 * Codeforces 3 * Problem#825E 4 * Accepted 5 * Time: 46ms 6 * Memory: 5300k 7 */ 8 #include <bits/stdc++.h> 9 #ifndef WIN32 10 #define Auto "%lld" 11 #else 12 #define Auto "%I64d" 13 #endif 14 using namespace std; 15 typedef bool boolean; 16 const signed int inf = (signed)((1u << 31) - 1); 17 const double eps = 1e-6; 18 const int binary_limit = 128; 19 #define smin(a, b) a = min(a, b) 20 #define smax(a, b) a = max(a, b) 21 #define max3(a, b, c) max(a, max(b, c)) 22 #define min3(a, b, c) min(a, min(b, c)) 23 template<typename T> 24 inline boolean readInteger(T& u){ 25 char x; 26 int aFlag = 1; 27 while(!isdigit((x = getchar())) && x != \'-\' && x != -1); 28 if(x == -1) { 29 ungetc(x, stdin); 30 return false; 31 } 32 if(x == \'-\'){ 33 x = getchar(); 34 aFlag = -1; 35 } 36 for(u = x - \'0\'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - \'0\'); 37 ungetc(x, stdin); 38 u *= aFlag; 39 return true; 40 } 41 42 ///map template starts 43 typedef class Edge{ 44 public: 45 int end; 46 int next; 47 Edge(const int end = 0, const int next = -1):end(end), next(next){} 48 }Edge; 49 50 typedef class MapManager{ 51 public: 52 int ce; 53 int *h; 54 vector<Edge> edge; 55 MapManager(){} 56 MapManager(int points):ce(0){ 57 h = new int[(const int)(points + 1)]; 58 memset(h, -1, sizeof(int) * (points + 1)); 59 } 60 inline void addEdge(int from, int end){ 61 edge.push_back(Edge(end, h[from])); 62 h[from] = ce++; 63 } 64 inline void addDoubleEdge(int from, int end){ 65 addEdge(from, end); 66 addEdge(end, from); 67 } 68 Edge& operator [] (int pos) { 69 return edge[pos]; 70 } 71 inline void clear() { 72 edge.clear(); 73 delete[] h; 74 } 75 }MapManager; 76 #define m_begin(g, i) (g).h[(i)] 77 #define m_endpos -1 78 ///map template ends 79 80 int n, m; 81 MapManager g; 82 int* dag; 83 int* dep; 84 85 inline boolean init() { 86 if(!readInteger(n)) return false; 87 readInteger(m); 88 g = MapManager(n); 89 dag = new int[(n + 1)]; 90 dep = new int[(n + 1)]; 91 memset(dag, 0, sizeof(int) * (n + 1)); 92 for(int i = 1, a, b; i <= m; i++) { 93 readInteger(a); 94 readInteger(b); 95 g.addEdge(b, a); 96 dag[a]++; 97 } 98 return true; 99 } 100 101 priority_queue<int> que; 102 inline void topu() { 103 for(int i = 1; i <= n; i++) 104 if(!dag[i]) { 105 que.push(i); 106 } 107 int cnt = 0; 108 while(!que.empty()) { 109 int e = que.top(); 110 dep[e] = cnt++; 111 que.pop(); 112 for(int i = m_begin(g, e); i != m_endpos; i = g[i].next) { 113 int& eu = g[i].end; 114 dag[eu]--; 115 if(!dag[eu]) 116 que.push(eu); 117 } 118 } 119 } 120 121 inline void solve() { 122 topu(); 123 for(int i = 1; i <= n; i++) 124 printf("%d ", n - dep[i]); 125 } 126 127 int main() { 128 init(); 129 solve(); 130 return 0; 131 }
以上是关于Codeforces 825E Minimal Labels - 拓扑排序 - 贪心的主要内容,如果未能解决你的问题,请参考以下文章
[拓扑排序 反向建图] 825E - Minimal Labels
CodeForces-1175E Minimal Segment Cover
Codeforces1579 G. Minimal Coverage(dp的状态设计)