MinhNGUYEN2k4

Untitled

Aug 6th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define pb(x) push_back(x)
  4. using namespace std;
  5. const int N = 100005;
  6. typedef pair<int,int> ii;
  7.  
  8. int n,m,par[N],cnt=0,num[N],low[N];
  9. vector<int> a[N],aa,bb;
  10. int ans=0;
  11. int k,l;
  12. int c[3][N];
  13.  
  14. void dfs(int u)
  15. {
  16.     num[u] = low[u] = ++cnt;
  17.     for(int &v : a[u])
  18.     {
  19.         if (par[u] == v) continue;
  20.         if (par[v]) low[u]=min(low[u],num[v]);
  21.         else
  22.         {
  23.             par[v] = u;
  24.             dfs(v);
  25.             c[1][u] += c[1][v];
  26.             c[2][u] += c[2][v];
  27.             low[u]=min(low[u],low[v]);
  28.             if (low[v] == num[v]) {
  29.                 if (c[1][v] == 0 || c[1][v] == k)
  30.                 {
  31.                     ans++;
  32.                 }
  33.                 else if (c[2][v] == 0 || c[2][v] == l)
  34.                 {
  35.                     ans++;
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }
  41.  
  42. signed main()
  43. {
  44.     ios_base::sync_with_stdio(false);
  45.     cin.tie(0);cout.tie(0);
  46.     freopen("net.inp","r",stdin);
  47.     freopen("net.out","w",stdout);
  48.     cin >> n >> m >> k >> l;
  49.     for(int i=1; i<=k; ++i)
  50.     {
  51.         int x;
  52.         cin >> x;
  53.         c[1][x]=1;
  54.     }
  55.     for(int i=1; i<=l; ++i)
  56.     {
  57.         int x;
  58.         cin >> x;
  59.         c[2][x]=1;
  60.     }
  61.     for(int i=1; i<=m; ++i)
  62.     {
  63.         int u, v;
  64.         cin >> u >> v;
  65.         a[u].pb(v);
  66.         a[v].pb(u);
  67.     }
  68.     for(int i=1; i<=n; ++i)
  69.     {
  70.         if (!par[i])
  71.         {
  72.             par[i] = -1;
  73.             dfs(i);
  74.         }
  75.     }
  76.     cout << ans << endl;
  77.     return 0;
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment