学习了一种前向星的写法
#include <bits/stdc++.h>
using namespace std;
typedef int lint;
typedef long long LL;
const int maxm = 2000000;
const int maxn = 2000000;
int ver[maxm],ne[maxm],he[maxn],tot;
void add( int x,int y ){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
}
int k;
int cal( int x ){
int mx = 0;
for( int i = 1;i <= k;i++ ){
while( he[i] && ver[ he[i] ] > x){
he[i] = ne[ he[i] ];
}
if( !he[i] ) return -1;
mx = max( mx,x-ver[he[i]] );
}
return mx;
}
int main(){
int n;
vector<int> ve;
scanf("%d%d",&n,&k);
for( int i = 1;i <= k;i++ ){
int c,x;
scanf("%d",&c);
for( int j = 1;j <= c;j++ ){
scanf("%d",&x);add(i,x);ve.push_back(x);
}
}
sort( ve.begin(),ve.end() );
ve.erase( unique( ve.begin(),ve.end() ),ve.end() );
int ans = 0x3f3f3f3f;
for( int i =(int)ve.size()-1;i >= 0;i-- ){
int cnt = 0;
if( (cnt = cal( ve[i] ))==-1 ) break;
ans = min( ans,cnt );
}
printf("%d",ans);
return 0;
}