运用stl
代码 c++
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const double eps = 1e-6;
const int N=1e4+333;
const ll mod = 1e9+7;
int n,m,in[N*6],dis[N*6],cnt=1;
vector<int> g[N*10];
int main()
{
cin>>n>>m;
for (int i = 1;i <= m;i ++) {
int num;cin>>num;
set<int> s;
while(num--) {
int x;cin>>x;
s.insert(x);
}
int var = n+i;
for (int j=*s.begin();j<=*s.rbegin();j++) {
if(s.count(j)==1) {
g[var].push_back(j);in[j]++;
}
else {
g[j].push_back(var);in[var]++;
}
}
}
queue<int> q;
for (int i=1;i<=n+m;i++) {
if(in[i]==0) q.push(i);
if(i<=n) dis[i]=1;
}
while (!q.empty()) {
int node = q.front();
q.pop();int w;
if(node<=n) w=0;
else w=1;
for (int i=0;i<g[node].size();i++) {
int u =g[node][i];
dis[u]=max(dis[u],dis[node]+w);
in[u]--;
if(in[u]==0) q.push(u);
}
}
int ans=1;
for (int i=1;i<=n;i++)
ans=max(ans,dis[i]);
cout<<ans<<endl;
return 0;
}