好久没发帖了,放假了来水一波。
代码:
#include<iostream> #include<cstring> #include<algorithm> using namespace std;const int N = 200010, mod = 80112002;int n, m; int nn; int e[N], h[N], ne[N], idx; int q[N], in[N], chu[N], f[N]; int maxx;int hh = 0, tt = -1, res; void add(int a, int b) {e[idx] = b;ne[idx] = h[a];h[a] = idx++; }int main() {cin >> n;memset(h, -1, sizeof h);for (int i = 1; i <= n; i++){int a, x;cin >> a >> x;f[a] = 1;maxx = max(maxx, a);for (int i = 1; i <= x; i++){int b;cin >> b;add(a, b);in[b]++;}}for (int i = 1; i <= maxx; i++){if (in[i] == 0&&f[i]==1)q[++tt] = i;}while (hh <= tt){auto t = q[hh++];for (int i = h[t]; i != -1; i = ne[i]){int j = e[i];in[j]--;if (in[j] == 0 && f[j] == 1){q[++tt] = j;}}}if (tt + 1 == n)cout << "YES" << endl;else cout << n - tt - 1 << endl;return 0; }
拓扑排序的一个板子题,但是需要记录一下是否存在摄像头(因为这个调了半个小时,草)