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 (uv) 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. ( TestCase5 )
The first line, three integers n,m,k(1n,m105,0km) .
Each of the next m lines has two integers: u,v(uv,1u,vn) , representing a direct edge (uv) .
 

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
 

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;
}






import cv2 as cv #添加椒盐噪声 import numpy as np # 读取图片 img=cv.imread(&#39;D:\\sztx\\dzy2\\rx1.png&#39;, cv.IMREAD_UNCHANGED) rows, cols, chn = img.shape # 添加椒盐噪声(随机) for i in range(50000): x = np.random.randint(0, rows) y = np.random.randint(0, cols) img[x, y, :] = 100 cv.imshow("noise", img) # 图像保存 cv.imwrite("jiaoyan.jpg", img) # 等待显示 cv.waitKey() cv.destroyAllWindows() import numpy as np #添加高斯噪声 import cv2 def gasuss_noise(image, mu=0.0, sigma=0.1): """ 添加高斯噪声 :param image: 输入的图像 :param mu: 均值 :param sigma: 标准差 :return: 含有高斯噪声的图像 """ image = np.array(image / 255, dtype=float) noise = np.random.normal(mu, sigma, image.shape) gauss_noise = image + noise if gauss_noise.min() < 0: low_clip = -1. else: low_clip = 0. gauss_noise = np.clip(gauss_noise, low_clip, 1.0) gauss_noise = np.uint8(gauss_noise * 255) return gauss_noise if __name__ == &#39;__main__&#39;: # ----------------------读取图片----------------------------- img = cv2.imread("D:\\sztx\\dzy2\\rx2.png") # --------------------添加高斯噪声--------------------------- out2 = gasuss_noise(img, mu=0.0, sigma=0.1) # ----------------------显示结果----------------------------- cv2.imshow(&#39;origion_pic&#39;, img) cv2.imshow(&#39;gauss_noise&#39;, out2) cv2.imwrite("gaosi.jpg", out2) cv2.waitKey(0) import cv2 #高斯平滑3*3 5*5 7*7 import datetime import matplotlib.pyplot as plt import numpy as np import PIL f = plt.imread("D:\\sztx\\dzy2\\rx2.png") image_3_LF = cv2.GaussianBlur(f,(3,3),0) #(3,3)为高斯半径 #image_3_LF为高斯滤波后的低分图像 plt.imsave("D:\\sztx\\dzy2\\rx3.png", image_3_LF) image_5_LF = cv2.GaussianBlur(f,(5,5),0) #(5,5)为高斯半径 #image_5_LF为高斯滤波后的低分图像 plt.imsave("D:\\sztx\\dzy2\\rx5.png", image_5_LF) image_7_LF = cv2.GaussianBlur(f,(7,7),0) #(7,7)为高斯半径 #image_7_LF为高斯滤波后的低分图像 plt.imsave("D:\\sztx\\dzy2\\rx7.png", image_7_LF)解析代码存在的问题
最新发布
06-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值