#include<bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; vector<vector<int>> graph; vector<int> val,dp,bn,dpp; int bnl; int fscie(int p,int q) { bn[p]=bnl; int mx=p; if(p==q) { bn[p]=0; return q; } for(auto I : graph[p]) { if(bn[I]==bnl) return -1; int tmp=fscie(I,q); if(tmp==-1) return -1; mx=max(mx,tmp); } bn[p]=0; return mx; } int main() { int n; scanf("%d",&n); graph.resize(n+1); val.resize(n+1); dp.resize(n+1); dpp.resize(n+1); bn.resize(n+1); for(int i=1;i<=n;++i) { val[i]=i; int il; scanf("%d",&il); for(int j=0;j<il;++j) { int tmp; scanf("%d",&tmp); val[i]=max(val[i],tmp); graph[i].pb(tmp); } dp[i]=val[i]; } ll suf[n+1]; for(int i=0;i<=n;++i) suf[i]=0; for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) { ++bnl; if(i==j) continue; int tmp=fscie(i,j); // printf("%d %d %d\n",i,j,tmp); if(tmp==-1) continue; ++suf[tmp]; } } ll su=0; for(int i=1;i<=n;++i) { su+=suf[i]; printf("%lld ",su); } printf("\n"); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include<bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; vector<vector<int>> graph; vector<int> val,dp,bn,dpp; int bnl; int fscie(int p,int q) { bn[p]=bnl; int mx=p; if(p==q) { bn[p]=0; return q; } for(auto I : graph[p]) { if(bn[I]==bnl) return -1; int tmp=fscie(I,q); if(tmp==-1) return -1; mx=max(mx,tmp); } bn[p]=0; return mx; } int main() { int n; scanf("%d",&n); graph.resize(n+1); val.resize(n+1); dp.resize(n+1); dpp.resize(n+1); bn.resize(n+1); for(int i=1;i<=n;++i) { val[i]=i; int il; scanf("%d",&il); for(int j=0;j<il;++j) { int tmp; scanf("%d",&tmp); val[i]=max(val[i],tmp); graph[i].pb(tmp); } dp[i]=val[i]; } ll suf[n+1]; for(int i=0;i<=n;++i) suf[i]=0; for(int i=1;i<=n;++i) { for(int j=1;j<=n;++j) { ++bnl; if(i==j) continue; int tmp=fscie(i,j); // printf("%d %d %d\n",i,j,tmp); if(tmp==-1) continue; ++suf[tmp]; } } ll su=0; for(int i=1;i<=n;++i) { su+=suf[i]; printf("%lld ",su); } printf("\n"); } |