Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma GCC optimize("Ofast,no-stack-protector")
- #include<bits/stdc++.h>
- #define int long long
- #define quick ios::sync_with_stdio(0);cin.tie(0);
- #define rep(x,a,b) for(int x=a;x<=b;x++)
- #define repd(x,a,b) for(int x=a;x>=b;x--)
- #define F first
- #define S second
- #define all(x) x.begin(),x.end()
- #define mp make_pair
- #define eb emplace_back
- #define sz(x) (int)(x.size())
- #define lowbit(x) (x&-x)
- using namespace std;
- typedef pair<int,int> pii;
- typedef complex<int> P;
- #define X real()
- #define Y imag()
- void debug(){
- cout<<"\n";
- }
- template<class T,class ...U>
- void debug(T a,U ...b){
- cout<<a<<" ",debug(b...);
- }
- const int N=2500+7;
- const int INF=1e18;
- vector<int> v[N];
- int low[N];
- int in[N];
- int t=1;
- int bridges=0;
- //set<pii> Bridges;
- void dfs(int x,int p=-1){
- low[x]=in[x]=t++;
- for(int i:v[x]){
- if(i==p) continue;
- if(in[i]) low[x]=min(low[x],in[i]);
- else{
- dfs(i,x);
- if(low[i]>in[x]){//(x,i) is bridge
- bridges++;
- }
- low[x]=min(low[x],low[i]);
- }
- }
- }
- struct Disjoint{
- int p[N];
- int s[N];
- void init(int n){
- rep(i,0,n) s[i]=1,p[i]=i;
- }
- int fp(int x){
- if(x!=p[x]) p[x]=fp(p[x]);
- return p[x];
- }
- void Union(int a,int b){
- a=fp(a);
- b=fp(b);
- if(a==b) return;
- if(s[a]<s[b]) swap(a,b);
- s[a]+=s[b];
- p[b]=a;
- }
- bool issame(int a,int b) {return fp(a)==fp(b);}
- }st;
- bool f[N][N];
- signed main(){
- quick
- int n;
- cin>>n;
- st.init(n);
- rep(i,0,n-1){
- int di;
- cin>>di;
- while(di--){
- int x;
- cin>>x;
- f[i][x]=true;
- }
- }
- rep(i,0,n-1){
- rep(j,0,n-1){
- if(f[i][j]){
- st.Union(i,j);
- v[i].eb(j);
- }
- }
- }
- int ans=n*(n-1)/2;
- rep(i,0,n-1){
- rep(j,i+1,n-1){
- if(!st.issame(i,j)) ans--;
- }
- }
- //debug("now",ans);
- rep(i,0,n-1) {
- if(!in[i]) dfs(i);
- }
- ans-=bridges;
- cout<<ans<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement