求助,链式前向星20pts,剩下都是MLE
查看原帖
求助,链式前向星20pts,剩下都是MLE
758664
ZXCVBASDFG楼主2022/10/4 16:22
#include<bits/stdc++.h>
using namespace std;

const int N=1e5+5,M=1e5+5;

struct node{
	int to,next;
};
node edge[M];
int n,m,cnt,head[N];

int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') w=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9'){
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*w;
}

int maxx(int x,int y){
	return (x>y)?x:y;
}

void add_edge(int x,int y){
	++cnt;
	edge[cnt].to=y;
	edge[cnt].next=head[x];
	head[x]=cnt;
}

int deal(int x){
	int maxn=x;
	for(int i=head[x];i!=0;i=edge[i].next){
		maxn=maxx(maxn,deal(edge[i].to));
	}
	return maxn;
}

int main(){
	n=read(); m=read();
	for(int i=0;i<m;++i){
		int x=read(),y=read();
		add_edge(x,y);
	}
	for(int i=1;i<=n;++i)
    		printf("%d ",deal(i));
	return 0;
}
2022/10/4 16:22
加载中...