Raslboyy

1932v2

Nov 27th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <algorithm>
  5. #include <string>
  6. #include <math.h>
  7. #include <climits>
  8.  
  9. //#include <chrono>
  10. //#include <fstream>
  11. //#include <bits/stdc++.h>
  12.  
  13. #define mp make_pair
  14. #define pb push_back
  15. #define eb emplace_back
  16. #define x first
  17. #define y second
  18. #define sz(x) (int)x.size()
  19. #define all(x) begin(x), end(x)
  20. #define rall(x) rbegin(x), rend(x)
  21. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  22. #define RFOR(i,b,a) for (int i = (b) - 1; i >= (a); i--)
  23.  
  24. using namespace std;
  25.  
  26. typedef unsigned long long ull;
  27. typedef long long ll;
  28. typedef long double ld;
  29. typedef pair<int, int> pi;
  30. typedef pair<ll, ll> pl;
  31. typedef vector<int> veci;
  32. typedef vector<bool> vecb;
  33. typedef vector<vector<int>> vvi;
  34. typedef vector<vector<bool>> vvb;
  35.  
  36. const int INF_I = 1e9;
  37. const ll INF_LL = 1e18;
  38.  
  39. int m, k, n;
  40. vvb foe;
  41. vecb used;
  42. veci p;
  43.  
  44. bool dfs(int v) {
  45.     if (used[v])
  46.         return false;
  47.     used[v] = true;
  48.     for (int i = 0; i < k; i++)
  49.         if (!foe[v][i] && (p[i] == -1 || dfs(p[i]))) {
  50.             p[i] = v;
  51.             return true;
  52.         }
  53.     return false;
  54. }
  55.  
  56. int main() {
  57.     /*ifstream cin("in.txt");
  58.     ofstream cout("out.txt");
  59.     auto start_time = chrono::high_resolution_clock::now();*/
  60.  
  61.     ios::sync_with_stdio(false);
  62.     cin.tie(0);
  63.     cout.tie(0);
  64.  
  65.     int t, q;
  66.     cin >> m >> k >> n >> t;
  67.     foe.assign(m, vecb(k));
  68.     p.assign(k, -1);
  69.     for (int i = 0; i < t; i++) {
  70.         int a, b;
  71.         cin >> a >> b;
  72.         a--; b -= (m + 1);
  73.         foe[a][b] = 1;
  74.     }
  75.     cin >> q;
  76.    
  77.     vecb not_alone(m + k);
  78.    
  79.     for (int i = 0; i < q; i++) {
  80.         int a; cin >> a;
  81.         not_alone[--a] = true;
  82.     }
  83.  
  84.     vecb used1(m);
  85.     for (int i = 0; i < m; i++)
  86.         for (int j = 0; j < k; j++)
  87.             if (p[j] == -1 && !foe[i][j] && (not_alone[i] || not_alone[m + j])) {
  88.                 p[j] = i;
  89.                 used1[i] = true;
  90.                 break;
  91.             }
  92.  
  93.    
  94.     for (int i = 0; i < m; i++) {
  95.         if (used1[i]) continue;
  96.         used.assign(m, 0);
  97.         int a = (int)dfs(i);
  98.     }
  99.  
  100.    
  101.     list<pi> res;
  102.     int cnt_n = 0;
  103.     for (int i = 0; i < m + k; i++) {
  104.         if (i >= m && p[i - m] != -1)
  105.             cnt_n++;
  106.         if (not_alone[i]) {
  107.             if (i >= m) {
  108.                 if (p[i - m] == -1) {
  109.                     cout << "NO\n";
  110.                     return 0;
  111.                 }
  112.                 res.eb(p[i - m] + 1, i + 1);
  113.                 p[i - m] = -1;
  114.             }
  115.             else {
  116.                 int in = find(all(p), i) - p.begin();
  117.                 if (in == k) {
  118.                     cout << "NO\n";
  119.                     return 0;
  120.                 }
  121.                 res.eb(p[in] + 1, in + 1 + m);
  122.                 p[in] = -1;
  123.             }
  124.         }
  125.        
  126.     }
  127.     if (cnt_n < n) {
  128.         cout << "NO\n";
  129.         return 0;
  130.     }
  131.  
  132.     cout << "YES\n";
  133.     for (pi i : res) {
  134.         cout << i.x << " " << i.y << "\n";
  135.         n--;
  136.         if (!n)
  137.             break;
  138.     }
  139.     for (int i = 0; i < k && n; i++) {
  140.         if (p[i] != -1) {
  141.             n--;
  142.             cout << p[i] + 1 << " " << m + i + 1 << "\n";
  143.         }
  144.     }
  145.            
  146.    
  147.    
  148.     /*auto end_time = chrono::high_resolution_clock::now();
  149.     chrono::duration<double> duration = end_time - start_time;
  150.     cout << duration.count() << "\n";
  151.     cin.close();
  152.     cout.close();*/
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment