#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef pair PII; typedef vector VI; typedef vector VPII; typedef pair PLL; typedef pair PIL; typedef pair PLI; typedef double DB; typedef long double LD; #define pb push_back #define all(x) (x).begin(), (x).end() #define bit(x) (1 << (x)) #define bitl(x) (1LL << (x)) #define sqr(x) ((x) * (x)) #define sz(x) ((int)(x.size())) #define counti(x) (__builtin_popcount(x)) #define countl(x) (__builtin_popcountll(x)) #define rep(i, n) for (int (i) = 0; (i) < (int)(n); ++(i)) #define X first #define Y second template inline void chkmax(T& x, U y) { if (x < y) x = y; } template inline void chkmin(T& x, U y) { if (y < x) x = y; } int get() { char c; while (c = getchar(), (c < '0' || c > '9') && (c != '-')); bool flag = (c == '-'); if (flag) c = getchar(); int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + c - 48; c = getchar(); } return flag ? -x : x; } void output(int x) { if (x < 0) { putchar('-'); x = -x; } int len = 0, data[20]; while (x) { data[len++] = x % 10; x /= 10; } if (!len) data[len++] = 0; while (len--) putchar(data[len] + 48); putchar('\n'); } #define MX 1005 VI con[MX]; int chk[MX], T; int ring[MX], prev[MX], Q[MX]; int n; void DFS(int id) { int i; for (i = 0; i < con[id].size(); i++) { int j = con[id][i]; if (chk[j] == T) continue; chk[j] = T; DFS(j); } } int Find(int id, int prev) { int i; for (i = 0; i < con[id].size(); i++) { int j = con[id][i]; if (j == prev) continue; if (chk[j] == T) return j; if (chk[j] < T) chk[j] = T; return Find(j, id); } } int calc(int id, int des, int prev) { int i; if (id == des) return 0; for (i = 0; i < con[id].size(); i++) { int j = con[id][i]; if (j == prev) continue; return calc(j, des, id) + 1; } } bool BFS(int st, int en) { int hd, tl; hd = tl = 0; Q[tl++] = st; T++; chk[st] = T; while (hd < tl) { int x = Q[hd++]; if (x == en) break; int i; for (i = 0; i < con[x].size(); i++) { int j = con[x][i]; if (chk[j] == T) continue; chk[j] = T; prev[j] = x; Q[tl++] = j; } } VI v; int i; for (i = en; i != st; i = prev[i]) { if (con[i].size() == 3) v.pb(i); } if (v.size() == 0) return true; if (v.size() == 1) { int x = v[0]; for (int j = 0; j < con[x].size(); j++) if (con[x][j] == x) return true; return false; } if (v.size() > 2) return false; int x = v[0]; for (int j = 0; j < con[x].size(); j++) if (con[x][j] == v[1]) return true; return false; } int main() { while (scanf("%d", &n) > 0) { int u, v; T++; int i; for (i = 0; i < n; i++) con[i].clear(); for (i = 0; i < n; i++) { u = get(), v = get(); u--, v--; if (u != v) con[u].pb(v), con[v].pb(u); else con[u].pb(v); } chk[0] = T; DFS(0); for (i = 0; i < n; i++) if (chk[i] < T || con[i].size() > 3) break; if (i < n) { puts("NO"); continue; } int c = 0; VI V; for (i = 0; i < n; i++) if (con[i].size() == 1) c++, V.pb(i); if (c == 0 || c == 1) { puts("YES"); continue; } if (c > 2) { puts("NO"); continue; } if (BFS(V[0], V[1])) puts("YES"); else puts("NO"); } return 0; }