solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int sc(int &x) { return scanf("%d", &x); } int sc(unsigned int &x) { return scanf("%u", &x); } int sc(long long &x) { return scanf("%lld", &x); } int sc(unsigned long long &x) { return scanf("%llu", &x); } int sc(double &x) { return scanf("%lf", &x); } int sc(long double &x) { return scanf("%Lf", &x); } int sc(char *x) { return scanf("%s", x); } int sc(char &x) { return scanf("%c", &x); } template <typename T, typename... Args> void sc(T &v, Args &...args) { sc(v); sc(args...); } void pr(const int &x) { printf("%d", x); } void pr(const unsigned int &x) { printf("%u", x); } void pr(const long long &x) { printf("%lld", x); } void pr(const unsigned long long &x) { printf("%llu", x); } void pr(const double &x) { printf("%.10lf", x); } void pr(const long double &x) { printf("%.10Lf", x); } void pr(const char *const &x) { printf("%s", x); } void pr(char *const &x) { printf("%s", x); } void pr(const char &x) { printf("%c", x); } template <typename T, typename... Args> void pr(T const &v, Args const &...args) { pr(v); pr(' '); pr(args...); } const long long MOD = 1e9 + 7; inline void _add(long long &a, long long b) { a = (a + b) % MOD; } inline void _sub(long long &a, long long b) { a = (a + MOD - b) % MOD; } inline void _mul(long long &a, long long b) { a = (long long)a * b % MOD; } inline long long _Add(long long a, long long b) { return (a + b) % MOD; } inline long long _Sub(long long a, long long b) { return (a + MOD - b) % MOD; } inline long long _Mul(long long a, long long b) { return a * b % MOD; } int deg[100100], n; int main() { sc(n); int u, v; for (int i = 1, lim = n - 1; i <= lim; ++i) { sc(u, v); deg[u]++; deg[v]++; } for (int i = 1, lim = n; i <= lim; ++i) { if (deg[i] == 2) { pr("NO"); return 0; } } pr("YES"); pr("\n"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 100005; long long degree[N]; int main() { long long n, i, v, u; cin >> n; for (i = 0; i < n - 1; i++) { cin >> v >> u; degree[v]++; degree[u]++; } for (i = 1; i <= n; i++) { if (degree[i] == 2) { cout << "NO\n"; ; return 0; } } cout << "YES\n"; return 0; }
10
CPP
#include <bits/stdc++.h> const int inff = 0x3f3f3f3f; const double eqs = 1e-9; const double E = 2.718281828459; const double pi = acos(-1.0); using namespace std; const int maxn = 1e5 + 5; int in[maxn]; int main() { int n, x, y; cin >> n; for (int i = 1; i <= n; i++) in[i] = 0; for (int i = 1; i < n; i++) { scanf("%d %d", &x, &y); in[x]++, in[y]++; } for (int i = 1; i <= n; i++) if (in[i] == 2) { puts("NO"); return 0; } puts("YES"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 5; int n; vector<int> G[MAX_N]; bool ans = true; int main() { scanf("%d", &n); for (int i = 0, t1, t2; i < n - 1; i++) { scanf("%d%d", &t1, &t2); G[t1].emplace_back(t2); G[t2].emplace_back(t1); } for (int i = 1; i <= n; i++) if (G[i].size() == 2) { ans = false; break; } puts(ans ? "YES" : "NO"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int> > a; void Input() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; a.resize(n); for (int i = 1; i <= n - 1; i++) { int x, y; cin >> x >> y; x--; y--; a[x].push_back(y); a[y].push_back(x); } } void Process() { for (int i = 0; i <= n - 1; i++) if (a[i].size() == 2) { cout << "NO"; return; } cout << "YES"; } int main() { Input(); Process(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long br[200002]; long long n, x, y; int main() { cin >> n; for (long long i = 1; i < n; i++) { cin >> x >> y; br[x]++; br[y]++; } for (long long i = 1; i <= n; i++) { if (br[i] == 2) { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const unsigned long long hash1 = 201326611; const unsigned long long hash2 = 50331653; const int N = 200000 + 10; const int M = 20; vector<int> g[N]; int n, vis[N]; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; int u, v; for (int i = 1; i < n; i++) { cin >> u >> v; vis[u]++; vis[v]++; } int flag = 0; for (int i = 1; i <= n; i++) { if (vis[i] == 2) flag = 1; } if (flag) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
10
CPP
n = int(input()) l = [0] * n for i in range(n - 1): u , v = map(int , input().split()) l[u - 1] += 1 l[v - 1] += 1 for i in l: if i == 2: print("NO") exit() print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<long long> adj[200005]; long long c = 0; bool vis[200005]; vector<long long> lf; void dfs(long long v) { if (vis[v]) { return; } vis[v] = true; if (adj[v].size() == 1) { c++; lf.push_back(v); } for (auto u : adj[v]) { dfs(u); } } int main() { long long n; cin >> n; long long x, y; for (int i = 0; i < n - 1; i++) { cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } dfs(x); for (int i = 1; i <= n; i++) { if (adj[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
10
CPP
from sys import stdin, stdout, exit n = int(input()) graph = [[] for i in range(n)] for i in range(n-1): u, v = map(int, stdin.readline().split()) graph[u-1].append(v-1) graph[v-1].append(u-1) for i in range(n): if len(graph[i]) == 2: print("NO") exit() print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, u, v; cin >> n; vector<vector<int>> g(n); for (int i = 0; i < n - 1; i++) { cin >> u >> v; g[u - 1].push_back(v - 1); g[v - 1].push_back(u - 1); } for (int i = 0; i < n; i++) { if (g[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 1; int d[MAX]; void solve() { int n; cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; d[u]++; d[v]++; } for (int i = 1; i <= n; ++i) { if (d[i] == 2) { cout << "NO\n"; return; } } cout << "YES\n"; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; vector<int> a[100010]; int main() { int n, m; scanf("%d", &n); int x, y; for (int i = 0; i < n - 1; i++) { scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } if (n == 2) { printf("YES"); return 0; } int f = 1; for (int i = 1; i <= n; i++) { if (a[i].size() == 2) { f = 0; break; } } if (f) printf("YES"); else printf("NO"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int O = 1e6; const int mod = 1e9 + 7; const int maxn = 1e6 + 5; const double PI = acos(-1.0); const double E = 2.718281828459; const double eps = 1e-8; struct dd { int to, val, next; } e[maxn]; int head[maxn], cnt = 0; void add(int u, int v) { e[cnt] = {v, 0, head[u]}; head[u] = cnt++; } int main() { memset(head, -1, sizeof(head)); int n; scanf("%d", &n); int num[maxn]; memset(num, 0, sizeof(num)); for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d%d", &u, &v); num[u]++; num[v]++; } bool flag = true; for (int i = 1; i <= n; i++) { if (num[i] == 2) flag = false; } flag ? printf("YES\n") : printf("NO\n"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; vector<long long> vin(long long n) { vector<long long> a(n); for (long long i = 0; i < n; i += 1) cin >> a[i]; return a; } long long intin() { long long n; cin >> n; return n; } char charin() { char a; cin >> a; return a; } string strin() { string s; cin >> s; return s; } long long factorial(long long n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, u, v; cin >> n; vector<long long> counter(n + 1, 0); for (long long i = 0; i < n - 1; i += 1) { cin >> u >> v; counter[u]++; counter[v]++; } for (auto i : counter) { if (i == 2) { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
10
CPP
from collections import Counter n = int(input()) x = [] for _ in range(n-1): x.extend(input().split()) print('NO' if 2 in Counter(x).values() else 'YES')
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; vector<vector<long long>> v(n); for (long long i = 0; i < n - 1; i++) { long long a, b; cin >> a >> b; v[a - 1].push_back(b - 1); v[b - 1].push_back(a - 1); } for (long long i = 0; i < n; i++) { if (v[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; }
10
CPP
import sys, os from io import BytesIO, IOBase from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log from collections import defaultdict as dd, deque from heapq import merge, heapify, heappop, heappush, nsmallest from bisect import bisect_left as bl, bisect_right as br, bisect # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") stdin, stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) mod = pow(10, 9) + 7 mod2 = 998244353 def inp(): return stdin.readline().strip() def iinp(): return int(inp()) def out(var, end="\n"): stdout.write(str(var)+"\n") def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end) def lmp(): return list(mp()) def mp(): return map(int, inp().split()) def smp(): return map(str, inp().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)] def remadd(x, y): return 1 if x%y else 0 def ceil(a,b): return (a+b-1)//b S1 = 'abcdefghijklmnopqrstuvwxyz' S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def isprime(x): if x<=1: return False if x in (2, 3): return True if x%2 == 0: return False for i in range(3, int(sqrt(x))+1, 2): if x%i == 0: return False return True n = iinp() edges = l1d(n+1) for i in range(n-1): x, y = mp() edges[x] += 1 edges[y] += 1 flg = True for i in edges: if i==2: flg = False break print("YES" if flg else "NO")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, x, y, d[100005]; vector<long long> a[100005]; void solve() { cin >> n; m = n - 1; while (m--) { cin >> x >> y; a[x].push_back(y); a[y].push_back(x); d[x]++, d[y]++; } for (__typeof((n + 1)) i = (1); i < (n + 1); i++) { if (d[i] == 2) { cout << "NO" << "\n"; return; }; } { cout << "YES" << "\n"; return; }; } void prep() {} int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; prep(); cout << fixed << setprecision(12); while (t--) solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, l, r, cnt = 0; cin >> n; vector<long long> v[n + 1]; bool ok = true; for (long long i = 0; i < n - 1; ++i) { cin >> l >> r; v[l].push_back(r); v[r].push_back(l); } for (long long i = 1; i <= n; ++i) { if (v[i].size() == 2) ok = false; } cout << ((ok) ? "YES\n" : "NO\n"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; vector<int> AL[100005]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d", &u); scanf("%d", &v); AL[u].push_back(v); AL[v].push_back(u); } long long int l = 0; int fl = 0; for (int i = 1; i <= n; i++) { if (AL[i].size() == 2) { fl = 1; } } if (fl == 0) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int g[100010]; int f; int main() { int n, i, j, ok = 0, x, y; scanf("%d", &n); for (i = 1; i < n; i++) { scanf("%d%d", &x, &y); g[x]++; g[y]++; } for (i = 1; i <= n; i++) { if (g[i] == 1) f++; if (g[i] == 2) ok = 1; } f = (f * (f - 1)) / 2; if (ok) printf("NO"); else printf("YES"); }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<int> adj[n]; int u, v; for (int i = 0; i < n - 1; i++) { cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 0; i < n; i++) { if (adj[i].size() == 2) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, root, Over; vector<int> P[MAXN]; int main() { scanf("%d", &n); for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); P[u].push_back(v); P[v].push_back(u); } for (int i = 1; i <= n; i++) if (P[i].size() == 2) { printf("NO\n"); return 0; } printf("YES\n"); }
10
CPP
#include <bits/stdc++.h> using namespace std; long long in[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, x, y; cin >> n; for (long long i = 0; i < n - 1; i++) { cin >> x >> y; in[x]++; in[y]++; } long long cnt = 0; bool first = 1; for (long long i = 1; i < n + 1; i++) { if (in[i] == 1) cnt++; if (in[i] == 2) { first = 0; break; } } if (first == 0) { cout << "NO" << "\n"; return 0; } if ((cnt * (cnt - 1)) / 2 >= n - 1) { cout << "YES" << "\n"; } else { cout << "NO" << "\n"; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, u, v, z = 0, f[100001]; int main() { for (int i = 1; i <= 10000; i++) z++; for (int i = 1; i <= 100; i++) z--; cin >> n; for (int i = 1; i < n; i++) { cin >> u >> v; f[u]++; f[v]++; } for (int i = 1; i <= n; i++) if (f[i] != 1 && f[i] <= 2) { cout << "NO"; return 0; } cout << "YES"; return 0; }
10
CPP
n=int(input()) dic={} for x in range(n-1): a,b=list(map(int,input().split())) if a in dic: dic[a]+=1 else: dic[a]=1 if b in dic: dic[b]+=1 else: dic[b]=1 flag=5 for it in dic: if dic[it]==2: flag=6 break if flag==6: print("NO") else: print("YES")
10
PYTHON3
n = int(input()) deg = [0 for i in range(n+1)] for i in range(n-1): a,b = [int(j) for j in input().split(" ")] deg[a] += 1 deg[b] += 1 ans = "YES" for i in range(n+1): if deg[i] == 2: ans = "NO" break print(ans)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int n, x, y; vector<int> g[100010]; int main() { cin >> n; for (int i = 0; i < n - 1; i++) { scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) { if (g[i].size() == 2) { puts("NO"); return 0; } } puts("YES"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long int n; vector<long long int> G[200020]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } for (int i = 1; i <= n; i++) { if (G[i].size() == 2) return cout << "NO", 0; } cout << "YES"; }
10
CPP
#include <bits/stdc++.h> using namespace std; int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1}; long long bigmod(long long x, long long p) { long long res = 1; while (p) { if (p & 1) res = (res * x) % 998244353; x = (x * x) % 998244353; p >>= 1; } return res; } int32_t main() { ios_base::sync_with_stdio(false); int n, m; cin >> n; m = n - 1; vector<int> cnt(n); while (m--) { int u, v; cin >> u >> v; cnt[u - 1]++; cnt[v - 1]++; } int i = 0; for (; i < n; i++) if (cnt[i] == 2) break; cout << (i < n ? "NO" : "YES\n"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int g[100005]; int main() { int n; memset(g, 0, sizeof(g)); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u]++; g[v]++; } for (int i = 1; i <= n; i++) { if (g[i] == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; ; const double eps = 1e-8; const int mod = 1e9 + 7; const int maxn = 3e5 + 5; const int INF = 0x3f3f3f3f; const long long INFLL = 0x3f3f3f3f3f3f3f3f; struct EDGE { int v, nxt; } edge[maxn << 1]; int head[maxn], tot; void add_edge(int u, int v) { edge[tot].v = v, edge[tot].nxt = head[u], head[u] = tot++; } int degree[maxn]; int main() { memset(head, -1, sizeof(head)); tot = 0; int n; scanf("%d", &n); for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); degree[u]++; degree[v]++; } bool flag = true; for (int i = 1; i <= n; i++) { if (degree[i] == 2) { flag = false; } } if (flag) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
10
CPP
n = int(input()) neigh = [[] for _ in range(n)] for i in range(n-1): u, v = map(int, input().split()) u -= 1 v -= 1 neigh[u].append(v) neigh[v].append(u) no = False for i in range(n): if len(neigh[i]) == 2: no = True if no: print('NO') else: print('YES')
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int BASE = 1e9 + 7; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const int N = 1e5 + 1; vector<int> a[N]; int pa[N], n; int F[N]; void DFS(int p, int u) { bool Check = true; for (__typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++) { int v = *it; if (v == p) continue; Check = false; DFS(u, v); pa[v] = u; F[u] = F[u] + F[v]; } F[u] += Check; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; int u, v; for (int i = 1; i < n; i++) { cin >> u >> v; a[u].push_back(v); a[v].push_back(u); } bool Check = true; for (int u = 1; u <= n; u++) { for (__typeof(a[u].begin()) it = a[u].begin(); it != a[u].end(); it++) { int v = *it; if (a[u].size() == 1) { if (a[v].size() == 1) continue; if (a[v].size() < 3) { Check = false; } } else if (a[v].size() == 1) { if (a[u].size() == 1) continue; if (a[u].size() < 3) { Check = false; } } else { if (a[v].size() < 3 || a[u].size() < 3) { Check = false; } } } } cout << (Check ? "YES" : "NO"); }
10
CPP
import sys input = lambda: sys.stdin.readline().strip() n = int(input()) graph = {} for i in range(1, n+1): graph[i] = [] for i in range(1, n): a, b = map(int, input().split()) graph[a].append(b) graph[b].append(a) for i in range(1, n+1): if len(graph[i])==2: print("NO") break else: print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } std::vector<long long> edge[100045]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t, i, j, n, m, k, q, temp, x, y; { long long count = 0; bool flag = 0; long long a[200050] = {0}; string s; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; edge[x].push_back(y); edge[y].push_back(x); } { flag = true; for (i = 1; i <= n; i++) { if (edge[i].size() == 2) { flag = false; } } if (flag) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; bool is_prime(long long n) { for (long long i = 2; i * i <= n; ++i) { if (n % i == 0) { return false; } } return true; } vector<long long> fact(long long n) { n = abs(n); vector<long long> ans; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { ans.push_back(i); ans.push_back(n / i); } } return ans; } inline long long getPow(long long a, long long b) { long long res = 1ll, tp = a; while (b) { if (b & 1ll) { res *= tp; } tp *= tp; b >>= 1ll; } return res; } long long vec_mult(long long x1, long long y1, long long x2, long long y2, long long x3, long long y3) { return abs((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)); } void ok() { cout << "YES" << endl; exit(0); } void no() { cout << "NO" << endl; exit(0); } inline long long nxt() { long long x; cin >> x; return x; } const long long N = 3e5 + 5, inf = 8e18; int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long n = nxt(); vector<vector<long long>> g(n); for (int i = 1; i < n; i++) { long long t1 = nxt() - 1, t2 = nxt() - 1; g[t1].push_back(t2); g[t2].push_back(t1); } map<long long, long long> mp; for (auto x : g) { if (x.size() == 2) { no(); } } ok(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, a, b; vector<int> adj[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = (int)1; i <= (int)n - 1; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = (int)1; i <= (int)n; i++) if (adj[i].size() == 2) { cout << "NO"; return 0; } cout << "YES"; }
10
CPP
n = int(input()) node = [] for i in range(1 + n): node.append( [] ) for q in range(n - 1): k = list(map(int, input().split())) node[ k[0] ].append( k[1] ) node[ k[1] ].append( k[0] ) hobe = True for i in range(n + 1): if(len(node[i]) == 2): hobe = False if hobe : print("YES") else : print("NO")
10
PYTHON3
#! /usr/bin/env python3 import sys from math import factorial class Node: def __init__(self, num): self.num = num self.binds = [] # for dijkstra self.marker = False self.val = None def add_bind(self, oth): self.binds.append(oth) def __repr__(self): return '<{}: {}{}>'.format( self.num, [i.num for i in self.binds], ', \tval: {}'.format(self.val) if self.val != None else '' ) class Graph: def __init__(self, size): self.size = size self.nodes = [None] + [Node(num) for num in range(1, size+1)] def read_input(self): for _ in range(1, self.size): i, j = (int(x) for x in sys.stdin.readline().split()) self.nodes[i].add_bind(self.nodes[j]) self.nodes[j].add_bind(self.nodes[i]) def __repr__(self): return '\n'.join(str(node) for node in self.nodes[1:]) def pairs(n): return factorial(n) // ( factorial(n-2) * 2 ) N = int(sys.stdin.readline()) g = Graph(N) g.read_input() #print(g) ends = [node for node in g.nodes[1:] if len(node.binds) == 1] #print('YES' if pairs(len(ends)) >= N-1 else 'NO') for n in g.nodes[1:]: if len(n.binds) == 2: print('NO') break else: print('YES')
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long inff = 0x3f3f3f3f3f3f3f3f; int n, x, y, du[100008]; int main() { cin.tie(0); cout.tie(0); cin >> n; for (int i(1); i <= (n - 1); ++i) { scanf("%d", &x), scanf("%d", &y); du[x]++, du[y]++; } for (int i(1); i <= (n); ++i) if (du[i] == 2) return puts("NO"), 0; puts("YES"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, f = 0; cin >> n; vector<int> V[100008]; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; V[u].push_back(v); V[v].push_back(u); } for (int i = 0; i <= n; i++) { if (V[i].size() == 2) { f = 1; break; } } if (f) cout << "NO" << endl; else cout << "YES" << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; int cnt[100005]; int main() { int n; scanf("%d", &n); if (n == 2) { printf("YES\n"); return 0; } if (n == 3) { printf("NO\n"); return 0; } for (int i = 0; i < n - 1; i++) { int u, v; scanf("%d%d", &u, &v); cnt[u]++; cnt[v]++; } for (int i = 1; i <= n; i++) { if (cnt[i] == 2) { printf("NO\n"); return 0; } } printf("YES\n"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n; vector<int> v[101010]; int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d %d", &a, &b); v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++) if (v[i].size() == 2) return !printf("NO"); printf("YES"); }
10
CPP
n=int(input()) a=[0]*n for i in range(n-1): c,d=map(int,input().split()) a[c-1]+=1 a[d-1]+=1 stat=2 in a if(stat): print('NO') else: print('YES')
10
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> g[100005]; int find(int v, int* parent) { if (parent[v] == v) { return v; } return find(parent[v], parent); } int vis[100005]; int dp[100005]; int dfs(int u) { vis[u] = 1; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!vis[v]) dfs(v); } } int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) { if (g[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
10
CPP
#include <bits/stdc++.h> int main() { long long in[100005]; long long n, i, j, m, t; memset(in, 0, sizeof(in)); scanf("%lld", &n); for (i = 1; i <= n - 1; i++) { long long u, v; scanf("%lld %lld", &u, &v); in[u]++; in[v]++; } long long num = 0; long long ans = 0; int flag = 1; for (i = 1; i <= n; i++) { if (in[i] == 1) num++; if (in[i] == 2) flag = 0; } num--; ans = (1 + num) * num / 2; if (ans >= n - 1 && flag) printf("YES\n"); else printf("NO\n"); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; vector<vector<int>> adj; int main() { int n; cin >> n; adj = vector<vector<int>>(n + 1); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } string ans = "YES\n"; int flag = 1; for (int i = 1; i <= n; i++) { if (adj[i].size() == 2) { ans = "NO\n"; flag = 0; break; } } cout << ans; return 0; }
10
CPP
import sys input = sys.stdin.readline from collections import deque class Graph(object): """docstring for Graph""" def __init__(self,n,d): # Number of nodes and d is True if directed self.n = n self.graph = [[] for i in range(n)] self.parent = [-1 for i in range(n)] self.directed = d def addEdge(self,x,y): self.graph[x].append(y) if not self.directed: self.graph[y].append(x) def bfs(self, root): # NORMAL BFS queue = [root] queue = deque(queue) vis = [0]*self.n while len(queue)!=0: element = queue.popleft() vis[element] = 1 count = 0 for i in self.graph[element]: if vis[i]==0: queue.append(i) self.parent[i] = element vis[i] = 1 count += 1 if count==1 and element!=root: return False return True def dfs(self, root, ans): # Iterative DFS stack=[root] vis=[0]*self.n stack2=[] while len(stack)!=0: # INITIAL TRAVERSAL element = stack.pop() if vis[element]: continue vis[element] = 1 stack2.append(element) for i in self.graph[element]: if vis[i]==0: self.parent[i] = element stack.append(i) while len(stack2)!=0: # BACKTRACING. Modify the loop according to the question element = stack2.pop() m = 0 for i in self.graph[element]: if i!=self.parent[element]: m += ans[i] ans[element] = m return ans def shortestpath(self, source, dest): # Calculate Shortest Path between two nodes self.bfs(source) path = [dest] while self.parent[path[-1]]!=-1: path.append(parent[path[-1]]) return path[::-1] def detect_cycle(self): indeg = [0]*self.n for i in range(self.n): for j in self.graph[i]: indeg[j] += 1 q = deque() vis = 0 for i in range(self.n): if indeg[i]==0: q.append(i) while len(q)!=0: e = q.popleft() vis += 1 for i in self.graph[e]: indeg[i] -= 1 if indeg[i]==0: q.append(i) if vis!=self.n: return True return False def reroot(self, root, ans): stack = [root] vis = [0]*n while len(stack)!=0: e = stack[-1] if vis[e]: stack.pop() # Reverse_The_Change() continue vis[e] = 1 for i in graph[e]: if not vis[e]: stack.append(i) if self.parent[e]==-1: continue # Change_The_Answers() n = int(input()) g = Graph(n,False) for i in range(n-1): u,v = map(int,input().split()) g.addEdge(u-1,v-1) for i in range(n): if len(g.graph[i])==1: leaf = i break if not g.bfs(leaf): print ("NO") else: print ("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int degree[100005]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; ++degree[u]; ++degree[v]; } for (int i = 1; i <= n; ++i) { if (degree[i] == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long max_n = 1e6 + 20; long long n, m, k, ans, sum; long long a[max_n]; long long mark[max_n]; vector<long long> v, adj[max_n], jda[max_n]; void dfs(long long v) { mark[v] = 1; if (adj[v].size() == 2) ans = -1; for (auto i : adj[v]) { if (!mark[i]) { dfs(i); } } } int32_t main() { cin >> n; for (long long i = 1; i < n; i++) { long long u, v; cin >> u >> v; u--, v--; adj[v].push_back(u); adj[u].push_back(v); } if (n == 2) { cout << "YES"; return 0; } for (long long i = 0; i < n; i++) { if (adj[i].size() == 1 && !mark[i]) dfs(i); } if (ans == -1) { cout << "NO"; } else cout << "YES"; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n, u, v; int a[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; memset(a, 0, sizeof(a)); for (int i = 0; i < n - 1; i++) { cin >> u >> v; a[u]++; a[v]++; } long long cnt[3] = {0, 0, 0}; for (int i = 1; i <= n; i++) { if (a[i] <= 2) cnt[a[i]]++; } if (cnt[2] == 0 && cnt[1] * (cnt[1] - 1) / 2 >= n - 1) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
10
CPP
n = int(input()) degree = [0 for i in range(n)] for i in range(n-1): a = [int(i) for i in input().split()] x = a[0]-1 y = a[1]-1 degree[x]+=1 degree[y]+=1 flag = 0 for i in range(n): if(degree[i] == 2): flag=1 break if(flag==0): print("YES") else: print("NO")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, cnt[100009]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; cnt[x]++, cnt[y]++; } for (int i = 1; i <= n; i++) if (cnt[i] == 2) return cout << "NO", 0; cout << "YES"; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 10; long long n, deg[maxn]; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void print(long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + '0'); } void write(long long x) { print(x); puts(""); } signed main() { n = read(); bool flag = 0; for (long long i = 2; i <= n; i++) deg[read()]++, deg[read()]++; for (long long i = 1; i <= n; i++) if (deg[i] == 2) { puts("NO"); flag = 1; break; } if (!flag) puts("YES"); return 0; }
10
CPP
n=int(input()) ar=[0]*n for i in range(n-1): a,b=map(int,input().split()) ar[a-1]+=1 ar[b-1]+=1 ans="YES" for i in range(n): if(ar[i]==2): ans="NO" break print(ans)
10
PYTHON3
import sys input = sys.stdin.readline def getN(): return int(input()) def getList(): return list(map(int, input().split())) from sys import exit n = getN() vertex = [[] for i in range(n)] for i in range(n-1): a, b = getList() vertex[a-1].append(b-1) vertex[b - 1].append(a - 1) for v in vertex: if len(v) == 2: print("NO") exit() print("YES")
10
PYTHON3
n = int(input()) # n, k = map(int, input().split()) # a = [int(i) for i in input().split()] d = [0 for i in range(n + 1)] for i in range(n - 1): a, b = map(int, input().split()) d[a] += 1; d[b] += 1 for val in d: if val == 2: print("NO") exit() print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<long long> tree[500500]; long long num[500500], fine = 0, n; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; for (int i = 1; i < n; i++) { long long u, v; cin >> u >> v; tree[u].push_back(v); tree[v].push_back(u); } for (int i = 1; i <= n; i++) if (tree[i].size() == 2) return cout << "NO", 0; cout << "YES"; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; void solve() { int n, u, v; cin >> n; vector<int> Adj[n]; for (int i = 1; i < n; ++i) { cin >> u >> v; --u; --v; Adj[u].push_back(v); Adj[v].push_back(u); } for (int i = 0; i < n; ++i) { if (Adj[i].size() == 2) { cout << "NO\n"; return; } } cout << "YES\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const int maxn = 1e5 + 100; int n, x; int a[maxn]; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x; a[x]++; cin >> x; a[x]++; } for (int i = 1; i < n + 1; i++) { if (a[i] == 2) { cout << "No" << endl; return 0; } } cout << "YES" << endl; return 0; }
10
CPP
n = int(input()) adj = [0 for i in range(n)] for i in range(n-1): iarr = list(map(int,input().split())) u = iarr[0]-1 v = iarr[1]-1 adj[u]+=1 adj[v]+=1 flag = 0 for i in adj: if i==2: flag=1 break if flag==1: print("NO") else: print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> g(n); int f, t; for (int i = 0; i < n - 1; ++i) { cin >> f >> t; f--; t--; g[f].push_back(t); g[t].push_back(f); } for (int i = 0; i < n; ++i) { if (g[i].size() == 2) { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
10
CPP
n=int(input()) d={} o,t=0,0 for _ in range(n-1): s,e=map(int,input().split()) if s in d: d[s]+=1 else: d[s]=1 if e in d: d[e]+=1 else: d[e]=1 for i in d: if d[i]==1: o+=1 elif d[i]==2: t+=1 if t: print('NO') else: if (o*(o-1))//2>=n-1: print('YES') else: print('NO')
10
PYTHON3
from sys import stdin input = stdin.readline n = int(input()) degree = [0 for i in range(n+1)] for _ in range(n-1): i, j = [int(i) for i in input().split()] degree[i] += 1 degree[j] += 1 res = False for i in range(1, n+1): if degree[i] == 2: res = True if res: print("NO") else: print("YES")
10
PYTHON3
n=int(input()) tree=[[] for i in range(n)] for i in range(n-1): u,v=map(int,input().split()) tree[u-1].append(v-1) tree[v-1].append(u-1) if n==2: print("YES") exit() #nが4以上 for i in range(n): if len(tree[i])==2: print("NO") break else: print("YES")
10
PYTHON3
if __name__ == '__main__': n = int(input()) g = [[] for _ in range(n)] for _ in range(n - 1): u, v = map(int, input().split()) g[u - 1].append(v - 1) g[v - 1].append(u - 1) if list(filter(lambda x: len(x) == 2, g)): print("NO") else: print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); a[x]++; a[y]++; } for (int i = 1; i <= n; i++) if (a[i] == 2) { printf("NO\n"); return 0; } printf("YES\n"); return 0; }
10
CPP
n = int(input()) a = [] for i in range(0,n+9): a.append(0) for i in range(1,n): u,v = map(int,input().split()) a[u] = a[u]+1 a[v] = a[v]+1 flag = 1; for i in range(1,n+1): if a[i]==2: flag = 0 if flag==0: print("NO") else: print("YES")
10
PYTHON3
from collections import defaultdict g = defaultdict(set) n = int(input()) for _ in range(n - 1): u, v = map(int, input().split()) g[u].add(v) g[v].add(u) def solve(n, g): for i in range(1, n + 1): if len(g[i]) == 1 or len(g[i]) >= 3: continue else: return False return True solution = solve(n, g) print('YES' if solution else 'NO')
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7; long long n; vector<long long> add[N]; void nie() { cout << "NO"; exit(0); } void yie() { cout << "YES"; exit(0); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); vector<pair<long long, long long>> v; cin >> n; for (long long i = 1; i < n; i++) { long long x, y; cin >> x >> y; add[x].push_back(y); add[y].push_back(x); v.push_back({x, y}); } if (n == 2) yie(); if (n == 3) nie(); for (auto i : v) { if (add[i.second].size() == 2 || add[i.first].size() == 2) nie(); } yie(); return 0; }
10
CPP
n = int(input()) l = [0 for i in range(n)] for _ in range(n-1): a, b = list(map(int, input().split())) l[a-1] += 1 l[b-1] += 1 for i in range(n): if (l[i] == 2): print('NO') break else: print('YES')
10
PYTHON3
from collections import Counter from sys import stdin n=int(input()) lst=[] for _ in range(n-1): a,b=map(int,stdin.readline().split()) lst.append(a) lst.append(b) fg=0 for _,i in Counter(lst).items(): if i==2: fg=1 break if fg==1: print("NO") else: print("YES")
10
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("-O2") using namespace std; const int LIM = 1e5 + 5, MOD = 1e9 + 7; const long double EPS = 1e-9; vector<vector<int> > g; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; g.resize(n); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; u--, v--; g[u].push_back(v); g[v].push_back(u); } for (int i = 0; i < n; ++i) { if (g[i].size() == 2) { cout << "NO" << '\n'; exit(0); } } cout << "YES" << '\n'; return 0; }
10
CPP
n = int(input()) edges = [0]*(n+1) isPossible = True for i in range(n-1): a,b = [int(i) for i in input().split()] edges[a]+=1 edges[b]+=1 for d in edges: if d == 2: isPossible = False break print("YES" if isPossible else "NO")
10
PYTHON3
import sys N = int(input()) adj_list = [[] for _ in range(N)] for _ in range(N-1): (u,v) = list(map(int, input().split())) u -= 1 v -= 1 adj_list[u].append(v) adj_list[v].append(u) num_leaf = 0 for node in range(N): if len(adj_list[node]) == 2: print("NO") sys.exit(0) print("YES")
10
PYTHON3
from sys import stdin input=stdin.readline n=int(input()) a=[[] for i in range(n)] for i in range(n-1): c,d=map(int,input().split()) a[c-1].append(d-1) a[d-1].append(c-1) if n==2: print('YES') else: k=0 for i in a: if len(i)==2: exit(print('NO')) print('YES')
10
PYTHON3
# | # _` | __ \ _` | __| _ \ __ \ _` | _` | # ( | | | ( | ( ( | | | ( | ( | # \__,_| _| _| \__,_| \___| \___/ _| _| \__,_| \__,_| import sys import math def read_line(): return sys.stdin.readline()[:-1] def read_int(): return int(sys.stdin.readline()) def read_int_line(): return [int(v) for v in sys.stdin.readline().split()] def read_float_line(): return [float(v) for v in sys.stdin.readline().split()] n = read_int() d = {} for i in range(n-1): u, v = read_int_line() if u in d: d[u] += 1 else: d[u] = 1 if v in d: d[v] += 1 else: d[v] = 1 f = False for i in d: if d[i]==2: f = True if f: print("NO") else: print("YES")
10
PYTHON3
def ii(): return int(input()) def ss(): return [x for x in input()] def si(): return [int(x) for x in input().split()] def mi(): return map(int, input().split()) a = ii() s = [0 for i in range(a)] for i in range(a - 1): c, d = [int(x) - 1 for x in input().split()] s[c] += 1 s[d] += 1 if 2 in s: print("NO") else: print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; void func() { int n; cin >> n; vector<int> adj[n + 1]; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) { if (adj[i].size() == 1) continue; if (adj[i].size() < 3) { cout << "NO" << endl; return; } } cout << "YES" << endl; return; } int main() { std::ios::sync_with_stdio(false); int t = 1; while (t--) func(); return 0; }
10
CPP
import sys input=sys.stdin.readline from collections import deque n=int(input()) if n==2: print('YES') exit() if n==3: print('NO') exit() Edges=[[] for _ in range(n)] for _ in range(n-1): u,v=map(lambda x: int(x)-1,input().split()) Edges[u].append(v) Edges[v].append(u) for i,E in enumerate(Edges): if len(E)>=3: root=i break else: print('NO') exit() Chi=[[] for _ in range(n)] Par=[0]*n q=deque() q.append(root) Used=[False]*n Used[root]=True while q: v=q.popleft() for c in Edges[v]: if Used[c]: continue Chi[v].append(c) Par[c]=v Used[c]=True q.append(c) Leaf=[] for v,l in enumerate(Chi): if not l: Leaf.append(v) for l in Leaf: while True: p=Par[l] if p==root: break if len(Chi[p])==1: print('NO') exit() l=p print('YES')
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long MAX = 1e5 + 10; long long arr[MAX]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; arr[u]++; arr[v]++; } for (int i = 1; i <= n; i++) if (arr[i] == 2) return puts("NO"); puts("YES"); return 0; }
10
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int N = 3e5 + 5, M = 1e8 + 5, OO = 1000000; int T, n, m; int u, v; int deg[N]; int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { scanf("%d %d", &u, &v); ++deg[u], ++deg[v]; } for (int i = 1; i <= n; ++i) { if (deg[i] == 2) { printf("NO\n"); return 0; } } printf("YES\n"); }
10
CPP
#include <bits/stdc++.h> using namespace std; int a[100010]; int main() { int n, flag = 1; scanf("%d", &n); for (int i = 1, x, y; i < n; i++) { scanf("%d %d", &x, &y); a[x]++; a[y]++; } for (int i = 1; i <= n; i++) if (a[i] == 2) flag = 0; flag ? printf("YES") : printf("NO"); }
10
CPP
#include <bits/stdc++.h> using namespace std; int deg[100005]; int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); deg[u]++; deg[v]++; } for (int i = 1; i <= n; i++) { if (deg[i] == 2) { printf("NO\n"); return 0; } } printf("YES\n"); return 0; }
10
CPP
n = int(input()) nodes = [[] for _ in range(n)] edges = [] for _ in range(n-1): u, v = sorted(map(int, input().split(" "))) u -= 1 v -= 1 nodes[u].append(v) nodes[v].append(u) edge = [u, v] edges.append(edge) leaf_nodes = [node for node in nodes if len(node) == 1] num_ads = [len(node) for node in nodes] if 2 in num_ads: out = "NO" else: out = "YES" #print(num_ads) #for u, v in edges: # num_ad_u = num_ads[u] # num_ad_v = num_ads[v] # #if num_ad_u == 1 or num_ad_v == 1: # # continue # if num_ad_u == 2 or num_ad_v == 2: # out = "NO" # break print(out)
10
PYTHON3
from collections import defaultdict n = int(input()) hash = defaultdict(list) for i in range(n-1): a,b = map(int,input().split()) hash[a].append(b) hash[b].append(a) flag = 1 for i in hash.keys(): if len(hash[i]) == 2: flag = 0 break if flag: print('YES') else: print('NO')
10
PYTHON3
#include <bits/stdc++.h> using ll = int64_t; using namespace std; const int N = 1e6 + 5; int n; int a[N]; basic_string<int> g[N]; int32_t main() { cin.tie(0)->sync_with_stdio(0); cin >> n; for (int i = 1, u, v; i < n; i++) { cin >> u >> v; g[u] += v; g[v] += u; } for (int i = 1; i <= n; i++) { if (g[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n; int n1, n2; int grau[100010]; vector<pair<int, int> > edges; int main() { scanf("%d", &n); for (int g = 0; g < n - 1; g++) { scanf("%d %d", &n1, &n2); grau[n1]++; grau[n2]++; } for (int g = 1; g <= n; g++) { if (grau[g] == 2) { printf("NO\n"); return 0; } } printf("YES\n"); }
10
CPP
#include <bits/stdc++.h> using namespace std; int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}; int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1}; int dx4[] = {1, 0, -1, 0}; int dy4[] = {0, 1, 0, -1}; template <class A, class B> ostream& operator<<(ostream& out, const pair<A, B>& a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream& operator<<(ostream& out, const vector<A>& a) { for (const A& it : a) out << it << " "; return out; } template <class A, class B> istream& operator>>(istream& in, pair<A, B>& a) { return in >> a.first >> a.second; } template <class A> istream& operator>>(istream& in, vector<A>& a) { for (A& i : a) in >> i; return in; } vector<int>* inputG(int n, int m) { vector<int>* edges = new vector<int>[n + 1]; for (int i = 0; i < m; i++) { int sv, ev; cin >> sv >> ev; edges[sv].push_back(ev); edges[ev].push_back(sv); } return edges; } void solve() { int n; cin >> n; vector<int>* edges = inputG(n, n - 1); for (int i = 1; i <= n; i++) { if (edges[i].size() == 2) { cout << "NO" << "\n"; ; return; } } cout << "YES" << "\n"; ; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
10
CPP
import sys input = sys.stdin.readline n = int(input()) a = [list(map(int, input().split())) for i in range(n-1)] tree = [[] for i in range(n)] for i in range(n - 1): tree[a[i][0] - 1].append(a[i][1] - 1) tree[a[i][1] - 1].append(a[i][0] - 1) for i in range(n): if len(tree[i]) == 2: print("NO") exit() print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int deg[200000]; int main() { int n; cin >> n; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; deg[u]++; deg[v]++; } for (int i = 1; i <= n; ++i) { if (deg[i] == 2) { cout << "NO\n"; return 0; } } cout << "YES\n"; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int x, y, n; cin >> n; vector<int> v[100010], barg; for (int i = 0; i < n - 1; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i <= n; i++) { if (v[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1000, mod = 1e9 + 7; vector<int> vec[N]; long long bin_pow(int x, int y) { if (y == 0) return 1; if (y == 1) return x; if (y % 2 == 0) { long long z = bin_pow(x, y / 2); return (z * z) % mod; } return (bin_pow(x, y - 1) * x) % mod; } bool check(int x) {} int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; vec[x].push_back(y); vec[y].push_back(x); } int k = 0; for (int i = 1; i <= n; i++) { if (vec[i].size() == 2) { cout << "NO"; return 0; } } cout << "YES"; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; vector<vector<long long> > g; void endProg() { cout << "NO"; exit(0); } void dfs(long long v, long long p) { long long l = 0; for (auto i : g[v]) { if (i == p) continue; if (g[i].size() == 1) { l++; continue; } dfs(i, v); } if (v == 0) { if (g[v].size() == 2) endProg(); } else { if (g[v].size() == 2) endProg(); } } int main() { long long n; cin >> n; g.resize(n); for (long long i = 0; i < n - 1; i++) { long long a, b; cin >> a >> b; g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } dfs(0, 0); cout << "YES"; return 0; }
10
CPP
import sys num = int(input()) deg = [0]*(num+1) for _ in range(1,num): a, b = [int(i) for i in input().split()] deg[a ] += 1 deg[b] += 1 for i in range(1, num+1): if deg[i] == 2: print("NO") sys.exit() print("YES")
10
PYTHON3
# -*- coding: utf-8 -*- import sys # from operator import itemgetter # from fractions import gcd # from math import ceil, floor # from copy import deepcopy # from itertools import accumulate from collections import deque # import math # from functools import reduce input = sys.stdin.readline def ii(): return int(input()) def mi(): return map(int, input().rstrip().split()) def lmi(): return list(map(int, input().rstrip().split())) def li(): return list(input().rstrip()) # template # BEGIN CUT HERE class Graph(): def __init__(self, n, Weighted=False, Directed=True, Matrix=False): self.sz = n self.is_Weighted = Weighted self.is_Directed = Directed self.is_Matrix = Matrix if Matrix: if Weighted: self.graph = [[0 for _i in range(n)] for _j in range(n)] else: self.graph = [[0 for _i in range(n)] for _j in range(n)] else: self.graph = [[] for _i in range(n)] def _weighted_add_edge(self, x, y, w): if self.is_Matrix: self.graph[x][y] = w else: self.graph[x].append((y, w)) def _unweighted_add_edge(self, x, y): if self.is_Matrix: self.graph[x][y] = 1 else: self.graph[x].append(y) def add_edge(self, x, y, *w): if self.is_Directed: if self.is_Weighted: self._weighted_add_edge(x, y, w[0]) else: self._unweighted_add_edge(x, y) else: if self.is_Weighted: self._weighted_add_edge(x, y, w[0]) self._weighted_add_edge(y, x, w[0]) else: self._unweighted_add_edge(x, y) self._unweighted_add_edge(y, x) def _convert_to_maxrix(self): if self.is_Matrix: return self mat_g = self.__class__( self.sz, Weighted=self.is_Weighted, Directed=self.is_Directed, Matrix=True) if self.is_Weighted: for i in range(self.sz): for j in self.graph[i]: mat_g.add_edge(i, j[0], j[1]) else: for i in range(self.sz): for j in self.graph[i]: mat_g.add_edge(i, j) return mat_g def __getitem__(self, n): return self.graph[n] # def __setitem__(self, n, v): # if not self.is_Weighted and not self.is_Matrix: # self.graph[n] = v def __str__(self): return str([self.graph[i] for i in range(self.sz)]) def main(): n = ii() g = Graph(n, Directed=False) for i in range(n - 1): s, t = mi() g.add_edge(s-1, t-1) for i in range(n): if len(g[i]) == 2: break else: print('YES') sys.exit() print('NO') if __name__ == '__main__': main()
10
PYTHON3
from collections import defaultdict n = int(input()) tr = defaultdict(int) for i in range(n-1): u, v = map(int, input().split()) tr[u]+=1 tr[v]+=1 for i in tr: if tr[i]==2: print("NO") break else: print("YES")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long inf = LONG_MAX; const long long arr = 1000000; bool comp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second < b.second; else return a.first < b.first; } vector<vector<long long> > g(arr); int main(void) { long long n; cin >> n; for (int i = 0; i < n - 1; i++) { long long l, r; cin >> l >> r; g[l].push_back(r); g[r].push_back(l); } for (long long i = 1; i <= n; i++) { if (g[i].size() == 2) return cout << "NO" << '\n', 0; } cout << "YES" << '\n'; return 0; }
10
CPP