数据太水了
查看原帖
数据太水了
322403
litianjing楼主2023/9/10 16:29

样例没过,上交Ac,数据太水了

#include  <bits/stdc++.h>
using namespace std;
int n,m,in[100001],num[500001],ans[500001];
bool flag;vector<int> tu[100001];
priority_queue <int> q;
int read(){
	char ch;
	int x=0;
	ch=getchar();
	while(ch>'9'||ch<'0'){
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x;
}
int main(){
	int T;
	T=read();
	while(T--){
		memset(num,0,sizeof(0));
		memset(in,0,sizeof(0));
		memset(ans,0,sizeof(0));
		for(int i=1;i<=n;i++){
			tu[i].clear();
		}
		n=read(),m=read();
		for(int i=1;i<=m;i++){
			int x,y;
			x=read(),y=read();
			in[x]++;
			tu[y].push_back(x);
		}
		int k=0;
		for(int i=n;i>=1;i--){
			if(in[i]==0){
				q.push(i);
				k++;
			}
		}
		int tmp=0;
		while(!q.empty()){
			int x=q.top();
			q.pop();
			ans[++tmp]=x;
			for(int i=0;i<tu[x].size();i++){
				int y=tu[x][i];
				in[y]--;
				if(in[y]==0){
					q.push(y);
					k++;
				}
			}
		}
		if (k!=n){
			printf("Impossible!\n");
			continue;
		}
		for(int i=n;i>=1;i--){
			printf("%d ",ans[i]);
		}
		printf("\n");
	}
	return 0;
}

2023/9/10 16:29
加载中...