Problem:
Problem F
Three Kingdoms of Bourdelot
You are an archaeologist at the Institute for Cryptic Pedigree Charts, specialized in the study of the ancient Three Kingdoms of Bourdelot. One day, you found a number of documents on the dynasties of the Three Kingdoms of Bourdelot during excavation of an ancient ruin. Since the early days of the Kingdoms of Bourdelot are veiled in mystery and even the names of many kings and queens are lost in history, the documents are expected to reveal the blood relationship of the ancient dynasties.
The documents have lines, each of which consists of a pair of names, presumably of ancient royal family members. An example of a document with two lines follows.
Alice Bob
Bob Clare
Lines should have been complete sentences, but the documents are heavily damaged and therefore you can read only two names per line. At first, you thought that all of those lines described the true ancestor relations, but you found that would lead to contradiction. Later, you found that some documents should be interpreted negatively in order to understand the ancestor relations without contradiction. Formally, a document should be interpreted either as a
positive
document or as a
negative
document.
In a positive document, the person on the left in each line is an ancestor of the person on the right in the line. If the document above is a positive document, it should be interpreted as "Alice is an ancestor of Bob, and Bob is an ancestor of Clare".
In a negative document, the person on the left in each line is NOT an ancestor of the person on the right in the line. If the document above is a negative document, it should be interpreted as "Alice is NOT an ancestor of Bob, and Bob is NOT an ancestor of Clare".
A single document can never be a mixture of positive and negative parts. The document above can never read "Alice is an ancestor of Bob, and Bob is NOT an ancestor of Clare".
You also found that there can be ancestor-descendant pairs that didn't directly appear in the documents but can be inferred by the following rule: For any persons $x$, $y$ and $z$, if $x$ is an ancestor of $y$ and $y$ is an ancestor of $z$, then $x$ is an ancestor of $z$. For example, if the document above is a positive document, then the ancestor-descendant pair of "Alice Clare" is inferred.
You are interested in the ancestry relationship of two royal family members. Unfortunately, the interpretation of the documents, that is, which are positive and which are negative, is unknown. Given a set of documents and two distinct names $p$ and $q$, your task is to find an interpretation of the documents that does not contradict with the hypothesis that $p$ is an ancestor of $q$. An interpretation of the documents contradicts with the hypothesis if and only if there exist persons $x$ and $y$ such that we can infer from the interpretation of the documents and the hypothesis that
$x$ is an ancestor of $y$ and $y$ is an ancestor of $x$, or
$x$ is an ancestor of $y$ and $x$ is not an
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long LL;
const int NAME=300+10;
const int MAXN=100000+10;
const int MAXS=1010;
int n,m,cnt,a[NAME][NAME],vis[MAXS];
struct node{
    int from,to;
};
vector<node> S[MAXS];
vector<int> scr[NAME][NAME];
map<string,int> ID;
bool dfs(int,int);

bool Set(int from,int to){
    if(a[to][from])return false;
    a[from][to]=1;
    for(int i=1;i<=cnt;++i){
        if(a[i][from]){
            if(a[to][i])return false;
            a[i][to]=1;
            if(!dfs(i,to))return false;
        }
        if(a[to][i]){
            if(a[i][from])return false;
            a[from][i]=1;
            if(!dfs(from,i))return false;
        }
    }
    return true;
}
bool dfs(int from,int to){
    vector<int> &G=scr[from][to];
    for(int i=0;i<G.size();++i){
        int u=G[i];
        if(!vis[u]){
            vis[u]=1;
            for(int j=0;j<S[u].size();++j){
                node e=S[u][j];
                if(!Set(e.from,e.to)||!dfs(e.from,e.to))return false;
            }
        }
    }
    return true;
}
bool solve(){
    cnt=0;
    string u,v;
    cin>>u>>v;
    ID[u]=++cnt;
    ID[v]=++cnt;
    a[1][2]=1;
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>m;
        for(int j=0;j<m;++j){
            cin>>u>>v;
            if(!ID.count(u))ID[u]=++cnt;
            if(!ID.count(v))ID[v]=++cnt;
            int uu=ID[u],vv=ID[v];
            S[i].push_back(node{uu,vv});
            scr[uu][vv].push_back(i);
        }
    }
    if(dfs(1,2)){
        for(int i=1;i<=cnt;++i)
            for(int j=1;j<=cnt;++j)if(a[i][j]==1&&a[j][i]==1)return 0;
        return 1;
    }
    else return 0;
}
int main(){
    if(solve())printf("Yes\n");
    else printf("No\n");
    return 0;
}
accuracy: 1
time_ms: 330
memory_kb: 9124
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
map<string,int> ID;
const int MAXN=1005;
const int MAXP=305;
const int MAXM=1000010;
int cnt,n,m,viss[MAXN],vis[MAXP][MAXP],st[MAXM<<1];
struct node{
    int from,to;
}nodes;
vector<node> S[MAXN];
vector<int> contact[MAXP][MAXP];
bool solve(){
    cnt=0;
    string u,v;
    cin>>u>>v;
    if(!ID.count(u))ID[u]=++cnt;
    if(!ID.count(v))ID[v]=++cnt;
    int uu=ID[u],vv=ID[v];
    vis[uu][vv]=1;
    int stu=uu,stv=vv;
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>m;
        for(int j=0;j<m;++j){
            cin>>u>>v;
            if(!ID.count(u))ID[u]=++cnt;
            if(!ID.count(v))ID[v]=++cnt;
            int uu=ID[u],vv=ID[v];
            if(uu==vv)while(1);
            S[i].push_back(node{uu,vv});
            contact[uu][vv].push_back(i);
        }
    }
    int s=1,rear=1;
    for(int i=0;i<contact[stu][stv].size();++i){
        int u=contact[stu][stv][i];
        if(!viss[u]){
            viss[u]=1;
            st[rear++]=u;
        }
    }
    while(s<rear){
        //cerr<<s<<endl;
        int u=st[s++];
        for(int i=0;i<S[u].size();++i){
            node x=S[u][i];
            int from=x.from,to=x.to;
            if(!vis[from][to]){
                //cerr<<from<<" "<<to<<" "<<vis[to][from]<<endl;
                if(vis[to][from])return 0;
                vis[from][to]=1;
                for(int j=1;j<=cnt;++j){
                    if(vis[j][from]){
                        if(vis[j][to])continue;
                        vis[j][to]=1;
                    }
                    if(vis[to][j])return 0;
                    for(int jj=0;jj<contact[j][to].size();++jj){
                        int y=contact[j][to][jj];
                        if(!viss[y]){
                            viss[y]=1;
                            st[rear++]=y;
                        }
                    }
                }
                for(int j=0;j<contact[from][to].size();++j){
                    int y=contact[from][to][j];
                    if(!viss[y]){
                        viss[y]=1;
                        st[rear++]=y;
                    }
                }
            }
        }
    }
    return 1;
}
int main(){
    ID.clear();
    ios::sync_with_stdio(0);
    if(solve())printf("Yes\n");
    else printf("No\n");
    return 0;
}
accuracy: 0.4576271186440678
time_ms: 40
memory_kb: 12716
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
map<string,int> ID;
const int MAXN=1005;
const int MAXP=305;
const int MAXM=1000010;
int cnt,n,m,viss[MAXN],vis[MAXP][MAXP],st[MAXM<<1];
vector<int> From[MAXP];
struct node{
    int from,to;
}nodes;
vector<node> S[MAXN];
vector<int> contact[MAXP][MAXP];
bool solve(){
    cnt=0;
    string u,v;
    cin>>u>>v;
    if(!ID.count(u))ID[u]=++cnt;
    if(!ID.count(v))ID[v]=++cnt;
    int uu=ID[u],vv=ID[v];
    vis[uu][vv]=1;
    From[vv].push_back(uu);
    int stu=uu,stv=vv;
    cin>>n;
    for(int i=1;i<=n;++i){
        cin>>m;
        for(int j=0;j<m;++j){
            cin>>u>>v;
            if(!ID.count(u))ID[u]=++cnt;
            if(!ID.count(v))ID[v]=++cnt;
            int uu=ID[u],vv=ID[v];
            S[i].push_back(node{uu,vv});
            contact[uu][vv].push_back(i);
        }
    }
    int s=1,rear=1;
    for(int i=0;i<contact[stu][stv].size();++i){
        int u=contact[stu][stv][i];
        if(!viss[u]){
            viss[u]=1;
            st[rear++]=u;
        }
    }
    while(s<rear){
        //cerr<<s<<endl;
        int u=st[s++];
        for(int i=0;i<S[u].size();++i){
            node x=S[u][i];
            int from=x.from,to=x.to;
            if(!vis[from][to]){
                //cerr<<from<<" "<<to<<" "<<vis[to][from]<<endl;
                if(vis[to][from])return 0;
                vis[from][to]=1;
                for(int j=0;j<From[from].size();++j){
                    int fj=From[from][j];
                    if(vis[to][fj])return 0;
                    if(vis[fj][to])continue;
                    vis[fj][to]=1;
                    //cerr<<fj<<" "<<to<<" "<<vis[fj][to]<<endl;
                    for(int jj=0;jj<contact[fj][to].size();++jj){
                        int y=contact[fj][to][jj];
                        if(!viss[y]){
                            viss[y]=1;
                            st[rear++]=y;
                        }
                    }
                }
                From[to].push_back(from);
                for(int j=0;j<contact[from][to].size();++j){
                    int y=contact[from][to][j];
                    if(!viss[y]){
                        viss[y]=1;
                        st[rear++]=y;
                    }
                }
            }
        }
    }
    return 1;
}
int main(){
    ID.clear();
    ios::sync_with_stdio(0);
    if(solve())printf("Yes\n");
    else printf("No\n");
    return 0;
}
accuracy: 0.13559322033898305
time_ms: 40
memory_kb: 12500
final_rank: 14

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int Cur = 0;
map<string,int> mps;

int get_string() {
    string s;
    cin >> s;
    if (mps.count(s)) return mps[s];
    mps[s] = Cur;
    Cur++;
    return Cur - 1;
}

const int MAXS = 300;

int main() {
    int SX, SY;
    SX = get_string();
    SY = get_string();
    int N;
    cin >> N;
    vector A(MAXS, vector(MAXS, vector<int>()));
    vector B(N, vector<pair<int,int>>());
    rep(i,0,N) {
        int M;
        cin >> M;
        while(M--) {
            int X, Y;
            X = get_string();
            Y = get_string();
            A[X][Y].push_back(i);
            B[i].push_back({X,Y});
        }
    }
    vector FA(MAXS, vector<bool>(MAXS, false));
    vector<bool> FB(N, false);
    queue<pair<int,int>> Q;
    FA[SX][SY] = true;
    Q.push({SX,SY});
    while(!Q.empty()) {
        auto [X, Y] = Q.front();
        Q.pop();
        if (FA[Y][X]) {
            cout << "No" << endl;
            return 0;
        }
        for (int P : A[X][Y]) {
            if (FB[P]) continue;
            FB[P] = true;
            for (pair<int, int> NP : B[P]) {
                auto [NX, NY] = NP;
                if (FA[NX][NY]) continue;
                FA[NX][NY] = true;
                Q.push({NX,NY});
            }
        }
        rep(Z,0,MAXS) {
            if (FA[Y][Z] && !FA[X][Z]) {
                FA[X][Z] = true;
                Q.push({X,Z});
            }
            if (FA[Z][X] && !FA[Z][Y]) {
                FA[Z][Y] = true;
                Q.push({Z,Y});
            }
        }
    }
    cout << "Yes" << endl;
}
accuracy: 1
time_ms: 80
memory_kb: 8724
final_rank: 1

code_snippet: #include <bits/stdc++.h>

#define rep(i,s,n) for(int i = int(s); i < int(n); i++)
#define rrep(i,s,n) for(int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<class T>
bool chmin(T &a, T b) {
    if(a <= b) return false;
    a = b;
    return true;
}

template<class T>
bool chmax(T &a, T b) {
    if(a >= b) return false;
    a = b;
    return true;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::string p, q;
    std::cin >> p >> q;
    std::map<std::string, int> map;
    int cnt = 2;
    int s = 0, t = 1;
    map[p] = 0;
    map[q] = 1;
    auto get_idx = [&](std::string &a) -> int {
        if(map.find(a) == map.end()) {
            map[a] = cnt++;
        }
        return map[a];
    };
    int n;
    std::cin >> n;
    std::vector edges(n, std::vector<std::pair<int,int>>());
    int sz = 310;
    std::vector table(sz, std::vector(sz, std::vector<int>()));
    rep(i,0,n) {
        int m;
        std::cin >> m;
        rep(j,0,m) {
            std::string x, y;
            std::cin >> x >> y;
            int u = get_idx(x);
            int v = get_idx(y);
            edges[i].emplace_back(u, v);
            table[u][v].emplace_back(i);
        }
    }
    std::vector<int> seen(n, -1);
    std::vector in(sz, std::vector<int>(sz, -1));
    std::queue<std::pair<int,int>> que;
    std::vector g(sz, std::vector<int>(sz, -1));
    auto add_edge = [&](int u, int v) -> void {
        if(g[u][v] > 0) return;
        g[u][v] = 1;
        if(in[u][v] == -1) {
            que.push({u, v});
            in[u][v] = 1;
        }
        for(auto i: table[u][v]) {
            if(seen[i] == -1) {
                seen[i] = 1;
                for(auto [a, b]: edges[i]) {
                    if(in[a][b] == -1) {
                        que.push({a, b});
                        in[a][b] = 1;
                    }
                }
            }
        }
    };
    que.push({s, t});
    in[s][t] = 1;
    while(!que.empty()) {
        auto [u, v] = que.front();
        que.pop();
        add_edge(u, v);
        rep(i,0,sz) {
            if(g[v][i] > 0) {
                add_edge(u, i);
            }
            if(g[i][u] > 0) {
                add_edge(i, v);
            }
        }
    }
    rep(i,0,sz) {
        rep(j,0,sz) {
            if(i == j) continue;
            if(g[i][j] > 0 && g[j][i] > 0) {
                std::cout << "No\n";
                return 0;
            }
        }
    }
    std::cout << "Yes\n";
}
accuracy: 1
time_ms: 110
memory_kb: 9976
final_rank: 3

code_snippet: #include <bits/stdc++.h>

#define rep(i,s,n) for(int i = int(s); i < int(n); i++)
#define rrep(i,s,n) for(int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template<class T>
bool chmin(T &a, T b) {
    if(a <= b) return false;
    a = b;
    return true;
}

template<class T>
bool chmax(T &a, T b) {
    if(a >= b) return false;
    a = b;
    return true;
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::string p, q;
    std::cin >> p >> q;
    std::map<std::string, int> map;
    int cnt = 2;
    int s = 0, t = 1;
    map[p] = 0;
    map[q] = 1;
    auto get_idx = [&](std::string &a) -> int {
        if(map.find(a) == map.end()) {
            map[a] = cnt++;
        }
        return map[a];
    };
    int n;
    std::cin >> n;
    std::vector edges(n, std::vector<std::pair<int,int>>());
    int sz = 310;
    std::vector table(sz, std::vector(sz, std::vector<int>()));
    rep(i,0,n) {
        int m;
        std::cin >> m;
        rep(j,0,m) {
            std::string x, y;
            std::cin >> x >> y;
            int u = get_idx(x);
            int v = get_idx(y);
            edges[i].emplace_back(u, v);
            table[u][v].emplace_back(i);
        }
    }
    std::vector<int> seen(n, -1);
    std::vector in(sz, std::vector<int>(sz, -1));
    std::queue<std::pair<int,int>> que;
    std::vector g(sz, std::vector<int>(sz, -1));
    auto add_edge = [&](int u, int v) -> void {
        if(g[u][v] > 0) return;
        g[u][v] = 1;
        for(auto i: table[u][v]) {
            if(seen[i] == -1) {
                seen[i] = 1;
                for(auto [a, b]: edges[i]) {
                    que.push({a, b});
                }
            }
        }
    };
    que.push({s, t});
    while(!que.empty()) {
        auto [u, v] = que.front();
        que.pop();
        if(in[u][v] > 0) continue;
        in[u][v] = 1;
        add_edge(u, v);
        rep(i,0,sz) {
            if(g[v][i] > 0) {
                add_edge(u, i);
            }
            if(g[i][u] > 0) {
                add_edge(i, v);
            }
        }
    }
    rep(i,0,sz) {
        rep(j,0,sz) {
            /*
            if(g[i][j] > 0) {
                std::cerr << i << " " << j << '\n';
            }
            */
            if(i == j) continue;
            rep(k,0,sz) {
                if(i == k || j == k) continue;
                if(g[i][j] > 0 && g[j][k] > 0 && g[k][i] > 0) {
                    std::cout << "No\n";
                    return 0;
                }
            }
        }
    }
    std::cout << "Yes\n";
}
accuracy: 0.7457627118644068
time_ms: 90
memory_kb: 9960
final_rank: 10

code_snippet: #line 1 "a.cpp"
// cSpell:disable
/*	author:  Kite_kuma
	created: 2022.11.18 00:30:55 */

#line 2 "SPJ-Library/template/kuma.hpp"

#line 2 "SPJ-Library/template/basic_func.hpp"

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool flag = true) { std::cout << (flag ? "Yes" : "No") << '\n'; }
void YES(bool flag = true) { std::cout << (flag ? "YES" : "NO") << '\n'; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(const T &x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(const Head &H, const Tail &... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T value) {
	for(auto &a : v) a += value;
	return;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

// ceil(a / b);
template <class T>
T dup(T a, T b) {
	assert(b);
	if(b < 0) {
		a *= -1;
		b *= -1;
	}
	return least_upper_multiple(a, b) / b;
}

long long pow_ll(long long a, long long n) {
	assert(n >= 0LL);
	if(n == 0) return 1LL;
	if(a == 0) return 0LL;
	if(a == 1) return 1LL;
	if(a == -1) return (n & 1LL) ? -1LL : 1LL;
	long long res = 1;
	while(n > 1LL) {
		if(n & 1LL) res *= a;
		a *= a;
		n >>= 1;
	}
	return res * a;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, const long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return (int)std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return (int)std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(const std::vector<T> &a) {
	std::vector<T> vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(const auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}
#line 1 "SPJ-Library/template/header.hpp"
#include <bits/stdc++.h>
#line 2 "SPJ-Library/template/io.hpp"

#line 4 "SPJ-Library/template/io.hpp"

#line 8 "SPJ-Library/template/debug.hpp"

#line 3 "SPJ-Library/template/constants.hpp"

constexpr int inf = 1000'000'000;
constexpr long long INF = 1'000'000'000'000'000'000LL;
constexpr int mod_1000000007 = 1000000007;
constexpr int mod_998244353 = 998244353;
const long double pi = acosl(-1.);
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
#line 10 "SPJ-Library/template/debug.hpp"

namespace viewer {

template <typename T, typename U>
void view(const std::pair<T, U> &p);

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p);

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p);

void view(const long long &e);

void view(const int &e);

template <typename T>
void view(const T &e);

template <typename T>
void view(const std::set<T> &s);

template <typename T>
void view(const std::unordered_set<T> &s);

template <typename T>
void view(const std::multiset<T> &s);

template <typename T>
void view(const std::unordered_multiset<T> &s);

template <typename T>
void view(const std::vector<T> &v);

template <typename T, std::size_t ary_size>
void view(const std::array<T, ary_size> &v);

template <typename T>
void view(const std::vector<std::vector<T>> &vv);

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v);

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v);

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v);

template <typename map_type>
void view_map_container(const map_type &m);

template <typename T, typename U>
void view(const std::map<T, U> &m);

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m);

template <typename container_type>
void view_container(const container_type &c, bool vertically = false) {
	typename container_type::const_iterator begin = c.begin();
	const typename container_type::const_iterator end = c.end();
	if(vertically) {
		std::cerr << "{\n";
		while(begin != end) {
			std::cerr << '\t';
			view(*(begin++));
			if(begin != end) std::cerr << ',';
			std::cerr << '\n';
		}
		std::cerr << '}';
		return;
	}
	std::cerr << "{ ";
	while(begin != end) {
		view(*(begin++));
		if(begin != end) std::cerr << ',';
		std::cerr << ' ';
	}
	std::cerr << '}';
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << '(';
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ')';
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << '(';
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ')';
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << '(';
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ')';
}

void view(const long long &e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int &e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T &e) {
	std::cerr << e;
}

template <typename T>
void view(const std::set<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::multiset<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::unordered_multiset<T> &s) {
	view_container(s);
}

template <typename T>
void view(const std::vector<T> &v) {
	view_container(v);
}

template <typename T, std::size_t ary_size>
void view(const std::array<T, ary_size> &v) {
	view_container(v);
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	view_container(vv, true);
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	view_container(v, true);
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	view_container(v, true);
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	view_container(v, true);
}

template <typename map_type>
void view_map_container(const map_type &m) {
	std::cerr << "{\n";
	for(typename map_type::const_iterator it = m.begin(); it != m.end(); it++) {
		std::cerr << "\t[";
		view(it->first);
		std::cerr << "] : ";
		view(it->second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	view_map_container(m);
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	view_map_container(m);
}

}  // namespace viewer

// when compiling : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename T>
void debug_out(const T &x) {
	viewer::view(x);
}
template <typename Head, typename... Tail>
void debug_out(const Head &H, const Tail &... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "]\n";                                       \
	} while(0)
#else
#define debug(...) (void(0))
#endif
#line 2 "SPJ-Library/template/scanner.hpp"

#line 6 "SPJ-Library/template/scanner.hpp"

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner
#line 7 "SPJ-Library/template/io.hpp"

#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << ' ' << p.second;
	return os;
}

struct fast_io {
	fast_io() {
		std::ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		std::cout << std::fixed << std::setprecision(15);
		srand((unsigned)time(NULL));
	}
} fast_io_;
#line 2 "SPJ-Library/template/macros.hpp"

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;
#line 7 "SPJ-Library/template/kuma.hpp"

using namespace std;
#line 6 "a.cpp"
string fir, sec;
vector<vector<pair<string, string>>> papers_str;
vector<vector<pair<int, int>>> papers;
int id = 0;
int paper_num;

bool solve() {
	cin >> fir >> sec;
	cin >> paper_num;
	map<string, int> names;
	names[fir] = 0;
	names[sec] = 0;
	papers_str.resize(paper_num);
	papers.resize(paper_num);
	foa(paper, papers_str) {
		int m;
		cin >> m;
		rep(m) {
			string a, b;
			cin >> a >> b;
			paper.emplace_back(a, b);
			names[a] = 0;
			names[b] = 0;
		}
	}

	foa(t, names) t.second = id++;

	vvvi pair_to_sheet_id(id, vvi(id));

	rep(i, paper_num) {
		auto &paper = papers[i];
		for(const auto &[a, b] : papers_str[i]) {
			int id_a = names[a];
			int id_b = names[b];
			paper.emplace_back(id_a, id_b);
			pair_to_sheet_id[id_a][id_b].push_back(i);
		}
	}

	vector<vector<int>> chked_pairs(id, vi(id));
	vector<int> checked_sheets(paper_num);
	queue<pair<int, int>> prs;

	auto add_que = [&](int id_a, int id_b) -> void {
		debug(id_a, id_b);
		if(chked_pairs[id_a][id_b]) return;
		chked_pairs[id_a][id_b] = 1;
		prs.emplace(id_a, id_b);
	};

	auto chk_sheet = [&](int sheet_id) -> void {
		debug(sheet_id);
		int &chked = checked_sheets[sheet_id];
		if(chked) return;
		chked = 1;
		for(const auto [ida, idb] : papers[sheet_id]) add_que(ida, idb);
		return;
	};

	auto chk_triplet = [&](int a, int b, int c) -> void {
		if(a == b || b == c) return;
		if(chked_pairs[a][b] && chked_pairs[b][c]) {
			add_que(a, c);
		}
		return;
	};

	add_que(names[fir], names[sec]);
	while(!prs.empty()) {
		int a, b;
		tie(a, b) = prs.front();
		prs.pop();

		for(int sheet_id : pair_to_sheet_id[a][b]) {
			chk_sheet(sheet_id);
		}
		rep(c, id) {
			chk_triplet(a, b, c);
			chk_triplet(c, a, b);
		}
	}

	debug(names);
	debug(chked_pairs);
	rep(i, id) {
		if(chked_pairs[i][i]) return false;
	}
	return true;
}

int main() {
	Yes(solve());
	return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 17444
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=305;
const ll INF=1LL<<60;

vector<int> S[MAX][MAX];

bool can[MAX][MAX];
bool seen[MAX][MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    map<string,int> MA;
    string s,t;cin>>s>>t;
    MA[s]=0;
    MA[t]=1;
    
    int N;cin>>N;
    vector<vector<pair<int,int>>> E(N);
    for(int i=0;i<N;i++){
        int K;cin>>K;
        E[i].resize(K);
        for(int j=0;j<K;j++){
            string a,b;cin>>a>>b;
            int x,y;
            if(MA.count(a)){
                x=MA[a];
            }else{
                x=si(MA);
                MA[a]=x;
            }
            
            if(MA.count(b)){
                y=MA[b];
            }else{
                y=si(MA);
                MA[b]=y;
            }
            
            E[i][j]=mp(x,y);
        }
        sort(all(E[i]));
        E[i].erase(unique(all(E[i])),E[i].end());
        
        for(auto [a,b]:E[i]){
            S[a][b].push_back(i);
        }
    }
    
    int V=si(MA);
    
    can[0][1]=true;
    while(1){
        set<int> SE;
        for(int i=0;i<V;i++){
            queue<int> Q;
            vector<int> mita(V);
            for(int j=0;j<V;j++){
                if(can[i][j]){
                    Q.push(j);
                    mita[j]=true;
                }
            }
            while(!Q.empty()){
                int u=Q.front();Q.pop();
                for(int k=0;k<V;k++){
                    if(can[u][k]&&!mita[k]){
                        Q.push(k);
                        mita[k]=true;
                    }
                }
            }
            for(int j=0;j<V;j++){
                if(mita[j]){
                    if(seen[i][j]) continue;
                    can[i][j]=true;
                    for(int x:S[i][j]){
                        SE.insert(x);
                    }
                    seen[i][j]=true;
                }
            }
        }
        
        if(si(SE)==0){
            cout<<"Yes\n";
            return 0;
        }
        
        for(int i:SE){
            for(auto [a,b]:E[i]){
                if(can[b][a]){
                    cout<<"No\n";
                    return 0;
                }
                can[a][b]=true;
            }
        }
    }
}
accuracy: 1
time_ms: 2150
memory_kb: 8572
final_rank: 8

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 10;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

//-----------------------------------

bool addedg[1000];
bool exi[300][300];
bool added[300][300];
vector<int> loc[300][300];
map<string, int> mp;
int isid(string s) {
	if (mp.find(s) == mp.end())mp[s] = mp.size();
	return mp[s];
}
void solve() {
	string p, q; cin >> p >> q;
	mp[p] = 0;
	mp[q] = 1;
	int n; cin >> n;
	vector<vector<P>> g(n);
	rep(i, n) {
		int m; cin >> m;
		rep(j, m) {
			string a, b; cin >> a >> b;
			g[i].push_back({ isid(a),isid(b) });
			loc[isid(a)][isid(b)].push_back(i);
		}
	}
	queue<P> que;
	auto addp = [&](int i, int j) {
		if (added[i][j])return;
		added[i][j] = true;
		que.push({ i,j });
	};
	addp(0, 1);
	while (!que.empty()) {
		P p = que.front(); que.pop();
		int i = p.first, j = p.second;
		exi[i][j] = true;
		for (int id : loc[i][j]) {
			if (!addedg[id]) {
				addedg[id] = true;
				for (P p : g[id]) {
					addp(p.first, p.second);
				}
			}
		}
		rep(k, mp.size())if (exi[j][k]) {
			addp(i, k);
		}
		rep(k, mp.size())if (exi[k][i]) {
			addp(k, j);
		}
	}
	string ans = "Yes";
	rep(i, mp.size())if (exi[i][i])ans = "No";
	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(8);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	//while(cin>>n,n)
	solve();
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 9188
final_rank: 4

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;

map<string,int> mp;
vector<string> nlst;

int getId(string s){
	if(mp.count(s))
		return mp[s];
	int id = mp.size();
	nlst.push_back(s);
	return mp[s] = id;
}

int readId(){
	string s;cin>>s;return getId(s);
}

int n;
vector<int> lst[321][321];

vector<int> src[1234],dst[1234];

bool done[1234];

bool reach[321][321];

int main(){
	int A=readId(),B=readId();

	cin>>n;

	rep(it,n){
		int m;cin>>m;

		rep(i,m){
			int u = readId(), v = readId();

			lst[u][v].push_back(it);

			src[it].push_back(u);
			dst[it].push_back(v);
		}
	}

	queue<int> que;

	que.push(A),que.push(B);
	reach[A][B] = 1;

	while(!que.empty()){
		int x = que.front();que.pop();
		int y = que.front();que.pop();
		//cout<<nlst[x]<<" "<<nlst[y]<<endl;

		rep(it,lst[x][y].size()){
			int id = lst[x][y][it];
			if(done[id])
				continue;
			done[id]=true;

			rep(t,src[id].size()){
				int u =src[id][t],v = dst[id][t];
				if(!reach[u][v]){
					reach[u][v]=1;
					que.push(u),que.push(v);
				}
			}
		}

		rep(xx,mp.size()) if(reach[xx][x] && !reach[xx][y]) {reach[xx][y] = 1;que.push(xx);que.push(y);}
		rep(yy,mp.size()) if(reach[y][yy] && !reach[x][yy]) {reach[x][yy] = 1;que.push(x);que.push(yy);}
	}

	bool ok = 1;
	rep(i,mp.size()) if(reach[i][i]) ok = 0;

	puts(ok?"Yes":"No");
}
accuracy: 1
time_ms: 190
memory_kb: 9472
final_rank: 5

code_snippet: #include "bits/stdc++.h"
//#include<stdio.h>
#define pb(x) push_back(x)
#define XX first
#define YY second
#define cst 1000000007
#define nor_rad(x) ( ( PI*x )/180)
#define rad_nor(x)  ( (x/IP)*180 )
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x));
#define eps 1e-9
#define rev reverse
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
using namespace std;
const int M = 310;

/// ===========================================  TAREQ =======================================================



// struct cm
// {
//   bool operator () ( const node n1, const node n2  ) const{
// return n1.cost>n2.cost;
//   }
// };

typedef struct{
    vector<int> x, y;
}Doc;

string P, Q;
int N;
map<string, int> ntoi;
bool matrix[M][M];


int main(){
    for(int i=0; i<M; i++)
        matrix[i][i] = true;
    
    cin >> P >> Q;
    ntoi[P] = 0;
    ntoi[Q] = 1;
    matrix[ntoi[P]][ntoi[Q]] = true;
    
    cin >> N;
    vector<Doc> docs;
    for(int i=0; i<N; i++){
        auto doc = Doc();
        int m; cin >> m;
        for(int i=0; i<m; i++){
            string x, y; cin >> x >> y;
            if(ntoi.find(x) == ntoi.end())
                ntoi[x] = ntoi.size() + 1;
            if(ntoi.find(y) == ntoi.end())
                ntoi[y] = ntoi.size() + 1;
            doc.x.push_back(ntoi[x]);
            doc.y.push_back(ntoi[y]);
        }
        docs.push_back(doc);
    }
    
    vector<bool> done(N);
    bool updated = true;
    while(updated){
        updated = false;
        for(int i=0; i<N; i++) if(!done[i]){
            auto &doc = docs[i];
            
            bool flag = false;
            for(int j=0; j<doc.x.size(); j++){
                if(matrix[doc.x[j]][doc.y[j]]){
                    flag = true;
                    break;
                }
            }
            if(!flag) continue;
            
            for(int j=0; j<doc.x.size(); j++){
                for(int k=0; k<M; k++){
                    if(matrix[k][doc.x[j]])
                        matrix[k][doc.y[j]] = true;
                    if(matrix[doc.y[j]][k])
                        matrix[doc.x[j]][k] = true;
                }
            }
            done[i] = true;
            updated = true;
        }
    }
    
    bool cycle = false;
    for(int i=0; i<M; i++){
        for(int j=0; j<M; j++) if(i != j){
            cycle |= matrix[i][j] && matrix[j][i];
        }
    }
    
    cout << (cycle? "No": "Yes") << endl;
    
    return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 4204
final_rank: 2

code_snippet: #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include<string>
#include<set>
#include<queue>
using namespace std;

#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a ,b) for(int i = int(a); i < int(b); ++i)
#define REP(i, a) FOR(i, 0, a)
#define RFOR(i, a, b) for(int i = int(b)-1; i >= int(a); --i)
#define RREP(i,a) RFOR(i, 0, a)
#define IN(a, x , b) (a <= x && x <= b)
template<class T> inline void CHMIN(T& a, T& b){if(a<b)a=b;}
template<class T> inline void CHMAX(T& a, T& b){if(a>b)a=b;}

using ll = long long;

template<class T> using V = std::vector<T>;



int main(){
  string par,child;
  cin>>par>>child;
  int N;
  cin>>N;
  int cnt=0;
  set<int>st;
  map<string,int>mp;
  map<pair<int,int>,vector<int>>kaburi;
  vector<vector<pair<int,int>>>v(N);
  std::vector<int>use;
  REP(i,N){
    int M;
    cin>>M;
    REP(j,M){
      string a,b;
      cin>>a>>b;
      if(!mp.count(a))mp[a]=cnt++;
      if(!mp.count(b))mp[b]=cnt++;
      v[i].push_back({mp[a],mp[b]});
      kaburi[{mp[a],mp[b]}].push_back(i);
      if(a==par&&b==child){
        use.push_back(i);
      }
    }
  }
  if(use.size()==0){
    cout<<"Yes"<<endl;
    return 0;
  }

  for(auto e:use)st.insert(e);
  vector<vector<int>>g(cnt);
  map<pair<int,int>,int>used;
  queue<int>que;
  for(auto e:use){
    for(auto p:v[e]){
      if(used.count({p.first,p.second}))continue;
      used[{p.first,p.second}]++;
      g[p.first].push_back(p.second);
      for(auto ee:kaburi[p]){
        if(st.count(ee))continue;
        st.insert(ee);
        que.push(ee);
      }
    }
  }
  while(que.size()){
    int e = que.front();
    que.pop();
    for(auto p:v[e]){
      if(used.count({p.first,p.second}))continue;
      used[{p.first,p.second}]++;
      g[p.first].push_back(p.second);
      for(auto ee:kaburi[p]){
        if(st.count(ee))continue;
        st.insert(ee);
        que.push(ee);
      }
    }
  }
  int ok = 1;


  vector<int>count(mp.size(),0);
  REP(i,cnt){
    for(auto e:g[i]){
      count[e]++;
    }
  }
while(que.size())que.pop();
  REP(i,cnt){
    if(count[i]==0)que.push(i);
  }
  while(que.size()){
    int now = que.front();
    que.pop();
    for(auto e:g[now]){
      count[e]--;
      if(count[e]==0)que.push(e);
    }
  }
  REP(i,cnt){
    if(count[i])ok=0;
  }

  if(ok){
    cout<<"Yes"<<endl;
  }else{
    cout<<"No"<<endl;
  }
}
accuracy: 0.03389830508474576
time_ms: 20
memory_kb: 4664
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) int(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXP = 302;
const int MAXN = 1003;
const ll MOD = 1000000007;


string ps, qs;
int n;

map<string, int> id;
int getID (string &name) {
    if (id.count(name)) {
        return id[name];
    } else {
        int sz = SZ(id);
        id[name] = sz;
        return sz;
    }
}

bool anc[MAXN][MAXP][MAXP];
bool edg[MAXN][MAXP][MAXP];
bool dag[MAXP][MAXP];
vector<int> edge[MAXP];
bool vis[MAXP];

typedef pair<int, int> pii;


void dfs (int nd, int hd, int cl) {
    vis[nd] = true;
    if (nd != hd) {
        anc[cl][hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd, cl);
        }
    }
}

bitset<MAXP> g[MAXP];
void add (int x, int y) {
    g[x][y] = true;
    g[x] |= g[y];
    REP (i, MAXP) {
        if (g[i][x]) {
            g[i] |= g[x];
        }
    }
}

void build (int cl) {
    int m;
    cin >> m;

    vector<int> vert;
    REP (i, m) {
        string f, t;
        cin >> f >> t;
        int nf = getID(f), nt = getID(t);
        edge[nf].emplace_back(nt);
        vert.emplace_back(nf);
        edg[cl][nf][nt] = true;
    }

    for (auto v : vert) {
        memset(vis, 0, sizeof(vis));
        dfs(v, v, cl);
    }
    for (auto v : vert) {
        edge[v].clear();
    }
}

bool tru[MAXN], fal[MAXN];
bool visp[MAXP][MAXP], visn[MAXN];
/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> ps >> qs;
    cin >> n;
    REP (i, n) {
        build(i);
    }

#ifdef tmdd
    for (auto p : id) {
        debug(p.first, p.second);
    }
    REP (i, n) {
        cout << "-----------------" << endl;
        REP (x, SZ(id)) {
            REP (y, SZ(id)) {
                cout << anc[i][x][y] << " \n"[y==SZ(id)-1];
            }
        }
    }
#endif

    queue<pii> bfs;
    bfs.emplace(getID(ps), getID(qs));

    visp[getID(ps)][getID(qs)] = true;

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (edg[i][cur.first][cur.second] && !visn[i]) {
                tru[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (edg[i][x][y]) {
                            add(x, y);
                        }
                    }
                }

                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (g[x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    bool err_flag = false;
    REP (x, SZ(id)) {
        REP (y, SZ(id)) {
            if (g[x][y] && g[y][x]) {
                err_flag = true;
            }
        }
    }

    cout << (err_flag ? "No" : "Yes") << endl;

    return 0;
}
accuracy: 1
time_ms: 2270
memory_kb: 153440
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) int(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXP = 302;
const int MAXN = 1003;
const ll MOD = 1000000007;


string ps, qs;
int n;

map<string, int> id;
int getID (string &name) {
    if (id.count(name)) {
        return id[name];
    } else {
        int sz = SZ(id);
        id[name] = sz;
        return sz;
    }
}

bool anc[MAXN][MAXP][MAXP];
bool edg[MAXN][MAXP][MAXP];
bool dag[MAXP][MAXP];
vector<int> edge[MAXP];
bool vis[MAXP];

typedef pair<int, int> pii;


void dfs (int nd, int hd, int cl) {
    vis[nd] = true;
    if (nd != hd) {
        anc[cl][hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd, cl);
        }
    }
}

void dfs (int nd, int hd) {
    vis[nd] = true;
    if (nd != hd) {
        dag[hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd);
        }
    }
}

void build (int cl) {
    int m;
    cin >> m;

    vector<int> vert;
    REP (i, m) {
        string f, t;
        cin >> f >> t;
        int nf = getID(f), nt = getID(t);
        edge[nf].emplace_back(nt);
        vert.emplace_back(nf);
        edg[cl][nf][nt] = true;
    }

    for (auto v : vert) {
        memset(vis, 0, sizeof(vis));
        dfs(v, v, cl);
    }
    for (auto v : vert) {
        edge[v].clear();
    }
}

bool tru[MAXN], fal[MAXN];
bool visp[MAXP][MAXP], visn[MAXN];

/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> ps >> qs;
    cin >> n;
    REP (i, n) {
    debug(i);
        build(i);
    }

#ifdef tmd
    for (auto p : id) {
        debug(p.first, p.second);
    }
    REP (i, n) {
        cout << "-----------------" << endl;
        REP (x, SZ(id)) {
            REP (y, SZ(id)) {
                cout << anc[i][x][y] << " \n"[y==SZ(id)-1];
            }
        }
    }
#endif

    queue<pii> bfs;
    bfs.emplace(getID(ps), getID(qs));

    visp[getID(ps)][getID(qs)] = true;

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (edg[i][cur.first][cur.second] && !visn[i]) {
                tru[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    memset(visp, 0, sizeof(visp));
    memset(visn, 0, sizeof(visn));

    REP (i, n) {
        if (tru[i]) {
            REP (x, SZ(id)) {
                REP (y, SZ(id)) {
                    if (edg[i][x][y]) {
                        edge[x].emplace_back(y);
                    }
                }
            }
        }
    }

    REP (i, SZ(id)) {
        memset(vis, 0, sizeof(vis));
        dfs(i, i);
    }

    REP (x, SZ(id)) {
        REP (y, SZ(id)) {
            if (dag[x][y]) {
                visp[y][x] = true;
                bfs.emplace(y, x);
            }
        }
    }

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                fal[i] = true;
                visn[i] = true;

                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (edg[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    bool err_flag = false;
    REP (i, n) {
        err_flag |= tru[i] && fal[i];
        debug(tru[i], fal[i]);
    }

    cout << (err_flag ? "No" : "Yes") << endl;

    return 0;
}
accuracy: 0.13559322033898305
time_ms: 2030
memory_kb: 110932
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) int(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXP = 302;
const int MAXN = 1003;
const ll MOD = 1000000007;


string ps, qs;
int n;

map<string, int> id;
int getID (string &name) {
    if (id.count(name)) {
        return id[name];
    } else {
        int sz = SZ(id);
        id[name] = sz;
        return sz;
    }
}

bool anc[MAXN][MAXP][MAXP];
bool edg[MAXN][MAXP][MAXP];
bool dag[MAXP][MAXP];
vector<int> edge[MAXP];
bool vis[MAXP];

typedef pair<int, int> pii;


void dfs (int nd, int hd, int cl) {
    vis[nd] = true;
    if (nd != hd) {
        anc[cl][hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd, cl);
        }
    }
}

void dfs (int nd, int hd) {
    vis[nd] = true;
    if (nd != hd) {
        dag[hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd);
        }
    }
}

void build (int cl) {
    int m;
    cin >> m;

    vector<int> vert;
    REP (i, m) {
        string f, t;
        cin >> f >> t;
        int nf = getID(f), nt = getID(t);
        edge[nf].emplace_back(nt);
        vert.emplace_back(nf);
        edg[cl][nf][nt] = true;
    }

    for (auto v : vert) {
        memset(vis, 0, sizeof(vis));
        dfs(v, v, cl);
    }
    for (auto v : vert) {
        edge[v].clear();
    }
}

bool tru[MAXN], fal[MAXN];
bool visp[MAXP][MAXP], visn[MAXN];

/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> ps >> qs;
    cin >> n;
    REP (i, n) {
    debug(i);
        build(i);
    }

#ifdef tmd
    for (auto p : id) {
        debug(p.first, p.second);
    }
    REP (i, n) {
        cout << "-----------------" << endl;
        REP (x, SZ(id)) {
            REP (y, SZ(id)) {
                cout << anc[i][x][y] << " \n"[y==SZ(id)-1];
            }
        }
    }
#endif

    queue<pii> bfs;
    bfs.emplace(getID(ps), getID(qs));

    visp[getID(ps)][getID(qs)] = true;

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (edg[i][cur.first][cur.second] && !visn[i]) {
                tru[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    memset(visp, 0, sizeof(visp));
    memset(visn, 0, sizeof(visn));

    REP (i, n) {
        if (tru[i]) {
            REP (x, SZ(id)) {
                REP (y, SZ(id)) {
                    if (edg[i][x][y]) {
                        edge[x].emplace_back(y);
                    }
                }
            }
        }
    }

    REP (i, SZ(id)) {
        memset(vis, 0, sizeof(vis));
        dfs(i, i);
    }

    REP (x, SZ(id)) {
        REP (y, SZ(id)) {
            if (dag[x][y]) {
                visp[x][y] = true;
                bfs.emplace(x, y);
            }
        }
    }

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                fal[i] = true;
                visn[i] = true;

                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (edg[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    bool err_flag = false;
    REP (i, n) {
        err_flag |= tru[i] && fal[i];
        debug(tru[i], fal[i]);
    }

    cout << (err_flag ? "No" : "Yes") << endl;

    return 0;
}
accuracy: 0.3898305084745763
time_ms: 2020
memory_kb: 153884
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) int(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXP = 302;
const int MAXN = 1003;
const ll MOD = 1000000007;


string ps, qs;
int n;

map<string, int> id;
int getID (string &name) {
    if (id.count(name)) {
        return id[name];
    } else {
        int sz = SZ(id);
        id[name] = sz;
        return sz;
    }
}

bool anc[MAXN][MAXP][MAXP];
bool edg[MAXN][MAXP][MAXP];
bool dag[MAXP][MAXP];
vector<int> edge[MAXP];
bool vis[MAXP];

typedef pair<int, int> pii;


void dfs (int nd, int hd, int cl) {
    vis[nd] = true;
    if (nd != hd) {
        anc[cl][hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd, cl);
        }
    }
}

void dfs (int nd, int hd) {
    vis[nd] = true;
    if (nd != hd) {
        dag[hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd);
        }
    }
}

void build (int cl) {
    int m;
    cin >> m;

    vector<int> vert;
    REP (i, m) {
        string f, t;
        cin >> f >> t;
        int nf = getID(f), nt = getID(t);
        edge[nf].emplace_back(nt);
        vert.emplace_back(nf);
        edg[cl][nf][nt] = true;
    }

    for (auto v : vert) {
        memset(vis, 0, sizeof(vis));
        dfs(v, v, cl);
    }
    for (auto v : vert) {
        edge[v].clear();
    }
}

bool tru[MAXN], fal[MAXN];
bool visp[MAXP][MAXP], visn[MAXN];

/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> ps >> qs;
    cin >> n;
    REP (i, n) {
    debug(i);
        build(i);
    }

#ifdef tmd
    for (auto p : id) {
        debug(p.first, p.second);
    }
    REP (i, n) {
        cout << "-----------------" << endl;
        REP (x, SZ(id)) {
            REP (y, SZ(id)) {
                cout << anc[i][x][y] << " \n"[y==SZ(id)-1];
            }
        }
    }
#endif

    queue<pii> bfs;
    bfs.emplace(getID(ps), getID(qs));

    visp[getID(ps)][getID(qs)] = true;

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (edg[i][cur.first][cur.second] && !visn[i]) {
                tru[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    memset(visp, 0, sizeof(visp));
    memset(visn, 0, sizeof(visn));

    REP (i, n) {
        if (tru[i]) {
            REP (x, SZ(id)) {
                REP (y, SZ(id)) {
                    if (anc[i][x][y]) {
                        edge[x].emplace_back(y);
                    }
                }
            }
        }
    }

    REP (i, SZ(id)) {
        memset(vis, 0, sizeof(vis));
        dfs(i, i);
    }

    REP (x, SZ(id)) {
        REP (y, SZ(id)) {
            if (dag[x][y]) {
                visp[x][y] = true;
                bfs.emplace(x, y);
            }
        }
    }

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                fal[i] = true;
                visn[i] = true;

                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (edg[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    bool err_flag = false;
    REP (i, n) {
        err_flag |= tru[i] && fal[i];
        debug(tru[i], fal[i]);
    }

    cout << (err_flag ? "No" : "Yes") << endl;

    return 0;
}
accuracy: 0.3898305084745763
time_ms: 2110
memory_kb: 158232
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) int(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXP = 302;
const int MAXN = 1003;
const ll MOD = 1000000007;


string ps, qs;
int n;

map<string, int> id;
int getID (string &name) {
    if (id.count(name)) {
        return id[name];
    } else {
        int sz = SZ(id);
        id[name] = sz;
        return sz;
    }
}

bool anc[MAXN][MAXP][MAXP];
bool edg[MAXN][MAXP][MAXP];
vector<int> edge[MAXP];
bool vis[MAXP];

typedef pair<int, int> pii;


void dfs (int nd, int hd, int cl) {
    vis[nd] = true;
    if (nd != hd) {
        anc[cl][hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd, cl);
        }
    }
}
void build (int cl) {
    int m;
    cin >> m;

    vector<int> vert;
    REP (i, m) {
        string f, t;
        cin >> f >> t;
        int nf = getID(f), nt = getID(t);
        edge[nf].emplace_back(nt);
        vert.emplace_back(nf);
        edg[cl][nf][nt] = true;
    }

    for (auto v : vert) {
        memset(vis, 0, sizeof(vis));
        dfs(v, v, cl);
    }
    for (auto v : vert) {
        edge[v].clear();
    }
}

bool tru[MAXN], fal[MAXN];
bool visp[MAXP][MAXP], visn[MAXN];

/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> ps >> qs;
    cin >> n;
    REP (i, n) {
    debug(i);
        build(i);
    }

#ifdef tmd
    for (auto p : id) {
        debug(p.first, p.second);
    }
    REP (i, n) {
        cout << "-----------------" << endl;
        REP (x, SZ(id)) {
            REP (y, SZ(id)) {
                cout << anc[i][x][y] << " \n"[y==SZ(id)-1];
            }
        }
    }
#endif

    queue<pii> bfs;
    bfs.emplace(getID(ps), getID(qs));

    visp[getID(ps)][getID(qs)] = true;

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (edg[i][cur.first][cur.second] && !visn[i]) {
                tru[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    memset(visp, 0, sizeof(visp));
    memset(visn, 0, sizeof(visn));

    REP (i, n) {
        if (tru[i]) {
            REP (x, SZ(id)) {
                REP (y, SZ(id)) {
                    if (anc[i][x][y] && !visp[y][x]) {
                        visp[y][x] = true;
                        bfs.emplace(y, x);
                    }
                }
            }
        }
    }

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                fal[i] = true;
                visn[i] = true;

                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (edg[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    bool err_flag = false;
    REP (i, n) {
        err_flag |= tru[i] && fal[i];
        debug(tru[i], fal[i]);
    }

    cout << (err_flag ? "No" : "Yes") << endl;

    return 0;
}
accuracy: 0.13559322033898305
time_ms: 2310
memory_kb: 110176
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) int(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXP = 302;
const int MAXN = 1003;
const ll MOD = 1000000007;


string ps, qs;
int n;

map<string, int> id;
int getID (string &name) {
    if (id.count(name)) {
        return id[name];
    } else {
        int sz = SZ(id);
        id[name] = sz;
        return sz;
    }
}

bool anc[MAXN][MAXP][MAXP];
bool edg[MAXN][MAXP][MAXP];
vector<int> edge[MAXP];
bool vis[MAXP];

typedef pair<int, int> pii;


void dfs (int nd, int hd, int cl) {
    vis[nd] = true;
    if (nd != hd) {
        anc[cl][hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd, cl);
        }
    }
}
void build (int cl) {
    int m;
    cin >> m;

    vector<int> vert;
    REP (i, m) {
        string f, t;
        cin >> f >> t;
        int nf = getID(f), nt = getID(t);
        edge[nf].emplace_back(nt);
        vert.emplace_back(nf);
        edg[cl][nf][nt] = true;
    }

    for (auto v : vert) {
        memset(vis, 0, sizeof(vis));
        dfs(v, v, cl);
    }
    for (auto v : vert) {
        edge[v].clear();
    }
}

bool tru[MAXN], fal[MAXN];
bool visp[MAXP][MAXP], visn[MAXN];

/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> ps >> qs;
    cin >> n;
    REP (i, n) {
        build(i);
    }

#ifdef tmd
    for (auto p : id) {
        debug(p.first, p.second);
    }
    REP (i, n) {
        cout << "-----------------" << endl;
        REP (x, SZ(id)) {
            REP (y, SZ(id)) {
                cout << anc[i][x][y] << " \n"[y==SZ(id)-1];
            }
        }
    }
#endif

    queue<pii> bfs;
    bfs.emplace(getID(ps), getID(qs));

    visp[getID(ps)][getID(qs)] = true;

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                tru[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    memset(visp, 0, sizeof(visp));
    memset(visn, 0, sizeof(visn));

    REP (i, n) {
        if (tru[i]) {
            REP (x, SZ(id)) {
                REP (y, SZ(id)) {
                    if (anc[i][x][y] && !visp[y][x]) {
                        visp[y][x] = true;
                        bfs.emplace(y, x);
                    }
                }
            }
        }
    }

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                fal[i] = true;
                visn[i] = true;

                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (edg[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    bool err_flag = false;
    REP (i, n) {
        err_flag |= tru[i] && fal[i];
        debug(tru[i], fal[i]);
    }

    cout << (err_flag ? "No" : "Yes") << endl;

    return 0;
}
accuracy: 0.01694915254237288
time_ms: 70
memory_kb: 27904
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) int(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXP = 302;
const int MAXN = 1003;
const ll MOD = 1000000007;


string ps, qs;
int n;

map<string, int> id;
int getID (string &name) {
    if (id.count(name)) {
        return id[name];
    } else {
        int sz = SZ(id);
        id[name] = sz;
        return sz;
    }
}

bool anc[MAXN][MAXP][MAXP];
vector<int> edge[MAXP];
bool vis[MAXP];

typedef pair<int, int> pii;
vector<pii> state[MAXN];

void dfs (int nd, int hd, int cl) {
    vis[nd] = true;
    if (nd != hd) {
        anc[cl][hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd, cl);
        }
    }
}
void build (int cl) {
    int m;
    cin >> m;

    vector<int> vert;
    REP (i, m) {
        string f, t;
        cin >> f >> t;
        int nf = getID(f), nt = getID(t);
        edge[nf].emplace_back(nt);
        vert.emplace_back(nf);
        state[cl].emplace_back(nf, nt);
    }

    for (auto v : vert) {
        memset(vis, 0, sizeof(vis));
        dfs(v, v, cl);
    }
    for (auto v : vert) {
        edge[v].clear();
    }
}

bool tru[MAXN], fal[MAXN];
bool visp[MAXP][MAXP], visn[MAXN];

/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> ps >> qs;
    cin >> n;
    REP (i, n) {
        build(i);
    }

#ifdef tmd
    for (auto p : id) {
        debug(p.first, p.second);
    }
    REP (i, n) {
        cout << "-----------------" << endl;
        REP (x, SZ(id)) {
            REP (y, SZ(id)) {
                cout << anc[i][x][y] << " \n"[y==SZ(id)-1];
            }
        }
    }
#endif

    queue<pii> bfs;
    bfs.emplace(getID(ps), getID(qs));

    while (SZ(id) > 300) {

    }
    visp[getID(ps)][getID(qs)] = true;

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                tru[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    memset(visp, 0, sizeof(visp));
    memset(visn, 0, sizeof(visn));

    while (bfs.size()) {
        bfs.pop();
    }
    assert(bfs.empty());
    REP (i, n) {
        if (tru[i]) {
            REP (x, SZ(id)) {
                REP (y, SZ(id)) {
                    if (anc[i][x][y] && !visp[y][x]) {
                        visp[y][x] = true;
                        bfs.emplace(y, x);
                    }
                }
            }
        }
    }

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                fal[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    bool err_flag = false;
    REP (i, n) {
        err_flag |= tru[i] && fal[i];
    }

    cout << (err_flag ? "No" : "Yes") << endl;

    return 0;
}
accuracy: 0.01694915254237288
time_ms: 70
memory_kb: 15800
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define SZ(i) int(i.size())
#ifdef tmd
#define IOS()
#define debug(...) fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define debug(...)
#endif

const int MAXP = 302;
const int MAXN = 1003;
const ll MOD = 1000000007;


string ps, qs;
int n;

map<string, int> id;
int getID (string &name) {
    if (id.count(name)) {
        return id[name];
    } else {
        int sz = SZ(id);
        id[name] = sz;
        return sz;
    }
}

bool anc[MAXN][MAXP][MAXP];
vector<int> edge[MAXP];
bool vis[MAXP];

typedef pair<int, int> pii;
vector<pii> state[MAXN];

void dfs (int nd, int hd, int cl) {
    vis[nd] = true;
    if (nd != hd) {
        anc[cl][hd][nd] = true;
    }
    for (auto v : edge[nd]) {
        if (!vis[v]) {
            dfs(v, hd, cl);
        }
    }
}
void build (int cl) {
    int m;
    cin >> m;

    vector<int> vert;
    REP (i, m) {
        string f, t;
        cin >> f >> t;
        int nf = getID(f), nt = getID(t);
        edge[nf].emplace_back(nt);
        vert.emplace_back(nf);
        state[cl].emplace_back(nf, nt);
    }

    for (auto v : vert) {
        memset(vis, 0, sizeof(vis));
        dfs(v, v, cl);
    }
    for (auto v : vert) {
        edge[v].clear();
    }
}

bool tru[MAXN], fal[MAXN];
bool visp[MAXP][MAXP], visn[MAXN];

/*********************GoodLuck***********************/
int main () {
    IOS();

    cin >> ps >> qs;
    cin >> n;
    REP (i, n) {
        build(i);
    }

#ifdef tmd
    for (auto p : id) {
        debug(p.first, p.second);
    }
    REP (i, n) {
        cout << "-----------------" << endl;
        REP (x, SZ(id)) {
            REP (y, SZ(id)) {
                cout << anc[i][x][y] << " \n"[y==SZ(id)-1];
            }
        }
    }
#endif

    queue<pii> bfs;
    bfs.emplace(getID(ps), getID(qs));

    visp[getID(ps)][getID(qs)] = true;

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                tru[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    memset(visp, 0, sizeof(visp));
    memset(visn, 0, sizeof(visn));

    REP (i, n) {
        if (tru[i]) {
            REP (x, SZ(id)) {
                REP (y, SZ(id)) {
                    if (anc[i][x][y] && !visp[y][x]) {
                        visp[y][x] = true;
                        bfs.emplace(y, x);
                    }
                }
            }
        }
    }

    while (!bfs.empty()) {
        pii cur = bfs.front();
        bfs.pop();

        REP (i, n) {
            if (anc[i][cur.first][cur.second] && !visn[i]) {
                fal[i] = true;
                visn[i] = true;
                REP (x, SZ(id)) {
                    REP (y, SZ(id)) {
                        if (anc[i][x][y] && !visp[x][y]) {
                            visp[x][y] = true;
                            bfs.emplace(x, y);
                        }
                    }
                }
            }
        }
    }

    bool err_flag = false;
    REP (i, n) {
        err_flag |= tru[i] && fal[i];
    }

    cout << (err_flag ? "No" : "Yes") << endl;

    return 0;
}
accuracy: 0.01694915254237288
time_ms: 70
memory_kb: 15740
final_rank: 18