Advertisement
BaoJIaoPisu

Untitled

Dec 24th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define pb push_back
  5. #define pf push_front
  6. #define popb pop_back
  7. #define popf pop_front
  8. #define ins insert
  9. #define pq priority_queue
  10. #define minele min_element
  11. #define maxele max_element
  12. #define mp make_pair
  13. #define lb lower_bound //first pos >= val
  14. #define ub upper_bound // first pos > val
  15. #define cnt_bit __builtin_popcount
  16. #define all(x) x.begin(), x.end()
  17. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  18. //#pragma GCC optimize("Ofast")
  19. //#pragma GCC target("avx,avx2,fma")
  20. using namespace std;
  21.  
  22. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  23.  
  24. typedef long long ll;
  25. typedef unsigned long long ull;
  26. typedef pair<ll, ll> pll;
  27. typedef pair<int, int> pii;
  28.  
  29. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  30. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  31. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  32.  
  33. const int INF = 1e9;
  34. const ll oo = 1e18;
  35.  
  36. template<class X, class Y>
  37.     bool minimize(X &x, const Y &y) {
  38.         if (x > y)
  39.         {
  40.             x = y;
  41.             return true;
  42.         }
  43.         return false;
  44.     }
  45. template<class X, class Y>
  46.     bool maximize(X &x, const Y &y) {
  47.         if (x < y)
  48.         {
  49.             x = y;
  50.             return true;
  51.         }
  52.         return false;
  53.     }
  54.  
  55. /* Author : Le Ngoc Bao Anh, 11A5, LQD High School for Gifted Student */
  56.  
  57. map<string, int> id;
  58. const int N = 1e5 + 10;
  59. string name[N];
  60. bool spec[N];
  61. int dp[N];
  62. vector<int> g[N];
  63.  
  64. void solve() {
  65.     int n, k, s, m; cin >> n >> k >> s >> m;
  66.     int cnt = 0;
  67.     vector<string> ans;
  68.     for(int i = 1; i <= s; i++) {
  69.         string s; cin >> s;
  70.         if(!id[s]) {
  71.             id[s] = ++cnt;
  72.             name[cnt] = s;
  73.             ans.pb(s);
  74.         }
  75.         spec[id[s]] = true;
  76.     }
  77.  
  78.     for(int i = 1; i <= m; i++) {
  79.         string x, y;
  80.         cin >> x >> y;
  81.         if(!id[x]) {
  82.             id[x] = ++cnt;
  83.             name[cnt] = x;
  84.         }
  85.         if(!id[y]) {
  86.             id[y] = ++cnt;
  87.             name[cnt] = y;
  88.         }
  89.         int u = id[x], v = id[y];
  90.         g[u].pb(v); g[v].pb(u);
  91.     }
  92.  
  93.     for(int i = 1; i <= n; i++) {
  94.         sort(all(g[i]));
  95.         g[i].resize(unique(all(g[i])) - g[i].begin());
  96.     }
  97.  
  98.     queue<int> q;
  99.     for(int i = 1; i <= n; i++) if(spec[i]) q.push(i);
  100.     while(!q.empty()) {
  101.         int u = q.front(); q.pop();
  102.         for(auto v : g[u]) {
  103.             if(spec[v]) continue;
  104.             ++dp[v];
  105.             if(dp[v] >= k) {
  106.                 spec[v] = true;
  107.                 ans.pb(name[v]);
  108.                 q.push(v);
  109.             }
  110.         }
  111.     }
  112.  
  113.     sort(all(ans));
  114.     cout << ans.size() << '\n';
  115.     for(int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
  116. }
  117.  
  118. int main()
  119. {
  120.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  121.     #ifndef ONLINE_JUDGE
  122.     freopen("input.txt", "r", stdin);
  123.     freopen("output.txt", "w", stdout);
  124.     #else
  125.     //online
  126.     #endif
  127.  
  128.     int tc = 1, ddd = 0;
  129.     // cin >> tc;
  130.     while(tc--) {
  131.         //ddd++;
  132.         //cout << "Case #" << ddd << ": ";
  133.         solve();
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement