蒟蒻60分求助
查看原帖
蒟蒻60分求助
302310
单念·Q楼主2020/12/4 08:45

60分,后四个点全TLE...

#include<bits/stdc++.h>

using namespace std;
const int len = int (1e7) + 10;

int n,m,head[len],js,xx;
int c1,c2;
bool p[len];

struct edge{//图 
	int u,v,d,next;
}bi[len];

inline int read()//快读 
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}

void insert(int u,int v)//建图 
{
	++js;
	bi[js].u=u;
	bi[js].v=v;
	bi[js].next=head[u];
	head[u]=js;
}

int work(int x)//搜索 
{
	xx=max(xx,x);
	if(p[x]==1) return xx;
	p[x]=1;
	for(int i=head[x];i!=0;i=bi[i].next)
	work(bi[i].v);
	return xx;
}
int main()
{
	n=read();
	m=read();
	
	for(int i=1;i<=m;i++)
	{
		c1=read();
		c2=read();
		
		insert(c1,c2);
	}
	for(int i=1;i<=n;i++)
	{
		xx=0;
		memset(p,0,sizeof(p));
		int y=work(i);
		printf("%d ",y);//输出 
	}
	return 0;
}
2020/12/4 08:45
加载中...