60分求助
查看原帖
60分求助
1385996
ZSYhaouuan楼主2024/9/8 22:39

本人刚学拓扑第1天

#include<bits/stdc++.h>
using namespace std;
vector<int> G[1000000+10];
int ind[1000000+10];
bool have[1000000+10];
int cnt;
int n;
void topuSort(){
	
	queue<int> q;
	for(int i=1;i<=n;i++){
		if(ind[i]==0 && have[i]==1){
			q.push(i);
		}
	}
	
	while(!q.empty()){
		int x=q.front();
		q.pop();
		cnt++;
		for(auto y:G[x]){
			if(have[y]==1){
				ind[y]--;
				if(ind[y]==0){
					q.push(y);
				}
			}
			
		}
	}
	if(cnt==n){
		cout<<"Yes";
		return;
	}else{
		cout<<n-cnt;
		return;
	}
}
int main(){
	
	cin>>n;
	for(int i=1;i<=n;i++){
		int x,k;
		cin>>x>>k;
		have[x]=1;
		while(k--){
			int t;
			cin>>t;
			G[x].push_back(t);
			ind[t]++;
		}
	}
	
	topuSort();
	return 0;
}
2024/9/8 22:39
加载中...