BestCoder Round #35(DZY Loves Topological Sorting-堆+贪心)
来源:互联网 发布:淘宝维修售处理时间 编辑:程序博客网 时间:2024/04/30 21:48
DZY Loves Topological Sorting
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 323 Accepted Submission(s): 86
Problem Description
A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge(u→v) from vertex u to vertex v ,u comes before v in the ordering.
Now, DZY has a directed acyclic graph(DAG). You should find the lexicographically largest topological ordering after erasing at mostk edges from the graph.
Now, DZY has a directed acyclic graph(DAG). You should find the lexicographically largest topological ordering after erasing at most
Input
The input consists several test cases. (TestCase≤5 )
The first line, three integersn,m,k(1≤n,m≤105,0≤k≤m) .
Each of the nextm lines has two integers: u,v(u≠v,1≤u,v≤n) , representing a direct edge(u→v) .
The first line, three integers
Each of the next
Output
For each test case, output the lexicographically largest topological ordering.
Sample Input
5 5 21 24 52 43 42 33 2 01 21 3
Sample Output
5 3 1 2 41 3 2HintCase 1.Erase the edge (2->3),(4->5).And the lexicographically largest topological ordering is (5,3,1,2,4).
Source
BestCoder Round #35
Recommend
hujie | We have carefully selected several similar problems for you: 5197 5196 5193 5192 5191
直接堆+贪心。
显然下一个选取的是i,
当且仅当i是indegree<=剩余删边数nowk,中编号最大的
因此建一个堆,把indegree<=k的扔进去,推出最大编号,
值得一题的是我没算过复杂度,最后直接过了。。
可能nowk降低,是会把原来堆中的元素T出来,但我未能找出TLE的反例
PS:因为next是系统中已有函数所以不能用,,
#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<functional>#include<iostream>#include<cmath>#include<cctype>#include<ctime>#include<queue>using namespace std;#define For(i,n) for(int i=1;i<=n;i++)#define Fork(i,k,n) for(int i=k;i<=n;i++)#define Rep(i,n) for(int i=0;i<n;i++)#define ForD(i,n) for(int i=n;i;i--)#define RepD(i,n) for(int i=n;i>=0;i--)#define Forp(x) for(int p=pre[x];p;p=Next[p])#define Forpiter(x) for(int &p=iter[x];p;p=Next[p]) #define Lson (x<<1)#define Rson ((x<<1)+1)#define MEM(a) memset(a,0,sizeof(a));#define MEMI(a) memset(a,127,sizeof(a));#define MEMi(a) memset(a,128,sizeof(a));#define INF (2139062143)#define F (100000007)#define MAXN (200000+10)#define MAXM (200000+10) typedef long long ll;ll mul(ll a,ll b){return (a*b)%F;}ll add(ll a,ll b){return (a+b)%F;}ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}void upd(ll &a,ll b){a=(a%F+b%F)%F;}priority_queue<int> q;bool b[MAXN]={0};int indegree[MAXN]={0};int edge[MAXM],pre[MAXN],Next[MAXM],siz=0;void addedge(int u,int v){edge[++siz]=v;Next[siz]=pre[u];pre[u]=siz; }int n,m,k;int main(){//freopen("Sorting.in","r",stdin);while(scanf("%d%d%d",&n,&m,&k)==3){while(!q.empty()) q.pop();MEM(b) MEM(indegree) MEM(edge) MEM(pre) MEM(Next) siz=0; For(i,m){int u,v;scanf("%d%d",&u,&v);addedge(u,v);indegree[v]++;}For(i,n){if (indegree[i]<=k) b[i]=1,q.push(i);}int nowk=k;int flag=0;while(!q.empty()){int t;while(1){t=q.top();q.pop();if (indegree[t]>nowk) b[t]=0;else break;}nowk-=indegree[t];indegree[t]=0;Forp(t){int v=edge[p];indegree[v]--;if (indegree[v]<=nowk&&b[v]==0) b[v]=1,q.push(v); }if (flag) printf(" ");flag++; printf("%d",t);if (flag==n) break;} putchar('\n');}return 0;}
0 0
- BestCoder Round #35(DZY Loves Topological Sorting-堆+贪心)
- hdu 5195 DZY Loves Topological Sorting && BestCoder Round #35
- HDU 5195 DZY Loves Topological Sorting 拓扑序(贪心)
- BC DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting
- hdu 5195 DZY Loves Topological Sorting
- [线段树] hdu5195 DZY Loves Topological Sorting
- (拓扑排序)DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting
- HDU 5195 DZY Loves Topological Sorting
- 【HDU】5195-DZY Loves Topological Sorting(拓扑 + 线段树 + 贪心)
- DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)
- 【Best Coder】#35 DZY Loves Topological Sorting(线段树维护)
- HDU 5195 DZY Loves Topological Sorting(线段树)
- HDU 5195 DZY Loves Topological Sorting (拓扑排序+线段树)
- hdu5195 DZY Loves Topological Sorting 线段树+拓扑排序
- Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】
- HDU 5195 DZY Loves Topological Sorting(优先队列)
- GCDAsyncSocket.
- DNS解析过程简析
- 有5个字符串,要求对他们按由小到大 string
- ARM Linux启动流程分析——内核自解压阶段
- 记梦--物道奇缘
- BestCoder Round #35(DZY Loves Topological Sorting-堆+贪心)
- 黑马程序员——Java基础知识——IO(Properties、打印流、IO其他类、字符编码)
- 设置顶部系统栏颜色和actionbar颜色相同
- Junit框架使用(1)--基本使用
- flume+kafka+storm+mysql
- LeetCode First Missing Positive
- Largest Number
- 第三周项目5-数组作数据成员(1)
- int**a = new int[5][6] 怎么delete