Advertisement
yuhung94

2227. E. 共同朋友

Dec 14th, 2022
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #pragma GCC optimize("Ofast,no-stack-protector")
  2. #include<bits/stdc++.h>
  3. #define int long long
  4. #define quick ios::sync_with_stdio(0);cin.tie(0);
  5. #define rep(x,a,b) for(int x=a;x<=b;x++)
  6. #define repd(x,a,b) for(int x=a;x>=b;x--)
  7. #define F first
  8. #define S second
  9. #define all(x) x.begin(),x.end()
  10. #define mp make_pair
  11. #define eb emplace_back
  12. #define sz(x) (int)(x.size())
  13. #define lowbit(x) (x&-x)
  14. using namespace std;
  15. typedef pair<int,int> pii;
  16. typedef complex<int> P;
  17. #define X real()
  18. #define Y imag()
  19. void debug(){
  20.     cout<<"\n";
  21. }
  22. template<class T,class ...U>
  23. void debug(T a,U ...b){
  24.     cout<<a<<" ",debug(b...);
  25. }
  26. const int N=2500+7;
  27. const int INF=1e18;
  28. vector<int> v[N];
  29. int low[N];
  30. int in[N];
  31. int t=1;
  32. int bridges=0;
  33. //set<pii> Bridges;
  34. void dfs(int x,int p=-1){
  35.     low[x]=in[x]=t++;
  36.     for(int i:v[x]){
  37.         if(i==p) continue;
  38.         if(in[i]) low[x]=min(low[x],in[i]);
  39.         else{
  40.             dfs(i,x);
  41.             if(low[i]>in[x]){//(x,i) is bridge
  42.                 bridges++;
  43.             }
  44.             low[x]=min(low[x],low[i]);
  45.         }
  46.     }
  47. }
  48. struct Disjoint{
  49.     int p[N];
  50.     int s[N];
  51.     void init(int n){
  52.         rep(i,0,n) s[i]=1,p[i]=i;
  53.     }
  54.     int fp(int x){
  55.         if(x!=p[x]) p[x]=fp(p[x]);
  56.         return p[x];
  57.     }
  58.     void Union(int a,int b){
  59.         a=fp(a);
  60.         b=fp(b);
  61.         if(a==b) return;
  62.         if(s[a]<s[b]) swap(a,b);
  63.         s[a]+=s[b];
  64.         p[b]=a;
  65.     }
  66.     bool issame(int a,int b) {return fp(a)==fp(b);}
  67. }st;
  68. bool f[N][N];
  69. signed main(){
  70.     quick
  71.     int n;
  72.     cin>>n;
  73.     st.init(n);
  74.     rep(i,0,n-1){
  75.         int di;
  76.         cin>>di;
  77.         while(di--){
  78.             int x;
  79.             cin>>x;
  80.             f[i][x]=true;
  81.         }
  82.     }
  83.     rep(i,0,n-1){
  84.         rep(j,0,n-1){
  85.             if(f[i][j]){
  86.                 st.Union(i,j);
  87.                 v[i].eb(j);
  88.             }
  89.         }
  90.     }
  91.     int ans=n*(n-1)/2;
  92.     rep(i,0,n-1){
  93.         rep(j,i+1,n-1){
  94.  
  95.             if(!st.issame(i,j)) ans--;
  96.         }
  97.     }
  98.     //debug("now",ans);
  99.     rep(i,0,n-1) {
  100.         if(!in[i]) dfs(i);
  101.     }
  102.     ans-=bridges;
  103.     cout<<ans<<"\n";
  104.  
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement