Gym
来源:互联网 发布:split赋值给数组 编辑:程序博客网 时间:2024/06/12 18:51
题目地址:http://codeforces.com/gym/100792/problem/K
思路:
1.根据到达顺序,可以使用拓扑排序:a比b早,a->b。
2.要求编号为1的尽量靠前,满编号为1的节点尽量靠前的条件下编号为2的尽量靠前。。。。满足n-1尽量靠前的情况下编号为n的尽量靠前。若直接进行优先队列(标号小优先)+拓扑排序,对于样例3 1;3 1,由于2、3节点入度均为0,则都会入队,但此时2会先出队,1的出队时间变晚,不满足节点最小的尽量靠前。
3.满足条件也即为距度为0的点越远的编号越小的点应越优先输出。则将所有边反向,每次取编号尽量大的点,倒序输出即可。
#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn=200000+50;const int maxm=400000+50;struct Edge{ int to,nt;};int tot,n,m;int head[maxn];Edge edge[maxm];vector<int> ans;priority_queue<int,vector<int>,less<int> > q;int d[maxn];void init(){ tot=0; ans.clear(); memset(d,0,sizeof(d)); memset(head,-1,sizeof(head));}void addEdge(int u,int v){ edge[tot].to=v,edge[tot].nt=head[u],head[u]=tot++;}void solve(){ for(int i=1;i<=n;i++) { if(!d[i]) { q.push(i); } } while(!q.empty()) { int now=q.top(); q.pop(); ans.push_back(now); for(int i=head[now];~i;i=edge[i].nt) { int nt=edge[i].to; if(--d[nt]==0) { q.push(nt); } } } int flag=0; for(int i=ans.size()-1;i>=0;i--) { if(!flag) printf("%d",ans[i]),flag=1; else printf(" %d",ans[i]); } printf("\n");}int main(){ init(); scanf("%d%d",&n,&m); for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); addEdge(y,x); d[x]++; } solve(); return 0;}
阅读全文
0 0
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- 【HDU3586】Information Disturbing-二分答案+树形DP
- c#中接口和类的有什么异同,c#中的委托是什么?事件是不是一种委托?事件和委托的关系
- LWC 50:679. 24 Game
- JavaScript函数,递归
- 数组与矩阵---不包含本位置值的累乘数组
- Gym
- ACM-9月21日周四周中训练心得
- Android中对话框Dialog的创建
- 线段树------贴海报
- 392. Is Subsequence (greedy)
- jQuery单选框的回显
- 关于ehcache配置文件
- Apache的BeanUtils组件学习
- 2017/9/21训练日记