Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) begin(x), end(x)
- using namespace std;
- using ll = long long;
- map<string, int> mp;
- int rs() {
- string s;
- cin >> s;
- if (mp.count(s)) return mp[s];
- return mp[s] = (int) mp.size();
- }
- const int N = 303;
- enum {
- UNDEF,
- PAR,
- NON
- };
- int g[N][N];
- bool par[N][N];
- bool notPar[N][N];
- void YES() {
- cout << "Yes\n";
- exit(0);
- }
- void NO() {
- cout << "No\n";
- exit(0);
- }
- void solve() {
- int f = rs();
- int t = rs();
- int n;
- cin >> n;
- vector<int> from;
- vector<vector<pair<int, int>>> edges(n);
- map<pair<int, int>, vector<int>> indexes;
- for (int i = 0; i < n; i++) {
- auto &v = edges[i];
- int k;
- cin >> k;
- v.resize(k);
- for (auto&[l, r]: v) {
- l = rs();
- r = rs();
- indexes[{l, r}].push_back(i);
- }
- }
- if (indexes.count({f, t}) == 0) {
- YES();
- }
- vector<bool> used(n);
- vector<int> q = indexes[{f, t}];
- int sz = mp.size();
- auto kek = [&](int x, int y) {
- auto &v = indexes[{x, y}];
- for (int i = 0; i < int(v.size()); i++) {
- if (!used[v[i]]) {
- q.push_back(v[i]);
- used[v[i]] = true;
- }
- }
- v.clear();
- };
- auto relax = [&](vector<pair<int, int>> &e) {
- vector<pair<int, int>> cur, rev;
- for (auto[l, r]: e) {
- cur.emplace_back(r, l);
- rev.emplace_back(l, l);
- }
- for (int i = 0; i < int(cur.size()); i++) {
- auto[v, p] = cur[i];
- for (int to = 0; to < sz; to++) {
- if (g[v][to] == PAR && g[p][to] != PAR) {
- g[p][to] = PAR;
- kek(p, to);
- // p -> to
- cur.emplace_back(p, to);
- }
- }
- }
- for (int i = 0; i < int(rev.size()); i++) {
- auto[p, c] = rev[i];
- for (int to = 0; to < sz; to++) {
- if (g[p][c] == PAR && g[to][c] != PAR) {
- g[to][c] = PAR;
- kek(to, c);
- rev.emplace_back(to, c);
- }
- }
- }
- };
- for (int i = 0; i < int(q.size()); i++) {
- int v = q[i];
- if (used[v]) continue;
- used[v] = true;
- vector<pair<int, int>> e;
- for (auto[x, y]: edges[v]) {
- if (g[x][y] == UNDEF) {
- g[x][y] = PAR;
- e.emplace_back(x, y);
- }
- }
- relax(e);
- }
- for (int i = 0; i < sz; i++) {
- for (int j = 0; j < sz; j++) {
- if (g[i][j] == PAR && g[j][i] == PAR) {
- NO();
- }
- }
- }
- for (int i = 0; i < n; i++) {
- if (!used[i]) {
- for (auto [l, r] : edges[i]) {
- if (g[l][r] == PAR) {
- NO();
- }
- }
- }
- }
- YES();
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement