Problem:
Courage Test
P「今日は肝試し企画の番組の収録だ。」
O型智絵里「き、きもだめし……」
四村かな子「智絵里ちゃん大丈夫？」
月野茜「さすがPさん！一行で状況が明晰判明にわかる素晴らしい解説です！さあ！火の玉に向かって走りましょう！」
P「ルールは簡単、みんなで協力してこのエリアのすべての神社にお参りしてくるだけだ。」
茜「オーソドックスですね！早速神社に向かって走りましょう！」
かな子「でもここ、日本有数の神社密度で行かなくちゃいけない神社がたくさんあるんじゃなかったっけ……」
かな子「でも茜ちゃんがいれば心強いよ！」
P「ちなみにスタートは全員違う神社からスタート、合流も禁止だ。それじゃあ智絵里とかな子は初期位置についてくれ」
智絵里「うぅ……」
かな子「お、お菓子を持っていけば大丈夫！」
智絵里「頑張る……」
茜「プロデューサー！私はどうすれば！」
P「お前は賑やかしだ」
茜「えっ」
P「座ってろ」
茜「」
Problem
それぞれ1~
n
の番号が付けられた
n
個の神社と
n
-1本の道がある。各道は神社
a
i
と神社
b
i
を繋ぎ、双方向に移動することができる。それぞれの神社は、任意の神社から1本以上の道を経由して到達することができる。また、任意の2つの神社の間の(迂回しない)経路は一意に定まる。
O型智絵里ちゃんと四村かな子ちゃんがそれぞれ神社
u
と神社
v
にいる。
今、2人を任意に移動させることを考える。移動とは、ある神社から1本の道を経由して別の神社に進むことである。
その際、以下のルールを満たしたい。
2人合わせて全ての神社を訪問する。
2人の移動数を同じにする。
1つの神社, 道は1人しか通れず、さらに1度しか通ることができない。
神社
u
,
v
は既に訪れているものとし、通ることはできない。
これらのルールを満たすことができるかどうかを判定し、できる場合は"Yes"を、できない場合は"No"を出力せよ。
Input
入力は以下の形式で与えられる。
n
u
v
a
1
b
1
a
2
b
2
...
a
n−1
b
n−1
1行目に3つの整数
n
,
u
,
v
が空白区切りで与えられる。
2行目から
n
行目までに、2つの整数
a
i
,
b
i
が空白区切りで与えられる。
Constraints
2 ≤
n
≤ 10
5
1 ≤
a
i
,
b
i
,
u
,
v
≤
n
u
≠
v
a
i
<
b
i
(
a
i
,
b
i
) ≠ (
a
j
,
b
j
) (
i
≠
j
)
Output
ルールを満たすことができる場合は"Yes"を、できない場合は"No"を1行に出力する。
Sample Input 1
4 1 4
1 2
2 3
3 4
Sample Output 1
Yes
Sample Input 2
8 3 6
1 2
2 4
3 4
4 5
5 6
6 7
7 8
Sample Output 2
No
Sample Input 3
4 2 3
1 2
2 3
3 4
Sample Output 3
Yes
Sample Input 4
6 1 5
1 2
1 4
2 3
4 5
3 6
Sample Output 4
No

Submitted solutions:

code_snippet: // AOJ #1577 Courage Test
// 2025.2.22

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

bool solve() {
    int n = Cin(), U = Cin(), V = Cin();

    if(n & 1) return 0;

    vector<vector<int>> G(n+1);
    for(int i = 1; i <= n-1; i++){
        int a = Cin(), b = Cin();
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int leafCount = 0, deg3Count = 0, maxDeg = 0;
    for (int i = 1; i <= n; i++){
        int d = G[i].size();
        if(d == 1) leafCount++;
        if(d == 3) deg3Count++;
        maxDeg = max(maxDeg, d);
    }
    bool validPattern = false;
    if(leafCount == 2 && maxDeg <= 2) validPattern = true;
    else if(leafCount == 3 && maxDeg == 3 && deg3Count == 1) validPattern = true;
    else if(leafCount == 4 && maxDeg == 3 && deg3Count == 2) validPattern = true;

    if(!validPattern) return 0;

    vector<int> sub(n+1, 0);
    int candidate_u = -1, candidate_v = -1;
    function<void(int,int)> dfs = [&](int cur, int parent){
        sub[cur] = 1;
        for (int nb : G[cur]){
            if(nb == parent) continue;
            dfs(nb, cur);
            sub[cur] += sub[nb];
            if(sub[nb] == n/2){
                candidate_u = cur;
                candidate_v = nb;
            }
        }
    };
    dfs(1, 0);
    if(candidate_u == -1) return 0;

    vector<bool> compMark(n+1, false);
    auto get_component = [&](int start) -> vector<int> {
        vector<int> comp;
        queue<int> que;
        compMark.assign(n+1, false);
        compMark[start] = true;
        que.push(start);
        while(!que.empty()){
            int cur = que.front();
            que.pop();
            comp.push_back(cur);
            for (int nb : G[cur]){
                if((cur == candidate_u && nb == candidate_v) || (cur == candidate_v && nb == candidate_u))
                    continue;
                if(!compMark[nb]){
                    compMark[nb] = true;
                    que.push(nb);
                }
            }
        }
        return comp;
    };
    vector<int> comp1 = get_component(candidate_v);
    vector<int> comp2 = get_component(candidate_u);
    if((int)comp1.size() != n/2 || (int)comp2.size() != n/2) return 0;

    bool uInComp1 = false, vInComp1 = false;
    for (int x : comp1){
        if(x == U) uInComp1 = true;
        if(x == V) vInComp1 = true;
    }
    if((uInComp1 && vInComp1) || (!uInComp1 && !vInComp1)) return 0;

    auto get_endpoints = [&](const vector<int>& comp) -> vector<int> {
        vector<bool> inComp(n+1, false);
        for (int x : comp) inComp[x] = true;
        vector<int> ends;
        for (int x : comp) {
            int cnt = 0;
            for (int nb : G[x]){
                if(!inComp[nb]) continue;
                cnt++;
            }
            if(cnt == 1) ends.push_back(x);
        }
        return ends;
    };
    vector<int> ends1 = get_endpoints(comp1);
    vector<int> ends2 = get_endpoints(comp2);
    if(ends1.size() != 2 || ends2.size() != 2) return 0;

    vector<int> endpoints;
    endpoints.insert(endpoints.end(), ends1.begin(), ends1.end());
    endpoints.insert(endpoints.end(), ends2.begin(), ends2.end());

    auto contains = [&](int x) -> bool {
        for (int y : endpoints)
            if(y == x) return true;
        return false;
    };
    if(!contains(U) || !contains(V)) return 0;

    int requiredDist = (n - 2) / 2;

    auto bfs_distance = [&](int start) -> vector<int> {
        vector<int> dist(n+1, INF);
        queue<int> que;
        dist[start] = 0;
        que.push(start);
        while(!que.empty()){
            int cur = que.front();
            que.pop();
            for (int nb : G[cur]){
                if(dist[nb] > dist[cur] + 1){
                    dist[nb] = dist[cur] + 1;
                    que.push(nb);
                }
            }
        }
        return dist;
    };
    vector<vector<int>> d(4);
    for (int i = 0; i < 4; i++){
        d[i] = bfs_distance(endpoints[i]);
    }

    vector<pair<pair<int,int>, pair<int,int>>> partitions = {
        {{0,1}, {2,3}},
        {{0,2}, {1,3}},
        {{0,3}, {1,2}}
    };
    bool ok = false;
    for(auto &part : partitions){
        auto p1 = part.first;
        auto p2 = part.second;
        if(d[p1.first][ endpoints[p1.second] ] == requiredDist &&
           d[p2.first][ endpoints[p2.second] ] == requiredDist)
            ok = true;
    }
    return ok;
}

int main(){
    printf(solve()? "Yes\n": "No\n");
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 20396
final_rank: 6

code_snippet: // AOJ #1577 Courage Test
// 2025.2.22

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

bool solve() {
    int n, U, V;
    cin >> n >> U >> V;

    if(n & 1) return 0;

    vector<vector<int>> G(n+1);
    for(int i = 1; i <= n-1; i++){
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int leafCount = 0, deg3Count = 0, maxDeg = 0;
    for (int i = 1; i <= n; i++){
        int d = G[i].size();
        if(d == 1) leafCount++;
        if(d == 3) deg3Count++;
        maxDeg = max(maxDeg, d);
    }
    bool validPattern = false;
    if(leafCount == 2 && maxDeg <= 2) validPattern = true;
    else if(leafCount == 3 && maxDeg == 3 && deg3Count == 1) validPattern = true;
    else if(leafCount == 4 && maxDeg == 3 && deg3Count == 2) validPattern = true;

    if(!validPattern) return 0;

    vector<int> sub(n+1, 0);
    int candidate_u = -1, candidate_v = -1;
    function<void(int,int)> dfs = [&](int cur, int parent){
        sub[cur] = 1;
        for (int nb : G[cur]){
            if(nb == parent) continue;
            dfs(nb, cur);
            sub[cur] += sub[nb];
            if(sub[nb] == n/2){
                candidate_u = cur;
                candidate_v = nb;
            }
        }
    };
    dfs(1, 0);
    if(candidate_u == -1) return 0;

    vector<bool> compMark(n+1, false);
    auto get_component = [&](int start) -> vector<int> {
        vector<int> comp;
        queue<int> que;
        compMark.assign(n+1, false);
        compMark[start] = true;
        que.push(start);
        while(!que.empty()){
            int cur = que.front();
            que.pop();
            comp.push_back(cur);
            for (int nb : G[cur]){
                if((cur == candidate_u && nb == candidate_v) || (cur == candidate_v && nb == candidate_u))
                    continue;
                if(!compMark[nb]){
                    compMark[nb] = true;
                    que.push(nb);
                }
            }
        }
        return comp;
    };
    vector<int> comp1 = get_component(candidate_v);
    vector<int> comp2 = get_component(candidate_u);
    if((int)comp1.size() != n/2 || (int)comp2.size() != n/2) return 0;

    bool uInComp1 = false, vInComp1 = false;
    for (int x : comp1){
        if(x == U) uInComp1 = true;
        if(x == V) vInComp1 = true;
    }
    if((uInComp1 && vInComp1) || (!uInComp1 && !vInComp1)) return 0;

    auto get_endpoints = [&](const vector<int>& comp) -> vector<int> {
        vector<bool> inComp(n+1, false);
        for (int x : comp) inComp[x] = true;
        vector<int> ends;
        for (int x : comp) {
            int cnt = 0;
            for (int nb : G[x]){
                if(!inComp[nb]) continue;
                cnt++;
            }
            if(cnt == 1) ends.push_back(x);
        }
        return ends;
    };
    vector<int> ends1 = get_endpoints(comp1);
    vector<int> ends2 = get_endpoints(comp2);
    if(ends1.size() != 2 || ends2.size() != 2) return 0;

    vector<int> endpoints;
    endpoints.insert(endpoints.end(), ends1.begin(), ends1.end());
    endpoints.insert(endpoints.end(), ends2.begin(), ends2.end());

    auto contains = [&](int x) -> bool {
        for (int y : endpoints)
            if(y == x) return true;
        return false;
    };
    if(!contains(U) || !contains(V)) return 0;

    int requiredDist = (n - 2) / 2;

    auto bfs_distance = [&](int start) -> vector<int> {
        vector<int> dist(n+1, INF);
        queue<int> que;
        dist[start] = 0;
        que.push(start);
        while(!que.empty()){
            int cur = que.front();
            que.pop();
            for (int nb : G[cur]){
                if(dist[nb] > dist[cur] + 1){
                    dist[nb] = dist[cur] + 1;
                    que.push(nb);
                }
            }
        }
        return dist;
    };
    vector<vector<int>> d(4);
    for (int i = 0; i < 4; i++){
        d[i] = bfs_distance(endpoints[i]);
    }

    vector<pair<pair<int,int>, pair<int,int>>> partitions = {
        {{0,1}, {2,3}},
        {{0,2}, {1,3}},
        {{0,3}, {1,2}}
    };
    bool ok = false;
    for(auto &part : partitions){
        auto p1 = part.first;
        auto p2 = part.second;
        if(d[p1.first][ endpoints[p1.second] ] == requiredDist &&
           d[p2.first][ endpoints[p2.second] ] == requiredDist)
            ok = true;
    }
    return ok;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout << (solve()? "Yes": "No") << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 20368
final_rank: 14

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};
vector<vector<int>> g(101010);
int sz[101010];
int x,y;
bool visited[101010];
int depth[101010];
void dfs(int v, int p) {
  sz[v] = 1;
  if(p != -1) depth[v] = depth[p] + 1;
  for(auto u: g[v]) {
    if(u == p) continue;
    dfs(u, v);
    sz[v] += sz[u];
  }
}
void dfs2(int v, int p) {
  visited[v] = true;
  for(auto u: g[v]) {
    if(u == p) continue;
    if(v == x && u == y) continue;
    if(v == y && u == x) continue;
    dfs2(u, v);
  }
}
void dfs3(int v, int p) {
  visited[v] = true;
  for(auto u: g[v]) {
    if(u == p) continue;
    if(v == x && u == y) continue;
    if(v == y && u == x) continue;
    dfs3(u, v);
    break;
  }
}
int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int n, u, v; cin >> n >> u >> v;
  u--; v--;
  REP(i,n-1) {
    int a, b; cin >> a >> b;
    a--; b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  if(n%2 == 1) {
    cout << "No" << endl;
    return 0;
  }
  dfs(0, -1);
  REP(i,n) {
    for(auto j: g[i]) {
      if(depth[i] > depth[j]) continue;
      if(sz[j] == n/2) {
        x = i, y = j;
        
      }
    }
  }
  //cout << x << " " << y << endl;
  memset(visited, false, sizeof(visited));
  dfs2(0,-1);
  if(visited[u] == visited[v]) cout << "No" << endl;
  else{
    memset(visited, false, sizeof(visited));
    dfs3(u, -1);
    dfs3(v, -1);
    REP(i,n) {
      if(!visited[i]) {
        cout << "No" << endl;
        return 0; 
      }
    }
    cout << "Yes" << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 15740
final_rank: 3

code_snippet: #include<algorithm>
#include<bitset>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << '\n'
#define endl '\n'

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-10;

constexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
constexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

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

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

bool solve2(int n, vector<int> s, vector< vector<int> > g){
    vector< vector<int> > ls(2);
    int tmp = 0;
    rep(i, n){
        if((int)g[i].size() == 1){
            int now = i;
            int pre = -1;
            while((int)ls[tmp].size() < (n >> 1)){
                ls[tmp].emplace_back(now);
                for(auto to : g[now]){
                    if(to != pre){
                        pre = now;
                        now = to;
                        break;
                    }
                }
            }
            ++tmp;
        }
    }

    rep(i, 2){
        if((ls[i].front() == s[0] || ls[i].back() == s[0]) && (ls[i ^ 1].front() == s[1] || ls[i ^ 1].back() == s[1])){
            return true;
        }
    }

    return false;
}

bool solve3(int n, vector<int> s, vector< vector<int> > g){
    if((int)g[s[0]].size() != 1 && (int)g[s[1]].size() != 1){
        return false;
    }

    vector< vector<int> > ls(3);
    int tmp = 0;
    rep(i, n){
        if((int)g[i].size() == 1){
            int now = i;
            int pre = -1;
            bool flag = true;
            while(flag){
                flag = false;
                ls[tmp].emplace_back(now);
                for(auto to : g[now]){
                    if(to != pre && (int)g[to].size() < 3){
                        pre = now;
                        now = to;
                        flag = true;
                        break;
                    }
                }
            }
            ++tmp;
        }
    }

    tmp = -1;
    rep(i, 3){
        if((int)ls[i].size() == (n >> 1)){
            rep(j, 2){
                if(ls[i].front() == s[j] || ls[i].back() == s[j]){
                    tmp = j;
                    break;
                }
            }
        }
    }

    if(tmp == -1){
        return false;
    }

    return (int)g[s[tmp ^ 1]].size() == 1;
}

bool solve4(int n, vector<int> s, vector< vector<int> > g){
    if((int)g[s[0]].size() != 1 || (int)g[s[1]].size() != 1){
        return false;
    }

    vector< vector<int> > ls(4);
    int tmp = 0;
    rep(i, n){
        if((int)g[i].size() == 1){
            int now = i;
            int pre = -1;
            bool flag = true;
            while(flag){
                flag = false;
                ls[tmp].emplace_back(now);
                if((int)g[now].size() >= 3){
                    break;
                }
                for(auto to : g[now]){
                    if(to != pre){
                        pre = now;
                        now = to;
                        flag = true;
                        break;
                    }
                }
            }
            ++tmp;
        }
    }

    bool flag = false;

    rep(i, 4){
        for(auto to : g[ls[i].back()]){
            rep(j, 4){
                if(to == ls[j].back()){
                    flag = true;
                    break;
                }
            }
            if(flag){
                break;
            }
        }
        if(flag){
            break;
        }
    }

    if(!flag){
        return false;
    }

    rep(i, 4){
        rep(j, i){
            if(ls[i].back() == ls[j].back() && (int)ls[i].size() + (int)ls[j].size() - 1 == (n >> 1)){
                rep(k, 4){
                    rep(l, k){
                        if(k == i || k == j || l == i || l == j){
                            continue;
                        }
                        if((ls[i].front() == s[0] || ls[j].front() == s[0]) && (ls[k].front() == s[1] || ls[l].front() == s[1])){
                            return true;
                        }
                    }
                }
            }
        }
    }

    return false;
}

bool solve(void){
    int n;
    cin >> n;

    if(n & 1){
        return false;
    }

    vector<int> s(2);
    rep(i, 2){
        cin >> s[i];
        --s[i];
    }

    vector< vector<int> > g(n);
    rep(i, n - 1){
        int a, b;
        cin >> a >> b;
        g[--a].emplace_back(--b);
        g[b].emplace_back(a);
    }

    int cnt = 0;
    rep(i, n){
        cnt += (int)g[i].size() == 1;
    }

    if(cnt == 2){
        return solve2(n, s, g);
    }else if(cnt == 3){
        return solve3(n, s, g);
    }else if(cnt == 4){
        return solve4(n, s, g);
    }

    return false;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    if(solve()){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }

    cout << flush;

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 14488
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long int;
using P = pair<ll,ll>;
constexpr int INF = 1<<30;

int main() {
    int n, s[2];
    cin >> n >> s[0] >> s[1];
    s[0]--; s[1]--;
    vector<vector<int> > g(n), d(2,vector<int>(n, INF));
    for(int i=0;i<n-1;i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    if(n%2){
        cout << "No" << endl;
        return 0;
    }
    for(int i=0;i<2;i++){
        d[i][s[i]] = 0;
        queue<int> que;
        que.push(s[i]);
        while(!que.empty()){
            int u = que.front();
            que.pop();
            for(auto v : g[u]){
                if(d[i][v] > d[i][u]+1){
                    d[i][v] = d[i][u]+1;
                    que.push(v);
                }
            }
        }
    }

    vector<bool> visited(n);
    visited[s[0]] = true;
    visited[s[1]] = true;
    int p[2] = {s[0], s[1]};
    bool ok = true;
    for(int i=0;i<n/2-1;i++){
        for(int j=0;j<2;j++){
            int ma = 0, nxt = -1;
            for(auto v : g[p[j]]){
                if(!visited[v] && d[j^1][v] > ma){
                    nxt = v;
                    ma = d[j^1][v];
                }
            }
            if(nxt == -1){
                ok = false;
                break;
            }
            visited[nxt] = true;
            p[j] = nxt;
        }
        if(!ok) break;
    }
    cout << (ok ? "Yes" : "No") << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 8952
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long int;
using P = pair<ll,ll>;
constexpr int INF = 1<<30;

int main() {
    int n, s[2];
    cin >> n >> s[0] >> s[1];
    s[0]--; s[1]--;
    vector<vector<int> > g(n), d(2,vector<int>(n, INF));
    for(int i=0;i<n-1;i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    if(n%2){
        cout << "No" << endl;
        return 0;
    }
    for(int i=0;i<2;i++){
        d[i][s[i]] = 0;
        queue<int> que;
        que.push(s[i]);
        while(!que.empty()){
            int u = que.front();
            que.pop();
            for(auto v : g[u]){
                if(d[i][v] < d[i][u]+1){
                    d[i][v] = d[i][u]+1;
                    que.push(v);
                }
            }
        }
    }

    vector<bool> visited(n);
    visited[s[0]] = true;
    visited[s[1]] = true;
    int p[2] = {s[0], s[1]};
    bool ok = true;
    for(int i=0;i<n/2-1;i++){
        for(int j=0;j<2;j++){
            int ma = 0, nxt = -1;
            for(auto v : g[p[j]]){
                if(!visited[v] && d[j^1][v] > ma){
                    nxt = v;
                    ma = d[j^1][v];
                }
            }
            if(nxt == -1){
                ok = false;
                break;
            }
            visited[nxt] = true;
            p[j] = nxt;
        }
        if(!ok) break;
    }
    cout << (ok ? "Yes" : "No") << endl;
    return 0;
}
accuracy: 0.43902439024390244
time_ms: 50
memory_kb: 9008
final_rank: 20

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 1577.cc: Courage Test
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef vector<int> vi;

/* global variables */

vi nbrs[MAX_N];
int lvs[MAX_N], crs[MAX_N], ps[MAX_N];

/* subroutines */

inline int nextp(int u, int up) {
  vi &nbru = nbrs[u];
  int v0 = nbru[0];
  if (v0 != up) return v0;
  return (nbru.size() > 1) ? nbru[1] : -1;
}

inline int findbr(int u, int &l, int &up) {
  l = 0, up = -1;
  while (nbrs[u].size() <= 2) {
    int v = nextp(u, up);
    up = u, u = v;
    l++;
  }
  return u;
}

inline bool matchp(int p0, int p1, int p2, int p3, int u, int v) {
  return
    ((p0 == u || p1 == u) && (p2 == v || p3 == v)) ||
    ((p0 == v || p1 == v) && (p2 == u || p3 == u));
}

/* main */

int main() {
  int n, u, v;
  scanf("%d%d%d", &n, &u, &v);
  u--, v--;

  if (n & 1) { puts("No"); return 0; }
  int hn = n / 2;
  
  for (int i = 1; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    nbrs[a].push_back(b);
    nbrs[b].push_back(a);
  }

  int lvn = 0, crn = 0;
  for (int i = 0; i < n; i++) {
    int sz = nbrs[i].size();
    if (sz == 1) lvs[lvn++] = i;
    else if (sz == 3) crs[crn++] = i;

    if (sz > 3 || lvn > 4 || crn > 2) { puts("No"); return 0; }
  }
  //printf("lvn=%d, crn=%d\n", lvn, crn);

  if (lvn == 2) {
    ps[0] = lvs[0];
    for (int i = 1, up = -1; i < n; i++) {
      ps[i] = nextp(ps[i - 1], up);
      up = ps[i - 1];
    }
    //for (int i = 0; i < n; i++) printf("%d ", ps[i]); putchar('\n');

    if (matchp(ps[0], ps[hn - 1], ps[hn], ps[n - 1], u, v)) {
      puts("Yes");
      return 0;
    }
  }
  else if (lvn == 3) {
    int ls[3], brs[3], bps[3];
    for (int i = 0; i < 3; i++) {
      brs[i] = findbr(lvs[i], ls[i], bps[i]);
      //printf("%d: lv=%d, l=%d, br=%d, bp=%d\n",
      //i, lvs[i], ls[i], brs[i], bps[i]);
    }

    for (int i = 0; i < 3; i++)
      if (ls[i] == hn) {
	int i1 = (i + 1) % 3, i2 = (i + 2) % 3;
	if (matchp(lvs[i], bps[i], lvs[i1], lvs[i2], u, v)) {
	  puts("Yes");
	  return 0;
	}
      }
  }
  else { // lvn == 4
    int ls[4], brs[4], bps[4];
    for (int i = 0; i < 4; i++) {
      brs[i] = findbr(lvs[i], ls[i], bps[i]);
      //printf("%d: lv=%d, l=%d, br=%d, bp=%d\n",
      //i, lvs[i], ls[i], brs[i], bps[i]);
    }

    if (brs[0] != brs[1])
      for (int i = 2; i < 4; i++)
	if (brs[0] == brs[i]) {
	  swap(lvs[1], lvs[i]);
	  swap(ls[1], ls[i]);
	  swap(brs[1], brs[i]);
	  swap(bps[1], bps[i]);
	  break;
	}
    if (ls[0] + ls[1] == hn - 1 && ls[2] + ls[3] == hn - 1 &&
	matchp(lvs[0], lvs[1], lvs[2], lvs[3], u, v)) {
      puts("Yes");
      return 0;
    }
  }

  puts("No");
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 9068
final_rank: 1

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

void dfs(const vector<vector<int>>&edges, int now, int pre,int rest,vector<int>&ans,bool flag) {

	ans[now]=true;
	if(rest==0)return;
	for (auto e : edges[now]) {
		if(e==pre)continue;
		else {
			if (flag&&edges[now].size() == 3) {
				flag=false;
				continue;
			}
			dfs(edges,e,now,rest-1,ans,flag);
			return ;
		}
	}
	return;
}

int main() {
	int N,S,T;cin>>N>>S>>T;
	S--;T--;

	vector<vector<int>>edges(N);
	for (int i = 0; i < N-1; ++i) {
		int a,b;cin>>a>>b;
		a--;b--;
		edges[a].push_back(b);
		edges[b].push_back(a);
	}
	bool ok=false;

	if (N == 2) {
		ok=true;
	}
	else if (N%2==1||edges[S].size() > 2 || edges[T].size() > 2) {
		ok=false;
	}
	else {
		if (count_if(edges.begin(), edges.end(), [](const vector<int>&v) {return v.size() >= 4; }) >= 1) {
			ok=false;
		}
		else if (count_if(edges.begin(), edges.end(), [](const vector<int>&v) {return v.size() >= 3; }) >= 3) {
			ok=false;
		}
		else {
			for (int i = 0; i < edges[S].size() * 2; ++i) {
				for (int j = 0; j < edges[T].size() * 2; ++j) {
					vector<int>vs, vt;
					vs.resize(N); vt.resize(N);
					vs[S] = true;
					vt[T] = true;
					dfs(edges, edges[S][i / 2], S, N / 2 - 2, vs, i % 2 == 0);
					dfs(edges, edges[T][j / 2], T, N / 2 - 2, vt, j % 2 == 0);

					bool aok = true;
					for (int k = 0; k < N; ++k) {
						if (!vs[k] && !vt[k])aok = false;
					}
					if (aok)ok = true;
				}
			}
		}
		
	}

	if(ok)cout<<"Yes"<<endl;
	else cout<<"No"<<endl;
	return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 9172
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const int N = 100010;
vector<int> G[N];

int par[N];
void dfs(int x){
    for(int e:G[x])if(e!=par[x]){
        par[e] = x;
        dfs(e);
    }
}

bool b[N]={};
int vis[N]={};

bool solve(){
    int n,u,v;
    scanf(" %d %d %d", &n, &u, &v);
    --u;
    --v;
    rep(i,n-1){
        int x,y;
        scanf(" %d %d", &x, &y);
        --x;
        --y;
        G[x].pb(y);
        G[y].pb(x);
    }

    // if(n%2==1) return false;

    par[u] = -1;
    dfs(u);

    int tt = v;
    while(tt!=u){
        b[tt] = true;
        tt = par[tt];
    }

    int R=1, B=1;
    vis[u] = 1;
    vis[v] = 2;

    int now = u;
    while(R<n/2){
        int ch=0;
        for(int e:G[now])if(par[now]!=e && !vis[e]) ++ch;

        if(ch==0) break;
        else if(ch==1){
            for(int e:G[now])if(par[now]!=e && !vis[e]){
                ++R;
                vis[e] = 1;
                now = e;
            }
        }
        else if(ch==2){
            for(int e:G[now])if(par[now]!=e && !vis[e]){
                if(!b[e]){
                    ++R;
                    vis[e] = 1;
                    now = e;
                    break;
                }
            }
        }
        else return false;
    }

    while(B<n/2){
        int ct = 0;
        for(int e:G[v])if(vis[e]==0) ++ct;
        if(ct==0) break;
        if(ct>=2) return false;

        for(int e:G[v])if(vis[e]==0){
            ++B;
            vis[e] = 2;
            v = e;
        }
    }

    rep(i,n)if(vis[i]==0) return false;
    return R==B;
}

int main(){
    cout << (solve()?"Yes":"No") << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 14240
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

const int N = 100005;
vector<int> g[N];
int cnt[N];
// P es[N];
vector<P> es;
int col[N];
int deg[N];

void dfs(ll v,ll pre){
  cnt[v]=1;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    dfs(nv,v);
    cnt[v]+=cnt[nv];
  }
}

void ddfs(ll v,ll pre,ll c){
  col[v]=c;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    ddfs(nv,v,c);
  }
}

bool solve(){
  int n,x,y;
  scanf("%d%d%d", &n, &x, &y);
  x--; y--;

  // memset(deg,0,sizeof(deg));

  rep(i,n-1){
    int a,b;
    scanf("%d%d", &a, &b);
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
    // es[i] = {a,b};
    es.push_back({a,b});
    deg[a]++;
    deg[b]++;
  }

  if(n==2) return true;
  if(n%2==1) return false;

  dfs(0,-1);
  memset(col,-1,sizeof(col));
  // rep(i,n-1){
    // ll a=es[i].fi, b=es[i].se;
  for(P p : es){
    ll a = p.first, b = p.second;
    if(cnt[a]<cnt[b]) swap(a,b);
    if(cnt[b]==n-cnt[b]){
      ddfs(a,b,0);
      ddfs(b,a,1);
      deg[a]--;
      deg[b]--;
      break;
    }
  }

  if(col[x]==col[y]) return false;

  int cnt1[2] = {0,0};
  rep(i,n){
    if(deg[i]>2) return false;
    if(deg[i]==1) cnt1[col[i]]++;
  }

  if(cnt1[0]!=2 || cnt1[1]!=2) return false;

  return (deg[x]==1 && deg[y]==1);
}

int main(){
  if(solve()) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 15824
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

const int N = 100005;
vector<int> g[N];
int cnt[N];
// P es[N];
vector<P> es;
int col[N];
int deg[N];

void dfs(ll v,ll pre){
  cnt[v]=1;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    dfs(nv,v);
    cnt[v]+=cnt[nv];
  }
}

void ddfs(ll v,ll pre,ll c){
  col[v]=c;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    ddfs(nv,v,c);
  }
}

bool solve(){
  int n,x,y;
  scanf("%d%d%d", &n, &x, &y);
  x--; y--;

  // memset(deg,0,sizeof(deg));

  rep(i,n-1){
    int a,b;
    scanf("%d%d", &a, &b);
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
    // es[i] = {a,b};
    es.push_back({a,b});
    deg[a]++;
    deg[b]++;
  }

  if(n==2) return true;
  if(n%2==1) return false;

  dfs(0,-1);
  memset(col,-1,sizeof(col));
  rep(i,n-1){
    ll a=es[i].fi, b=es[i].se;
    if(cnt[a]<cnt[b]) swap(a,b);
    if(cnt[b]==n-cnt[b]){
      ddfs(a,b,0);
      ddfs(b,a,1);
      deg[a]--;
      deg[b]--;
      break;
    }
  }

  if(col[x]==col[y]) return false;

  int cnt1[2] = {0,0};
  rep(i,n){
    if(deg[i]>2) return false;
    if(deg[i]==1) cnt1[col[i]]++;
  }

  if(cnt1[0]!=2 || cnt1[1]!=2) return false;

  return (deg[x]==1 && deg[y]==1);
}

int main(){
  if(solve()) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 15828
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

const int N = 100005;
vector<int> g[N];
int cnt[N];
P es[N];
int col[N];
int deg[N];

void dfs(ll v,ll pre){
  cnt[v]=1;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    dfs(nv,v);
    cnt[v]+=cnt[nv];
  }
}

void ddfs(ll v,ll pre,ll c){
  col[v]=c;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    ddfs(nv,v,c);
  }
}

bool solve(){
  int n,x,y;
  scanf("%d%d%d", &n, &x, &y);
  x--; y--;

  // memset(deg,0,sizeof(deg));

  rep(i,n-1){
    int a,b;
    scanf("%d%d", &a, &b);
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
    es[i] = {a,b};
    deg[a]++;
    deg[b]++;
  }

  if(n==2) return true;
  if(n%2==1) return false;

  dfs(0,-1);
  memset(col,-1,sizeof(col));
  rep(i,n-1){
    ll a=es[i].fi, b=es[i].se;
    if(cnt[a]<cnt[b]) swap(a,b);
    if(cnt[b]==n-cnt[b]){
      ddfs(a,b,0);
      ddfs(b,a,1);
      deg[a]--;
      deg[b]--;
      break;
    }
  }

  if(col[x]==col[y]) return false;

  int cnt1[2] = {0,0};
  rep(i,n){
    if(deg[i]>2) return false;
    if(deg[i]==1) cnt1[col[i]]++;
  }

  if(cnt1[0]!=2 || cnt1[1]!=2) return false;

  return (deg[x]==1 && deg[y]==1);
}

int main(){
  if(solve()) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 16024
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

const int N = 100005;
vector<int> g[N];
int cnt[N];
P es[N];
int col[N];
int deg[N];

void dfs(ll v,ll pre){
  cnt[v]=1;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    dfs(nv,v);
    cnt[v]+=cnt[nv];
  }
}

void ddfs(ll v,ll pre,ll c){
  col[v]=c;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    ddfs(nv,v,c);
  }
}

bool solve(){
  int n,x,y;
  scanf("%d%d%d", &n, &x, &y);
  x--; y--;

  memset(deg,0,sizeof(deg));

  rep(i,n-1){
    int a,b;
    scanf("%d%d", &a, &b);
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
    es[i] = {a,b};
    deg[a]++;
    deg[b]++;
  }

  if(n==2) return true;
  if(n%2==1) return false;

  dfs(0,-1);
  memset(col,-1,sizeof(col));
  rep(i,n-1){
    ll a=es[i].fi, b=es[i].se;
    if(cnt[a]<cnt[b]) swap(a,b);
    if(cnt[b]==n-cnt[b]){
      ddfs(a,b,0);
      ddfs(b,a,1);
      deg[a]--;
      deg[b]--;
      break;
    }
  }

  if(col[x]==col[y]) return false;

  int cnt1[2] = {0,0};
  rep(i,n){
    if(deg[i]>2) return false;
    if(deg[i]==1) cnt1[col[i]]++;
  }

  if(cnt1[0]!=2 || cnt1[1]!=2) return false;

  return (deg[x]==1 && deg[y]==1);
}

int main(){
  if(solve()) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 16088
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef int ll;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

const int N = 100005;
vector<int> g[N];
int cnt[N];
P es[N];
int col[N];
int deg[N];

void dfs(ll v,ll pre){
  cnt[v]=1;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    dfs(nv,v);
    cnt[v]+=cnt[nv];
  }
}

void ddfs(ll v,ll pre,ll c){
  col[v]=c;
  for(ll nv : g[v]){
    if(nv==pre)continue;
    ddfs(nv,v,c);
  }
}

bool solve(){
  int n,x,y;
  scanf("%d%d%d", &n, &x, &y);
  x--; y--;

  memset(deg,0,sizeof(deg));

  rep(i,n-1){
    int a,b;
    scanf("%d%d", &a, &b);
    a--;b--;
    g[a].push_back(b);
    g[b].push_back(a);
    es[i] = {a,b};
    deg[a]++;
    deg[b]++;
  }

  if(n==2) return true;
  if(n%2==1) return false;

  dfs(0,-1);
  memset(col,-1,sizeof(col));
  rep(i,n-1){
    ll a=es[i].fi, b=es[i].se;
    if(cnt[a]<cnt[b]) swap(a,b);
    if(cnt[b]==n-cnt[b]){
      ddfs(a,b,0);
      ddfs(b,a,1);
      deg[a]--;
      deg[b]--;
      break;
    }
  }

  if(col[x]==col[y]) return false;

  int cnt1[2] = {0,0};
  rep(i,n){
    if(deg[i]>2) return false;
    if(deg[i]==1) cnt1[col[i]]++;
  }

  if(cnt1[0]!=2 || cnt1[1]!=2) return false;

  return (deg[x]==1 && deg[y]==1);
}

int main(){
  if(solve()) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 16884
final_rank: 13

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 100000

int N,A,B,num_A,num_B;
bool check[NUM];
vector<int> G[NUM];

bool recursive(int side_A,int side_B){

	if(num_A == N/2 && num_B == N/2)return true;

	for(int i = 0; i < G[side_A].size(); i++){

		if(check[G[side_A][i]])continue;
		check[G[side_A][i]] = true;

		num_A++;
		for(int k = 0; k < G[side_B].size(); k++){

			if(check[G[side_B][k]])continue;
			check[G[side_B][k]] = true;

			num_B++;
			if(recursive(G[side_A][i],G[side_B][k]))return true;

			check[G[side_B][k]] = false;
			num_B--;
		}
		check[G[side_A][i]] = false;
		num_A--;

	}
    return false;
}


int main(){

	scanf("%d %d %d",&N,&A,&B);
	A--;
	B--;
	num_A = num_B = 1;

    for(int i = 0; i < N; i++)check[i] = false;

    check[A] = true;
    check[B] = true;

    int from,to;

    for(int loop = 0; loop < N-1; loop++){
    	scanf("%d %d",&from,&to);
    	from--;
    	to--;
    	G[from].push_back(to);
    	G[to].push_back(from);
    }

    if(N%2 != 0){
        printf("No\n");
        return 0;
    }

    int num_3 = 0;
    for(int i = 0; i < N; i++){
    	if(G[i].size() >= 4){
    		printf("No\n");
    		return 0;
    	}
    	if(G[i].size() == 3){
    		num_3++;
    		if(num_3 > 2){
    			printf("No\n");
    			return 0;
    		}
    	}
    }

    if(recursive(A,B)){
    	printf("Yes\n");
    }else{
    	printf("No\n");
    }
}
accuracy: 1
time_ms: 20
memory_kb: 12704
final_rank: 2

code_snippet: #include <iostream>
#include <vector>
#include <cstdio>
  
template<class T>
using Graph = std::vector<std::vector<T>>;
  
bool dfs(Graph<int> &G, int step, int u, int v,
         std::vector<bool> &visited)
{
    int n = G.size();
    int x = (step < n / 2 ? u : v);
    if (step == n - 1) {
        return true;
    }
      
    bool ret = false;
    for (int next : G[x]) {
        if (!visited[next]) {
            visited[next] = true;
  
            if (step < n / 2) {
                ret |= dfs(G, step + 1, next, v, visited);
            } else {
                ret |= dfs(G, step + 1, u, next, visited);
            }           
        }        
    }
  
    return ret;
}
  
bool solve(Graph<int> &G, int u, int v)
{
    int n = G.size();
    if (n & 1) {
        return false;
    }
  
    std::vector<bool> visited(n, false);
    visited[u] = visited[v] = true;
    return dfs(G, 1, u, v, visited);
}
  
int main()
{
    int n, u, v;
    scanf("%d %d %d", &n, &u, &v);
    u--; v--;
      
    Graph<int> G(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
  
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
  
    printf("%s\n", (solve(G, u, v) ? "Yes" : "No"));
      
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 17584
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <cstdio>
 
template<class T>
using Graph = std::vector<std::vector<T>>;
 
bool dfs(Graph<int> &G, int step, int u, int v,
         std::vector<bool> &visited)
{
    int n = G.size();
    int x = (step < n / 2 ? u : v);
    if (step == n - 1) {
        return true;
    }
     
    bool ret = false;
    for (int next : G[x]) {
        if (!visited[next]) {
            visited[next] = true;
 
            if (step < n / 2) {
                ret |= dfs(G, step + 1, next, v, visited);
            } else {
                ret |= dfs(G, step + 1, u, next, visited);
            }           
        }        
    }
 
    return ret;
}
 
bool solve(Graph<int> &G, int u, int v)
{
    int n = G.size();
    if (n & 1) {
        return false;
    }
 
    std::vector<bool> visited(n, false);
    visited[u] = visited[v] = true;
    return dfs(G, 1, u, v, visited);
}
 
int main()
{
    int n, u, v;
    scanf("%d %d %d", &n, &u, &v);
    u--; v--;
     
    Graph<int> G(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
 
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
 
    printf("%s\n", (solve(G, u, v) ? "Yes" : "No"));
     
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 17640
final_rank: 16

code_snippet: #include <iostream>
#include <vector>
 
template<class T>
using Graph = std::vector<std::vector<T>>;
 
bool dfs(Graph<int> &G, int step, int u, int v,
         std::vector<bool> &visited)
{
    int n = G.size();
    int x = (step < n / 2 ? u : v);
    if (step == n - 1) {
        return true;
    }
     
    bool ret = false;
    for (int next : G[x]) {
        if (!visited[next]) {
            visited[next] = true;
 
            if (step < n / 2) {
                ret |= dfs(G, step + 1, next, v, visited);
            } else {
                ret |= dfs(G, step + 1, u, next, visited);
            }           
        }        
    }
 
    return ret;
}
 
bool solve(Graph<int> &G, int u, int v)
{
    int n = G.size();
    if (n & 1) {
        return false;
    }
 
    std::vector<bool> visited(n, false);
    visited[u] = visited[v] = true;
    return dfs(G, 1, u, v, visited);
}
 
int main()
{
    int n, u, v;
    std::cin >> n >> u >> v;
    u--; v--;
     
    Graph<int> G(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        std::cin >> a >> b;
        a--; b--;
 
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
 
    std::cout << (solve(G, u, v) ? "Yes" : "No") << std::endl;
     
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 17564
final_rank: 18

code_snippet: #include <iostream>
#include <vector>

template<class T>
using Graph = std::vector<std::vector<int>>;

bool dfs(Graph<int> &G, int step, int u, int v,
         std::vector<bool> &visited)
{
    int n = G.size();
    int x = (step < n / 2 ? u : v);
    if (step == n - 1) {
        return true;
    }
    
    bool ret = false;
    for (int next : G[x]) {
        if (!visited[next]) {
            visited[next] = true;

            if (step < n / 2) {
                ret |= dfs(G, step + 1, next, v, visited);
            } else {
                ret |= dfs(G, step + 1, u, next, visited);
            }           
        }        
    }

    return ret;
}

bool solve(Graph<int> &G, int u, int v)
{
    int n = G.size();
    if (n & 1) {
        return false;
    }

    std::vector<bool> visited(n, false);
    visited[u] = visited[v] = true;
    return dfs(G, 1, u, v, visited);
}

int main()
{
    int n, u, v;
    std::cin >> n >> u >> v;
    u--; v--;
    
    Graph<int> G(n);
    for (int i = 0; i < n - 1; i++) {
        int a, b;
        std::cin >> a >> b;
        a--; b--;

        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    std::cout << (solve(G, u, v) ? "Yes" : "No") << std::endl;
    
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 17584
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
vector<P> v[100001];
int n;
int solve(int i,int p) {
  if(v[i].size()==1&&v[i][0].first==p) {
    v[i][0].second=n-1;
    return 1;
  }
  int sum=0,k=-1;
  for(int j=0; j<v[i].size(); j++) {
    if(v[i][j].first!=p) {
      if(v[i][j].second==0) {
        v[i][j].second=solve(v[i][j].first,i);
        sum+=v[i][j].second;
      } else sum+=v[i][j].second;
    } else k=j;
  }
  if(k!=-1) v[i][k].second=n-1-sum;
  return sum+1;
}
int d[111111],d2[111111],f2;
void dfs(int x,int k,int y) {
  d[x]=k;
  for(int i=0; i<v[x].size(); i++) {
    if(d[v[x][i].first]==-1&&v[x][i].first!=y) dfs(v[x][i].first,k+1,y);
  }
}
void D(int s,int t) {
  memset(d,-1,sizeof(d));
  dfs(s,0,t);
  int M=-1,x,f=0;
  for(int i=1; i<=n; i++) {
    if(d[i]>M) M=d[i],x=i;
  }
  memset(d,-1,sizeof(d));
  dfs(x,0,t);
  for(int i=1; i<=n; i++) f|=d[i]==n/2-1;
  if(!f) f2=0;
}
bool check(int s, int t, int n) {return (!d2[s]||d2[s]==n/2-1)&&(!d2[t]||d2[t]==n/2-1);}

int main() {
  int s,t;
  cin >> n >> s >> t;
  for(int i=0,x,y; i<n-1; i++) {
    cin >> x >> y;
    v[x].push_back(P(y,0));
    v[y].push_back(P(x,0));
  }
  for(int i=1; i<=n; i++) {
    if(v[i].size()==1) {
      solve(i,-1);
      break;
    }
  }
  int x=-1,y=-1;
  for(int i=1; i<=n; i++) {
    for(int j=0; j<v[i].size(); j++) {
      if(v[i][j].second==n/2) {x=i,y=v[i][j].first;break;}
    }
  }
  if(x!=-1) {
    memset(d2,-1,sizeof(d2));f2=1;
    D(x,y);
    for(int i=1; i<=n; i++) d2[i]=max(d2[i],d[i]);
    if(d2[s]!=-1&&d2[t]!=-1||d2[s]==-1&&d2[t]==-1) f2=0;
    D(y,x);
    for(int i=1; i<=n; i++) d2[i]=max(d2[i],d[i]);
  }
  if(n%2==0&&f2&&check(s,t,n)) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 17260
final_rank: 17