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.

Input

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.

Output

Print n numbers — lexicographically smallest correct permutation of labels of vertices.

Examples
Input
3 3 1 2 1 3 3 2
Output
1 3 2 
Input
4 5 3 1 4 1 2 3 3 4 2 4
Output
4 1 2 3 
Input
5 4 3 1 2 1 2 3 4 5
Output
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 }

 

posted @ 2017-08-05 14:16  阿波罗2003  阅读(478)  评论(0编辑  收藏  举报