Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

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 ancestor of $y$.

We are sure that every person mentioned in the documents had a unique single name, i.e., no two persons have the same name and one person is never mentioned with different names.

When a person A is an ancestor of another person B, the person A can be a parent, a grandparent, a great-grandparent, or so on, of the person B. Also, there can be persons or ancestor-descendant pairs that do not appear in the documents. For example, for a family tree shown in Figure F.1, there can be a positive document:

A H
B H
D H
F H
E I

where persons C and G do not appear, and the ancestor-descendant pairs such as "A E", "D F", and "C I" do not appear.

Figure F.1. A Family Tree

Input

The input consists of a single test case of the following format.

$p$ $q$

$n$

$c_1$

.

.

.

$c_n$

The first line of a test case consists of two distinct names, $p$ and $q$, separated by a space. The second line of a test case consists of a single integer $n$ that indicates the number of documents. Then the descriptions of $n$ documents follow.

The description of the $i$-th document $c_i$ is formatted as follows:

$m_i$

$x_{i,1}$ $y_{i,1}$

.

.

.

$x_{i,m_i}$ $y_{i,m_i}$

The first line consists of a single integer $m_i$ that indicates the number of pairs of names in the document. Each of the following $m_i$ lines consists of a pair of distinct names $x_{i,j}$ and $y_{i,j}$ ($1 \leq j \leq m_i$), separated by a space.

Each name consists of lowercase or uppercase letters and its length is between 1 and 5, inclusive.

A test case satisfies the following constraints.

$1 \leq n \leq 1000$.

$1 \leq m_i$.

$\sum^{n}_{i=1} m_i \leq 100000$, that is, the total number of pairs of names in the documents does not exceed 100000.

The number of distinct names that appear in a test case does not exceed 300.

Output

Output "Yes" (without quotes) in a single line if there exists an interpretation of the documents that does not contradict with the hypothesis that $p$ is an ancestor of $q$. Output "No", otherwise.

Sample Input 1

Alice Bob
3
2
Alice Bob
Bob Clare
2
Bob Clare
Clare David
2
Clare David
David Alice

Sample Output 1

No

Sample Input 2

Alice David
3
2
Alice Bob
Bob Clare
2
Bob Clare
Clare David
2
Clare David
David Alice

Sample Output 2

Yes

Sample Input 3

Alice Bob
1
2
Clare David
David Clare

Sample Output 3

Yes

Solution in C++11 (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#define int long long
#define N 100010
using namespace std;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}

class SCC{
public:
  int V,K;                      //????????°, scc??????????????°
  vector<vector<int> > G;       //??°???????????£??\??????????????¨???
  vector<vector<int> >rG;       //?????????????????????????????°??????
  vector<vector<int> >DAG;      //scc????????°???????????£??\??????????????¨???
  vector<vector<int> >rDAG;     //scc???????????????????????????????????°??????
  vector<int> vs;               //??°?????????????????????
  vector<int>used;              //?????§???????????????
  vector<int> cmp;              //?±????????????£?????????????????????????????????

  void init(){G.resize(V);rG.resize(V);used.resize(V,0);cmp.resize(V,0);}
  SCC(){V = K = -1;}
  SCC(int V):V(V){init();};
  
  void add_edge(int from,int to){
    assert(from<V && to<V);
    G[from].push_back(to);
    rG[to].push_back(from);
  }

  void dfs(int v){
    used[v]=true;
    for(int nx:G[v]) if(!used[nx]) dfs(nx);
    vs.push_back(v);
  }

  void rdfs(int v,int k){
    used[v]=true;
    cmp[v]=k;
    for(int nx:rG[v]) if(!used[nx]) rdfs(nx,k);
  }

  int scc(bool makeDAG = 0){ //DAG?????°???????????????????????£??????makeDAG???1?????????
    used.clear(); used.resize(V,0);
    vs.clear();
    for(int v=0;v<V;v++) if(!used[v]) dfs(v);
  
    used.clear(); used.resize(V,0);
    int &k= K = 0;
    for(int i=vs.size()-1;i>=0;i--)if(!used[vs[i]]) rdfs(vs[i],k++);

    if(makeDAG) DAG = getDAG(G), rDAG = getDAG(rG); //O(ElogE)

    return k;
  }

  vector<vector<int> >getDAG(const vector<vector<int> > &G){
    assert(K>=0);
    vector<vector<int> >res(K);
    for(int from=0;from<V;from++)
      for(int to:G[from]) if(cmp[from]!=cmp[to]) res[cmp[from]].push_back(cmp[to]);
    
    for(int i=0;i<K;i++){
      sort(res[i].begin(),res[i].end());
      res[i].erase(unique(res[i].begin(),res[i].end()),res[i].end());
    }
    return res;
  }
};

typedef pair<int,int> P;
typedef vector<P> vec;
typedef set<vec> mat;
bool check(const vec &A, const vec &B, bool a, bool b){
  int n(310);
  SCC s(n);
  if(!a) for(P p:A) s.add_edge(p.first,p.second);
  if(!b) for(P p:B) s.add_edge(p.first,p.second);
  if( s.scc() != n ) return 0;
  vector<set<int> > childs(n);

  for(int pos:s.vs)
    for(int to:s.rG[pos]) {
      childs[to].insert(pos);
      for(int c:childs[pos]) childs[to].insert(c);
    }

  if(a) for(P p:A) if(childs[p.first].count(p.second)) return 0;
  if(b) for(P p:B) if(childs[p.first].count(p.second)) return 0;
  return 1;
}

signed main(){

  string a,b;
  cin >>a>>b;
  map<string,int> idx;
  int num = 0;
  idx[a] = num++, idx[b] = num++;
  int n;
  cin>>n;
  vector<vec> G;
  G.push_back(vec({P(idx[a],idx[b])}));
  for(int i=0;i<n;i++){
    int m;
    cin>>m;
    vec g(m);
    for(int j=0;j<m;j++){
      string a,b;
      cin>>a>>b;
      if(!idx.count(a)) idx[a] = num++;
      if(!idx.count(b)) idx[b] = num++;
      g[j] = P(idx[a],idx[b]);
    }
    G.push_back(g);
  }

  queue<int> Q; Q.push(0);
  vector<int> used(n+1,-1); used[0] = 0;

  while(!Q.empty()){
    int i = Q.front();Q.pop();
    for(int j=0;j<=n;j++){
      for(int b=0;b<2;b++){
        int a = used[i];
        if(i == j || used[j] != -1|| check(G[i],G[j],a,b)) continue;
        Q.push(j);
        used[j] = !b;
      }
    }
  }
  
  vector<P> A,B;
  for(int i=0;i<=n;i++){ 
    if(used[i] == 0) for(P p:G[i]) A.push_back(p);
    if(used[i] == 1) for(P p:G[i]) B.push_back(p);
  }
  
  int ans = check(A,B,0,1);
  cout<<(ans? "Yes":"No")<<endl;
  
  return 0;
}