80分求调
查看原帖
80分求调
1394430
SatoruGojo楼主2025/1/18 14:42
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n;
queue <int> q;
vector <int> e[maxn];
int in[maxn];
bool vis[maxn];
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int x, m;
		cin >> x >> m;
		vis[x] = 1;
		for (int j = 1; j <= m; j++)
		{
			int y;
			cin >> y;
			e[x].push_back(y);	
			in[y]++;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		if (in[i] == 0)
		{
			q.push(i);
		}
	}
	int ans = 0;
	while(!q.empty())
	{	
		ans++;
		int x = q.front();
		q.pop(); 
		for (auto y : e[x])
		{
			in[y]--;
			if (in[y] == 0 && vis[y])
			{
				q.push(y);
			}
		}
	}
	if (ans == n)
	{
		cout << "YES" << endl;
	}
	else
	{
		cout << n - ans << endl;
	}
	return 0;
}
2025/1/18 14:42
加载中...