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 }

 

转载于:https://www.cnblogs.com/yyf0309/p/7290124.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Springboot+Vue的智慧养老服务管理系统源码+项目使用说明(优秀毕业设计).zip 该项目属于个人毕业设计,经导师的精心指导与严格评审获得高分通过的设计项目。主要针对计算机相关专业的教师、正在做毕设、课设的学生使用,也可作为项目实战演练,可直接作为课程设计、期末大作业、毕设等。 1.项目代码功能经验证ok,确保稳定可靠运行。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通。 2.主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 3.项目具有丰富的拓展空间,不仅可作为入门进阶,也可直接作为毕设、课程设计、大作业、初期项目立项演示等用途。 4.当然也鼓励大家基于此进行二次开发。 5.期待你能在项目中找到乐趣和灵感,也欢迎你的分享和反馈! 介绍 随着人口老龄化趋势的加剧,老年人口比例不断增加,传统的养老服务模式已经难以满足现代老年人的多元化需求。因此,如何高效、精准地提供养老服务,成为了社会关注的焦点。信息化与智能化的发展为养老服务带来了新的机遇。智慧养老作为新兴的养老服务模式,正逐步改变传统的养老服务格局。 #### 安装教程 1. 运行环境准备mysql8+java8+npm14.16.1 2. 配置maven路径,加载依赖 3. 运行sql文件,确保application.yml的数据库名称和账号密码是数据库所在主机的账号密码 #### 使用说明 1. 登入 账号:admin 密码:admin 账号:user 密码:123456 2.运行流程 前端初始化指令: > npm install 前端运行指令(有两个端,一个是管理端,一个是用户端): > npm run serve #### 项目演示 + 普通用户 访问者可以在最顶端看到系统的导航栏,根据自己的需求点击需要去到的页面。用户可以单击“入院指南”,在入院指南界面的输入框中输入标题并进行搜索,就可以查看标题、发布时间、注意事项、发布人、封面等其他信息,在个人中心界面中,可以看见家属的账号、密码和性别等基本信息,还可以对这些信息进行更新操作。 ![GIF 2024-6-17 19-49-40](GIF 2024-6-17 19-49-40-1718630421956.gif) + 管理员 管理员单击家属管理,在家属管理页面中输入家属的各项信息后,在页面中进行查询、新增或删除家属信息等操作。管理员点击护工管理,在护工管理页面中输入护工的所有信息,然后在页面上可以点击详情来查看护工的详细情况,点击修改来修改护工的信息,点击删除来删除护工的信息。管理员单击房间资料管理,在这个页面中可以对房间资料的信息进行管理。管理员单击床位管理,可以查看房间号、楼房名称等各项信息,还可以点击修改来修改床位信息,点击删除来删除床位信息。管理员还可以对老人入住进行管理,在这个界面中可以查看像入住编号、老人年龄等老人入住的详细信息,可以在界面顶端输入具体信息来查找,还可以点击删除来进行删除操作。管理员单击外出报备管理,在外出报备管理界面中可以查看老人的外出报备情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值