拓扑排序80分求助
  • 板块P2712 摄像头
  • 楼主lsortf
  • 当前回复6
  • 已保存回复7
  • 发布时间2023/12/3 12:28
  • 上次更新2023/12/3 15:07:26
查看原帖
拓扑排序80分求助
1043998
lsortf楼主2023/12/3 12:28
#include<bits/stdc++.h>
using namespace std;
vector<int> a[501];
queue<int> q;
int n,ru[501],wz[501],ans;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x,y,m;
		wz[i]=x;
		scanf("%d%d",&x,&m);
		for(int j=1;j<=m;j++){
			scanf("%d",&y);
			a[x].push_back(y);
			if(wz[i]>0) ru[y]++;
		}
	}
	for(int i=1;i<=n;i++) if(ru[wz[i]]==0&&wz[i]>0) q.push(wz[i]);
	int cnt=0;
	while(!q.empty()){
		int t=q.front();
		cnt++;
		q.pop();
		for(int i=0;i<a[t].size();i++){
			int xx=a[t][i];
			ru[xx]--;
			if(ru[xx]==0&&wz[xx]>0) q.push(xx);
		}
	}
	if(cnt==n) printf("YES");
	else printf("%d",n-cnt);
	return 0;
}
2023/12/3 12:28
加载中...