拓扑排序80分
查看原帖
拓扑排序80分
794701
wo_hen_la楼主2024/9/25 21:58

WAon #2#6#16#19

#include <bits/stdc++.h>
using namespace std;

#define sp putchar(' ')
#define en putchar('\n')
#define pb push_back
#define int long long
#define P 998244353
#define HP 1000000000000002097
#define N (int)(5e3+5)

int read(){ char ch=getchar();int x=0,f=1;while(ch>'9' || ch<'0'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();return x*f; }
void print(int x){ if(x<0) putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+'0'); }

int n,k,p,R;
int dis[N],in[N];
vector<int> e[N];
queue<int> r;

signed main()
{
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
	cin>>n>>k>>p;
	for(int i=1;i<=n;i++) dis[i]=-1;
	for(int i=1,a;i<=p;i++) cin>>a,r.push(a),dis[i]=0;
	cin>>R;
	while(R--){
		int v,s,u;
		cin>>v>>s;
		while(s--) cin>>u,e[u].pb(v),in[v]++;
	}
	while(!r.empty()){
		int u=r.front();
		r.pop();
		for(auto v:e[u]){
			dis[v]=max(dis[v],dis[u]+1);
			if(!--in[v]) r.push(v);
		}
	}
	//for(int i=1;i<=n;i++) cout<<dis[i]<<" ";
	cout<<dis[k];
	return 0;
}



2024/9/25 21:58
加载中...