Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. /// author: Mr.Hakimov
  2.  
  3. #include <bits/stdc++.h>
  4. #include <chrono>
  5.  
  6. #define fi first
  7. #define se second
  8. #define pb push_back
  9. #define pf push_front
  10. #define Pb pop_back
  11. #define Pf pop_front
  12. #define all(x) (x).begin(), (x).end()
  13. #define fin(s) freopen(s, "r", stdin);
  14. #define fout(s) freopen(s, "w", stdout);
  15.  
  16. /* Just some advices:
  17. 1. If I use set/multiset, I will check it - is it correct to use set/multiset in a problem?
  18. 2. If I can't solve problem, I must write a program, which could help me to understand it much more better)
  19. ...
  20. */
  21.  
  22. using namespace std;
  23.  
  24. #pragma GCC optimize("Ofast")
  25. #pragma GCC optimize ("unroll-loops")
  26. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  27.  
  28. typedef long long LL;
  29. typedef long double LD;
  30.  
  31. int TN = 1;
  32. const int N = 2002;
  33.  
  34. vector<vector<int>> edges(N);
  35. vector<vector<int>> edgesRev(N);
  36.  
  37. vector<bool> used(N, false);
  38. vector<int> topSort;
  39. vector<int> color(N, -1);
  40.  
  41. void dfsFirst(int u) {
  42.     used[u] = true;
  43.     for (int v : edges[u]) {
  44.         if (!used[v]) {
  45.             dfsFirst(v);
  46.         }
  47.     }
  48.     topSort.pb(u);
  49. }
  50.  
  51. void dfsSecond(int u, int clr) {
  52.     color[u] = clr;
  53.     for (int v : edgesRev[u]) {
  54.         if (color[v] == -1) {
  55.             dfsSecond(v, clr);
  56.         }
  57.     }
  58. }
  59.  
  60. void solve() {
  61.     int n;
  62.     int m;
  63.     cin >> n >> m;
  64.    
  65.     int cnt = 0;
  66.     map<string, int> num;
  67.     map<int, string> numRev;
  68.     for (int i = 0; i < n; ++i) {
  69.         string name;
  70.         cin >> name;
  71.         num[name] = ++cnt;
  72.         numRev[cnt] = name;
  73.     }
  74.    
  75.     for (int i = 0; i < m; ++i) {
  76.         string a;
  77.         string msr;
  78.         string b;
  79.        
  80.         cin >> a >> msr >> b;
  81.        
  82.         int x;
  83.         int y;
  84.         if (a[0] == '+') {
  85.             x = num[a.substr(1, a.size() - 1)];
  86.         } else {
  87.             x = num[a.substr(1, a.size() - 1)] + n;
  88.         }
  89.        
  90.         if (b[0] == '+') {
  91.             y = num[b.substr(1, b.size() - 1)];
  92.         } else {
  93.             y = num[b.substr(1, b.size() - 1)] + n;
  94.         }
  95.        
  96.         edges[x].pb(y);
  97.         // cout << x << " " << y << endl;
  98.         // cout << (x > n ? x - n : x + n) << " " << (y > n ? y - n : y + n) << endl;
  99.         edgesRev[x > n ? x - n : x + n].pb(y > n ? y - n : y + n);
  100.         edgesRev[y].pb(x);
  101.         edges[y > n ? y - n : y + n].pb(x > n ? x - n : x + n);
  102.     }
  103.    
  104.     for (int i = 1; i <= 2 * n; ++i) {
  105.         if (!used[i]) {
  106.             dfsFirst(i);
  107.         }
  108.     }
  109.    
  110.     int clr = 0;
  111.     for (int i = 2 * n - 1; i >= 0; --i) {
  112.         if (color[topSort[i]] == -1) {
  113.             dfsSecond(topSort[i], ++clr);
  114.         }
  115.     }
  116.    
  117.     for (int i = 1; i <= n; ++i) {
  118.         if (color[i] == color[i + n]) {
  119.             cout << -1;
  120.             return;
  121.         }
  122.     }
  123.    
  124.    
  125.     set<int> answer;
  126.     for (int i = 1; i <= n; ++i) {
  127.         if (color[i] > color[i + n]) {
  128.             answer.insert(i);
  129.         }
  130.        
  131.     }
  132.    
  133.     cout << answer.size() << endl;
  134.     for (int el : answer) {
  135.         cout << numRev[el] << endl;
  136.     }
  137.     cout << endl;
  138. }
  139.  
  140. int main() {
  141.     auto start = chrono::steady_clock::now();
  142.     ios_base::sync_with_stdio(0);
  143.     cin.tie(nullptr); cout.tie(nullptr);
  144.     /// =========================================
  145.     /// fin("input.txt"); fout("output.txt");
  146.     /// fin("file.in"); fout("file.out");
  147.     /// cin >> TN;
  148.     /// =========================================
  149.     while (TN--) solve();
  150.     auto finish = chrono::steady_clock::now();
  151.     auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(finish - start);
  152.     cerr << endl << "Time: " << elapsed_ms.count() << " ms\n";
  153.     return 0;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement