#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e5+7;
vector<int>V[LIM], S[LIM];
int ans[LIM], odw[LIM], in[LIM], kiedy[LIM], n;
void solve(int x) {
rep(i, n) {
S[i].clear();
odw[i]=0;
in[i]=V[i].size();
}
rep(i, x) for(auto j : V[i]) S[j].pb(i);
in[x]=0;
odw[x]=1;
queue<int>q;
q.push(x);
for(int i=x; i<n; ++i) {
if(i!=x) {
for(auto j : V[i]) if(odw[j]) --in[i]; else S[j].pb(i);
if(!in[i]) {
odw[i]=1;
q.push(i);
}
}
while(!q.empty()) {
int p=q.front(); q.pop();
if(p!=x) ++ans[i];
for(auto j : S[p]) {
--in[j];
if(!in[j]) {
odw[j]=1;
q.push(j);
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
rep(i, n) {
int l;
cin >> l;
while(l--) {
int a;
cin >> a; --a;
if(kiedy[a]<=i) {
V[i].pb(a);
kiedy[a]=i+1;
}
}
}
rep(i, n) solve(i);
rep(i, n) {
if(i) ans[i]+=ans[i-1];
cout << ans[i] << " ";
}
cout << '\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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int LIM=2e5+7; vector<int>V[LIM], S[LIM]; int ans[LIM], odw[LIM], in[LIM], kiedy[LIM], n; void solve(int x) { rep(i, n) { S[i].clear(); odw[i]=0; in[i]=V[i].size(); } rep(i, x) for(auto j : V[i]) S[j].pb(i); in[x]=0; odw[x]=1; queue<int>q; q.push(x); for(int i=x; i<n; ++i) { if(i!=x) { for(auto j : V[i]) if(odw[j]) --in[i]; else S[j].pb(i); if(!in[i]) { odw[i]=1; q.push(i); } } while(!q.empty()) { int p=q.front(); q.pop(); if(p!=x) ++ans[i]; for(auto j : S[p]) { --in[j]; if(!in[j]) { odw[j]=1; q.push(j); } } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; rep(i, n) { int l; cin >> l; while(l--) { int a; cin >> a; --a; if(kiedy[a]<=i) { V[i].pb(a); kiedy[a]=i+1; } } } rep(i, n) solve(i); rep(i, n) { if(i) ans[i]+=ans[i-1]; cout << ans[i] << " "; } cout << '\n'; } |
English