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;
- bool g[N][N];
- vector<int> indexes[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<vector<pair<int, int>>> edges(n);
- vector<bool> used(n);
- 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[f][t].empty()) {
- YES();
- }
- vector<int> q = indexes[f][t];
- int sz = (int) mp.size();
- auto fill_edges = [&](int x, int y) {
- auto &v = indexes[x][y];
- for (int &i: v) {
- if (!used[i]) {
- q.push_back(i);
- used[i] = true;
- }
- }
- v.clear();
- };
- auto relax = [&](vector<pair<int, int>> &e) {
- vector<pair<int, int>> cur, rev;
- for (auto[fr, to]: e) {
- cur.emplace_back(fr, to);
- rev.emplace_back(fr, to);
- }
- for (int i = 0; i < int(cur.size()); i++) {
- auto[fr, to] = cur[i];
- for (int nxt = 0; nxt < sz; nxt++) {
- if (g[to][nxt] && !g[fr][nxt]) {
- g[fr][nxt] = true;
- fill_edges(fr, nxt);
- cur.emplace_back(fr, nxt);
- }
- }
- }
- for (int i = 0; i < int(rev.size()); i++) {
- auto[fr, to] = rev[i];
- for (int pre = 0; pre < sz; pre++) {
- if (g[pre][fr] && !g[pre][to]) {
- g[pre][to] = true;
- fill_edges(pre, to);
- rev.emplace_back(pre, to);
- }
- }
- }
- };
- for (int i = 0; i < int(q.size()); i++) {
- int v = q[i];
- used[v] = true;
- vector<pair<int, int>> e;
- for (auto[x, y]: edges[v]) {
- if (!g[x][y]) {
- g[x][y] = true;
- fill_edges(x, y);
- 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] && g[j][i] && i != j) {
- NO();
- }
- }
- }
- YES();
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement