Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// author: Mr.Hakimov
- #include <bits/stdc++.h>
- #include <chrono>
- #define fi first
- #define se second
- #define pb push_back
- #define pf push_front
- #define Pb pop_back
- #define Pf pop_front
- #define all(x) (x).begin(), (x).end()
- #define fin(s) freopen(s, "r", stdin);
- #define fout(s) freopen(s, "w", stdout);
- /* Just some advices:
- 1. If I use set/multiset, I will check it - is it correct to use set/multiset in a problem?
- 2. If I can't solve problem, I must write a program, which could help me to understand it much more better)
- ...
- */
- using namespace std;
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize ("unroll-loops")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- typedef long long LL;
- typedef long double LD;
- int TN = 1;
- const int N = 2002;
- vector<vector<int>> edges(N);
- vector<vector<int>> edgesRev(N);
- vector<bool> used(N, false);
- vector<int> topSort;
- vector<int> color(N, -1);
- void dfsFirst(int u) {
- used[u] = true;
- for (int v : edges[u]) {
- if (!used[v]) {
- dfsFirst(v);
- }
- }
- topSort.pb(u);
- }
- void dfsSecond(int u, int clr) {
- color[u] = clr;
- for (int v : edgesRev[u]) {
- if (color[v] == -1) {
- dfsSecond(v, clr);
- }
- }
- }
- void solve() {
- int n;
- int m;
- cin >> n >> m;
- int cnt = 0;
- map<string, int> num;
- map<int, string> numRev;
- for (int i = 0; i < n; ++i) {
- string name;
- cin >> name;
- num[name] = ++cnt;
- numRev[cnt] = name;
- }
- for (int i = 0; i < m; ++i) {
- string a;
- string msr;
- string b;
- cin >> a >> msr >> b;
- int x;
- int y;
- if (a[0] == '+') {
- x = num[a.substr(1, a.size() - 1)];
- } else {
- x = num[a.substr(1, a.size() - 1)] + n;
- }
- if (b[0] == '+') {
- y = num[b.substr(1, b.size() - 1)];
- } else {
- y = num[b.substr(1, b.size() - 1)] + n;
- }
- edges[x].pb(y);
- // cout << x << " " << y << endl;
- // cout << (x > n ? x - n : x + n) << " " << (y > n ? y - n : y + n) << endl;
- edgesRev[x > n ? x - n : x + n].pb(y > n ? y - n : y + n);
- edgesRev[y].pb(x);
- edges[y > n ? y - n : y + n].pb(x > n ? x - n : x + n);
- }
- for (int i = 1; i <= 2 * n; ++i) {
- if (!used[i]) {
- dfsFirst(i);
- }
- }
- int clr = 0;
- for (int i = 2 * n - 1; i >= 0; --i) {
- if (color[topSort[i]] == -1) {
- dfsSecond(topSort[i], ++clr);
- }
- }
- for (int i = 1; i <= n; ++i) {
- if (color[i] == color[i + n]) {
- cout << -1;
- return;
- }
- }
- set<int> answer;
- for (int i = 1; i <= n; ++i) {
- if (color[i] > color[i + n]) {
- answer.insert(i);
- }
- }
- cout << answer.size() << endl;
- for (int el : answer) {
- cout << numRev[el] << endl;
- }
- cout << endl;
- }
- int main() {
- auto start = chrono::steady_clock::now();
- ios_base::sync_with_stdio(0);
- cin.tie(nullptr); cout.tie(nullptr);
- /// =========================================
- /// fin("input.txt"); fout("output.txt");
- /// fin("file.in"); fout("file.out");
- /// cin >> TN;
- /// =========================================
- while (TN--) solve();
- auto finish = chrono::steady_clock::now();
- auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(finish - start);
- cerr << endl << "Time: " << elapsed_ms.count() << " ms\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement