抽象出差分约束 然后还有一点就是建立超级源点 优化建图
然后就是比较有趣的拓扑图求差分约束了其实spfa也可
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e6+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int n,q,m;
int e[N],ne[N],w[N],h[N],idx;
void add(int a,int b,int c){
e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
}
bool stop[N];
int din[N];
vector<int>ans;
int ranks[N];
void topsort()
{
queue<int>q;
for(int i=1;i<=n;i++)ranks[i] = 1;
for(int i=1;i<=n+m;++i)if(!din[i]){q.push(i);}
while(q.size()){
int t = q.front();
q.pop();
ans.push_back(t);
for(int i=h[t];~i;i=ne[i]){
int j = e[i];
din[j]--;
if(!din[j])q.push(j);
}
}
for(int &ver:ans){
for(int i=h[ver];~i;i=ne[i]){
int j = e[i];
ranks[j] = max(ranks[j],ranks[ver]+w[i]);
}
}
int res = 0;
for(int i=1;i<=n;i++)res = max(res,ranks[i]);
cout<<res;
}
void solve()
{
cin>>n>>m;
memset(h,-1,sizeof h);
for(int i=1;i<=m;i++){
int num;cin>>num;
for(int j=1;j<=n;j++)stop[j] = false;
int st,ed;st = 0x3f3f3f,ed = 0;
for(int j=1;j<=num;++j){
int x;cin>>x;
stop[x] = true;
st = min(st,x);
ed = max(ed,x);
}
int ver = n+i;
for(int j=st;j<=ed;++j){
if(!stop[j]){add(j,ver,0);din[ver]++;}
else {add(ver,j,1);din[j]++;}
}
}
topsort();
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int _;
//cin>>_;
_ = 1;
while(_--)solve();
return 0;
}