BestCoder Round #35(DZY Loves Topological Sorting-堆+贪心)

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 most
k
edges from the graph.

Input

The input consists several test cases. (TestCase≤5)

The first line, three integers n,m,k(1≤n,m≤105,0≤k≤m).

Each of the next m
lines has two integers: u,v(u≠v,1≤u,v≤n),
representing a direct edge(u→v).

Output

For each test case, output the lexicographically largest topological ordering.

Sample Input

5 5 2
1 2
4 5
2 4
3 4
2 3
3 2 0
1 2
1 3

Sample Output

5 3 1 2 4
1 3 2

Hint

Case 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;
}
时间: 2024-08-29 19:11:14

BestCoder Round #35(DZY Loves Topological Sorting-堆+贪心)的相关文章

BestCoder Round #35(DZY Loves Balls-暴力dp)

DZY Loves Balls Accepts: 371 Submissions: 988 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description There are n  black balls and m  white balls in the big box. Now, DZY starts to randomly pick out the ba

hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]

传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 52 Problem Description A topological sort or topological ordering of a directed

DZY Loves Topological Sorting (BC #35 hdu 5195 topsort+优先队列)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 264    Accepted Submission(s): 63 Problem Description A topological sort or topological ordering of a directed gr

hdu.5195.DZY Loves Topological Sorting(topo排序 &amp;&amp; 贪心)

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 866    Accepted Submission(s): 250 Problem Description A topological sort or topological ordering of a directed g

Hdoj 5195 DZY Loves Topological Sorting 【拓扑】+【线段树】

DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 922 Accepted Submission(s): 269 Problem Description A topological sort or topological ordering of a directed graph i

hdu 5195 DZY Loves Topological Sorting 线段树+拓扑排序

DZY Loves Topological Sorting Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for ev

BC DZY Loves Topological Sorting

DZY Loves Topological Sorting Accepts: 112 Submissions: 586 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) 问题描述 一张有向图的拓扑序列是图中点的一个排列,满足对于图中的每条有向边(u→v) 从 u 到 v,都满足u在排列中出现在v之前. 现在,DZY有一张有向无环图(DAG).你要在最多删去k条边之后,求出字典序最大

HDU 5195 DZY Loves Topological Sorting 拓扑排序

题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5195 bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=573&pid=1002 代码: 1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio&

HDU 5195 - DZY Loves Topological Sorting

题意: 删去K条边,使拓扑排序后序列字典序最大 分析: 因为我们要求最后的拓扑序列字典序最大,所以一定要贪心地将标号越大的点越早入队.我们定义点i的入度为di. 假设当前还能删去k条边,那么我们一定会把当前还没入队的di≤k的最大的i找出来,把它的di条入边都删掉,然后加入拓扑序列. 删除的一定是小连大的边,因为大连小的边在拓扑序列生成的时候就去掉了 1 #include <iostream> 2 #include <cstdio> 3 #include <queue>