text
stringlengths
49
983k
#include <bits/stdc++.h> const int N = 200054, M = N; int V, E; int w[N], W[N], deg[N]; int to[M], first[N], next[M]; int topo[N], mark[N], sg[N]; inline void up(int &x, const int y) { x < y ? x = y : 0; } inline void addedge(int u, int v, int id) { to[id] = v, next[id] = first[u], first[u] = id, ++deg[v]; } int main...
#include <bits/stdc++.h> inline long long gi() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) f ^= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f ? x : -x; } int h[200010], SG[200010], sum[200010]; int fir[200010], nxt[200010], dis[200010], ...
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch - '0' < 0 || ch - '0' > 9) { if (ch == '-') f = -1; ch = getchar(); } while (ch - '0' >= 0 && ch - '0' <= 9) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } in...
#include <bits/stdc++.h> using namespace std; template <typename _tp> inline void read(_tp& x) { char ch = getchar(), ob = 0; x = 0; while (ch != '-' && !isdigit(ch)) ch = getchar(); if (ch == '-') ob = 1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); if (ob) x = -x; } const int...
#include <bits/stdc++.h> using namespace std; const int prime = 999983; const int INF = 0x7FFFFFFF; const long long INFF = 0x7FFFFFFFFFFFFFFF; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-6; const long long mod = 1e9 + 7; long long qpow(long long a, long long b) { long long s = 1; wh...
#include <bits/stdc++.h> using namespace std; int h[212345]; vector<vector<int> > graph; vector<int> mex; vector<int> xors; int get_mex(int v) { if (mex[v] != -1) return mex[v]; vector<bool> seen(graph[v].size(), false); for (auto u : graph[v]) { int val = get_mex(u); if (val < seen.size()) seen[val] = tr...
#include <bits/stdc++.h> using namespace std; constexpr int inf32 = 0x3f3f3f3f; constexpr long long inf64 = 0x3f3f3f3f3f3f3f3f; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; vector<long long> a(n); for (int u = 0; u < (n); ++u) cin >> a[u]; vector<vector<int> ...
#include <bits/stdc++.h> using namespace std; const int MOD = (int)998244353; const int MAXN = (int)2e5 + 3; const int infint = (int)1e9 + 3; const long long inf = (long long)1e18; int n, m, M[MAXN], visited[MAXN], h[MAXN], t[MAXN]; vector<int> G[MAXN], topol; void dfs(int u) { visited[u] = 1; for (auto v : G[u]) ...
#include <bits/stdc++.h> using namespace std; using LL = long long; const int MAXN = 200010; vector<int> x[MAXN], y[MAXN]; int v[MAXN], out[MAXN], sg[MAXN]; int xr[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) cin >> v[i]; for (int i = 0; i...
#include <bits/stdc++.h> using namespace std; template <typename tp> inline void read(tp &x) { x = 0; char c = getchar(); int f = 0; for (; c < '0' || c > '9'; f |= c == '-', c = getchar()) ; for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar()) ; if (f) x = -x; } const int ...
#include <bits/stdc++.h> const int MAXN = 200010; int head[MAXN], nxt[MAXN], to[MAXN], ind[MAXN], tot; void adde(int b, int e) { nxt[++tot] = head[b]; to[head[b] = tot] = e; ++ind[e]; } int A[MAXN], mx[MAXN]; std::set<int> S; int n, m; std::queue<int> q; int bfn[MAXN], idx; int B[MAXN]; int main() { std::ios_ba...
#include <bits/stdc++.h> using namespace std; vector<vector<int>> E; vector<int> G; vector<int> h; void dfs(int cur) { if (G[cur] != -1) return; set<int> S; for (int i = 0; i < (E[cur].size()); ++i) { int to = E[cur][i]; dfs(to); S.insert(G[to]); } for (int i = 0; i < (1000000001); ++i) { if (...
#include <bits/stdc++.h> using namespace std; template <class n, class second> ostream &operator<<(ostream &p, pair<n, second> x) { return p << "<" << x.first << ", " << x.second << ">"; } template <class n> auto operator<<(ostream &p, n y) -> typename enable_if<!is_same<n, string>::value, ...
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> a[200005]; struct edge { int to, nxt; } e[200005]; int hed[200005], cnt; inline void add(int u, int v) { e[++cnt] = (edge){v, hed[u]}; hed[u] = cnt; } int num[200005], ru[200005]; int sg[200005], tag[200005], dep[200005]; inline void topo() { q...
#include <bits/stdc++.h> using namespace std; struct node { int t, next; } a[200010], a1[200010]; vector<int> v[200010]; queue<int> q; int head[200010], head1[200010], num[200010], out[200010], f[200010], sum[200010], n, m, tot, tot1; bool vis[200010]; inline int rd() { int x = 0; char ch = getchar(); for (...
#include <bits/stdc++.h> using namespace std; int read() { char c = getchar(); int x = 0; while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar(); return x; } int n, m; const int _ = 2e5 + 7; int val[_], v[_]; vector<int> E[_]; int deg[_]; int st[_], top; int ...
#include <bits/stdc++.h> using namespace std; int gi() { int x = 0, w = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') w = 0, ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return w ? x : -x; } const ...
#include <bits/stdc++.h> using namespace std; long long h[200005], s[200005]; int n, m, a, b, ru[200006], mex[200005], fu[200005], ma; vector<int> x[200005]; int dfs(int now) { if (mex[now]) return mex[now]; for (int i = 0; i < x[now].size(); i++) dfs(x[now][i]); for (int i = 0; i < x[now].size(); i++) fu[mex[x[n...
#include <bits/stdc++.h> using namespace std; const int maxn = 200005, maxm = 200005; int n, m, e, stp; int h[maxn], SG[maxn], start[maxn], to[maxm], then[maxm], xorsum[maxn], deg[maxn], vis[maxn]; vector<int> v[maxn], revg[maxn], g[maxn]; queue<int> q; inline void add(int x, int y) { g[x].push_back(y), deg[x]++;...
#include <bits/stdc++.h> using namespace std; int mod = 998244353; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { long long c = a * 1LL * b; return c % mod; } i...
#include <bits/stdc++.h> const int N = 200005; int n, m, sg[N]; bool r[N]; long long a[N], sum[N]; std::vector<int> g[N]; bool vis[N]; int dfs(int x) { if (~sg[x]) return sg[x]; for (int p : g[x]) dfs(p); for (int p : g[x]) vis[dfs(p)] = 1; for (int i = 0;; ++i) if (!vis[i]) { sg[x] = i; break; ...
#include <bits/stdc++.h> using namespace std; const long long N = 200005; long long ver[N << 1], nxt[N << 1], head[N], deg[N], tot; long long h[N], st[N], vis[N << 1], id[N], t[N << 1], Max, top, n, m; queue<long long> q; inline long long read() { long long x = 0, ff = 1; char ch = getchar(); while (ch < '0' || c...
#include <bits/stdc++.h> using namespace std; int mod = 998244353; int sum(int a, int b) { int c = a + b; if (c >= mod) { c -= mod; } return c; } int dif(int a, int b) { int c = a - b; if (c < 0) { c += mod; } return c; } int mlt(int a, int b) { long long c = a * 1LL * b; return c % mod; } i...
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct edge { int to, nxt; } e[N]; vector<int> G[N]; int deg[N], n, head[N], cnt, m, h[N], mex[N], X[N]; bool vis[N]; queue<int> q; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", h + i); while (m--) { int u, v...
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) { if (ch == '-') f = -1; } for (; isdigit(ch); ch = getchar()) { x = x * 10 + ch - 48; } return x * f; } const int mxN = 2e5; struct Edge { int v, nxt; } ...
#include <bits/stdc++.h> const int N = 200005; int n, m, t = -1, he, ta, sg[N], d[N], s[N], q[N]; bool vis[N]; long long h[N]; struct ed { ed* nxt; int to; } pool[N], *p = pool, *lnk[N]; void ae(int u, int v) { *++p = (ed){lnk[u], v}, ++d[v], lnk[u] = p; } void dfs(int u) {} int main() { scanf("%d%d", &n, &m); ...
#include <bits/stdc++.h> using namespace std; const int N = 400005; int n, m, i, j, k, h[N], head[N], adj[N], nxt[N], sg[N], q[N], bg, ed, d[N], s[N]; bool v[N]; int main() { scanf("%d%d", &n, &m); for (i = 1; i <= n; ++i) scanf("%d", h + i); for (i = 1; i <= m; ++i) { scanf("%d%d", &j, &k); adj[i] = ...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; inline int gi() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } int n, m, val[maxn], Id[maxn], sum[maxn], vis[maxn]; vector<in...
#include <bits/stdc++.h> using namespace std; int head[200005], ver[200005], nxt[200005], cnt; void add(int a, int b) { ver[++cnt] = b, nxt[cnt] = head[a], head[a] = cnt; } vector<int> e[200005], has[200005]; int n, m, cd[200005]; long long a[200005], sg[200005]; int pos[200005], mex[200005], mx; bool vis[200005]; queu...
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<int> E[N]; int n, m, h[N], deg[N], vis[N], sg[N], sum[N], sq[N], tt = 0; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> h[i]; for (int i = 1, u, v; i <= m; i++) cin >> u >> v, ++deg[v], E[u].push_back(v); queue<int> q;...
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int n, m, h[N], q[N], d[N], s[N], vis[N], sg[N]; vector<int> G[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1, x, y; i <= m; i++) scanf("%d%d", &x, &y), G[x].push_back(y), d[y]++; in...
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline T1 max(T1 a, T2 b) { return a < b ? b : a; } template <typename T1, typename T2> inline T1 min(T1 a, T2 b) { return a < b ? a : b; } const char lf = '\n'; namespace ae86 { const int bufl = 1 << 15; char buf[bufl], *s = buf, *t ...
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 50; int n, m, h[N], f[N]; vector<int> G[N]; int d[N], S[N]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), ++d[v], G[u].push_back(v); static int q[N]; i...
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007LL; long long large = 2000000000000000000LL; vector<int> dp; vector<vector<int> > adj; int f(int u) { if (dp[u] != -1) return dp[u]; vector<int> ch; for (int j = 0; j < (int)adj[u].size(); j++) { int v = adj[u][j]; ch.push_back(f(v));...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> G[maxn]; int sg[maxn], deg[maxn], Xor[maxn], topo[maxn], mem[maxn], cnt; long long h[maxn], XOR[maxn]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%lld", &h[i]); for (int i = 0; i < m; ++i) ...
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(200001); int c[200001], mex[200001], h[200001], b[200001]; void DFS(int i) { if (mex[i] >= 0) { return; } for (int j : adj[i]) { DFS(j); } for (int j : adj[i]) { c[mex[j]] = i; } int &k = mex[i]; for (k = 0; c[k] == i; ++...
#include <bits/stdc++.h> using namespace std; long long n, m, num[200100], sy[200100], ds[200100], bj[200100], sum[200100]; vector<long long> to[200100], pre[200100]; queue<long long> que; int main() { long long i, j, t, p, q; cin >> n >> m; for (i = 1; i <= n; i++) scanf("%lld", &num[i]); for (i = 1; i <= m; i...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> V[maxn], H[maxn]; int n, m; long long a[maxn]; int SG[maxn]; int h[maxn]; int maxx = 0; int dfs(int u) { if (SG[u] != -1) return SG[u]; vector<int> tmp; for (auto v : V[u]) { tmp.push_back(dfs(v)); } sort(tmp.begin(), tmp....
#include <bits/stdc++.h> using namespace std; int n, m, h[200205]; int info[200205], Prev[200205], to[200205], cnt_e; void Node(int u, int v) { Prev[++cnt_e] = info[u], info[u] = cnt_e, to[cnt_e] = v; } int in[200205], Q[200205], L, R, SG[200205], S[200205]; int vis[200205], tim, Mx; int main() { scanf("%d%d", &n, ...
#include <bits/stdc++.h> using namespace std; int h[200000]; vector<int> adjList[200000]; int nim[200000]; int doDFS(int u) { if (nim[u] != -1) return 0; int i, j = 0; vector<int> s; for (i = 0; i < adjList[u].size(); i++) { int v = adjList[u][i]; doDFS(v), s.push_back(nim[v]); } sort(s.begin(), s.e...
#include <bits/stdc++.h> using namespace std; const int MAXN = 2.1e5; int N, M; uint64_t H[MAXN]; int outDeg[MAXN]; vector<int> outEdges[MAXN]; vector<int> inEdges[MAXN]; int omegaPow[MAXN]; uint64_t totalNimber[MAXN]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N >> M; for (int i =...
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int M = 2e5 + 5; int n, m; long long h[N]; long long ans[N]; struct Edge { int num; int next; } edge[M]; int tot, last[N]; int du[N]; int sg[N]; queue<int> L; int node[N], cnt; int mem[N]; void Add(int i, int j) { tot++; edge[tot].num = ...
#include <bits/stdc++.h> using namespace std; const int N = 200005; using ll = long long; int n, m, top; vector<int> v[N]; ll a[N], val[N]; int deg[N], p[N], vis[N], id[N], mx; queue<int> q; void toposort() { for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i); while (!q.empty()) { int nd = q.front(); q...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, m, h[maxn], u, v, d[maxn], sg[maxn], vis[maxn], chc[maxn], hed[maxn], ed, s[maxn], cnt; struct edge { int to, nex; edge(int _ = 0, int __ = 0) : to(_), nex(__) {} } e[maxn]; inline void add(int u, int v) { e[++ed] = edge(v, hed[u]...
#include <bits/stdc++.h> using namespace std; const int maxN = 200002; int mex(vector<int>& cands) { static bool flags[maxN]; for (int i = 0; i <= cands.size(); ++i) { flags[i] = false; } for (int x : cands) { flags[x] = true; } int res = 0; while (flags[res]) { ++res; } return res; } stat...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n, m; int h[maxn], in[maxn], q[maxn], sg[maxn], sum[maxn], vis[maxn]; vector<int> G[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", &h[i]); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d",...
#include <bits/stdc++.h> #pragma optimize("unroll-loops,no-stack-protector") using namespace std; namespace io { struct eof { eof() {} }; const int L = (1 << 21) | 5; char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, qu[55]; int f, qr; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = o...
#include <bits/stdc++.h> using namespace std; const int MX = 200005; int n, m, u, v, a[MX], b[MX], ex[MX], x[MX]; vector<int> adj[MX], in[MX]; void dfs(int u) { if (b[u] != -1) return; for (int v : adj[u]) dfs(v); for (int v : adj[u]) ex[b[v]] = u; b[u] = 0; while (ex[b[u]] == u) b[u]++; in[b[u]].push_back(...
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(200001); int c[200001], mex[200001], h[200001], b[200001]; void DFS(int i) { for (int j : adj[i]) { if (mex[j] >= 0) { continue; } DFS(j); } for (int j : adj[i]) { c[mex[j]] = i; } int &k = mex[i]; for (k = 0; c[k] ...
#include <bits/stdc++.h> using namespace std; struct ahowwogaw { int v, next; } _[1000005]; int head[1000005], tot = 1, zyl[200005], yihuo[1000005], a[1000005]; bool qwer[1000005]; int fyr(int iakioi) { if (zyl[iakioi] >= 0) { return zyl[iakioi]; } int i, ykb = 0, t; for (i = head[iakioi]; i; i = _[i].nex...
#include <bits/stdc++.h> using namespace std; const int N = 200010, M = 200010; int n, m; int e, to[M], nxt[M], hd[N], deg[N]; int tag[N], sg[N], val[N], sum[N]; vector<int> vec[N]; void add(int x, int y) { to[++e] = y; nxt[e] = hd[x]; hd[x] = e; } void topo() { queue<int> q; for (int i = 1; i <= n; i++) ...
#include <bits/stdc++.h> using ll = long long; int const nmax = 200000; int v[1 + nmax]; std::vector<int> g[1 + nmax]; int mex(std::vector<int> aux) { std::sort(aux.begin(), aux.end()); aux.erase(std::unique(aux.begin(), aux.end()), aux.end()); for (int i = 0; i < aux.size(); i++) if (aux[i] != i) return i; ...
#include <bits/stdc++.h> using namespace std; const int MAX = (int)2e5 + 5; int n, m; vector<int> v[MAX]; int h[MAX]; set<int> s[MAX]; set<int> t[MAX]; int dep[MAX]; int xors[MAX]; vector<pair<int, int> > mxx[MAX]; int bio[MAX]; int p[1000]; void dfs(int cx) { if (bio[cx]) return; bio[cx] = 1; for (int i = (0), _...
#include <bits/stdc++.h> const int N = 200005; using namespace std; long long n, m, num[N], sy[N], ds[N], bj[N], sum[N]; vector<long long> to[N], pre[N]; queue<long long> que; int main() { long long i, j, t, p, q; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf("%lld", &num[i]); for (i = 1; i <= m; i++) {...
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) const int N = 2e5 + 5; long long h[N], s[N]; int w[N]; vector<int> g[N]; bool used[N]; void dfs(int node) { if (used[node]) return; used[node] = 1; vector<int> t; for (int to : g[node]) { dfs(to); t.push_back(w[to]); } sor...
#include <bits/stdc++.h> using namespace std; const int N = 1000005; int n, m, h[N], nxt[N], adj[N], rd[N], u, v, t, id[N], mx, q[N], top1, top2; long long a[N], val[N]; bool vs[N]; inline void add() { nxt[++t] = h[u], h[u] = t, adj[t] = v; } int main() { scanf("%d%d", &n, &m); register int i; for (i = 1; i <= n;...
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 200200; vector<int> g[N]; int ord[N]; int ordSz; bool used[N]; int n, m; int a[N]; int b[N]; int c[N]; bool mex[N]; void dfs(int v) { used[v] = 1; for (int u : g[v]) { if (used[u]) co...
#include <bits/stdc++.h> struct city { int estart; long long int tax; bool visited; int mex; } v[200003]; struct edge { int enext, to; } e[200003]; int n, m; void dfs(int i) { v[i].visited = true; std::vector<int> temp; for (int j = v[i].estart; j != -1; j = e[j].enext) { int to = e[j].to; if (!...
#include <bits/stdc++.h> using namespace std; struct node { int to, nxt; } edge[200100]; int val[200100], maxn, cnt, head[200100], n, m; long long h[200100], X[710]; void addedge(int x, int y) { edge[++cnt].to = y; edge[cnt].nxt = head[x]; head[x] = cnt; } void dfs(int x) { if (val[x] != -1) return; bool vi...
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; int n, m; const int NN = 200011; int h[NN]; VI adj[NN]; map<int, int> g; int tot[NN]; inline int get(int u) { if (g.count(u)) return g[u]; map<int, int> vst; for (int v : adj[u]) { vst[get(v)] = 1; } for (int i = 0;; i++) if (vst.co...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10, mod = 1e9 + 7; const long long inf = 1e18; int a[maxn]; bool mark[maxn]; vector<int> v[maxn], vec; vector<int> vv[maxn]; int p[maxn], xr[maxn], C; void dfs(int u) { mark[u] = 1; for (int y : v[u]) { if (!mark[y]) dfs(y); } vec.push_bac...
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; const long double eps = 0.00000000001; int n, m; vector<int> E[300000]; int tab[300000]; int vis[300000]; int levelId[300000]; int levelXor[300000]; void dfs(int a) { vis[a] = true; vector<int> L; for (int v : E[a]) { if (!vis[v]) { dfs(...
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m; vector<int> edges[N]; bool vis[N]; int mex[N]; int getMex(int u) { if (~mex[u]) return mex[u]; vector<bool> vis(edges[u].size() + 1, false); for (int v : edges[u]) { vis[min(getMex(v), (int)edges[u].size())] = true; } mex[u] = 0...
#include <bits/stdc++.h> const long long INF = 1e18; const int inf = 1e9; const int MAXN = 3e5 + 200; const long double eps = 1e-16; const long double pi = acos(-1.0); using namespace std; int dx[] = {0, 1, 1, 1, -1, -1, -1}; int dy[] = {1, -1, 0, 1, -1, 0, 1, -1}; int n, m; long long a[MAXN], H[MAXN]; int M[MAXN]; vec...
#include <bits/stdc++.h> using namespace std; int n, m, h[200012], cnt = 0, a[200012], f[200012], b[200012], c[200012]; bool vis[200012]; struct Edge { int to, next; } e[200012]; inline void AddEdge(int x, int y) { e[++cnt] = (Edge){y, h[x]}; h[x] = cnt; } void dfs(int x) { if (vis[x]) return; vis[x] = 1; i...
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int gi() { int x = 0, o = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * o; } vec...
#include <bits/stdc++.h> int h[200010], SG[200010], sum[200010]; int fir[200010], nxt[200010], dis[200010], id; void link(int a, int b) { nxt[++id] = fir[a], fir[a] = id, dis[id] = b; } int que[200010], hd = 1, tl = 1, in[200010]; int c[200010]; int main() { int n, m, a, b; scanf("%d%d", &n, &m); for (int i = 1; ...
#include <bits/stdc++.h> using namespace std; struct edge { int to, nxxt; } e[200005 << 1]; queue<int> q; vector<int> v[200005]; bool vis[200005]; int head[200005], cnt = 1, n, m, a[200005], id[200005], b[200005], tot, rd[200005], tb[200005], xr[200005], mx; inline void ins(int u, int v) { e[cnt] ...
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; struct fastio { char s[100000]; int it, len; fastio() { it = len = 0; } inline char get() { if (it < len) return s[it++]; it = 0; len = fread(s, 1, 100000, stdin); if (len == 0) return EOF; else return s[it++]...
#include <bits/stdc++.h> using namespace std; const int c = 200002; queue<int> q; vector<int> sz[c], s[c], f[c]; long long n, m, be[c], me[c], t[c], xo[c], maxi; int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> t[i], f[i].push_back(c); for (int i = 1; i <= m; i++...
#include <bits/stdc++.h> using namespace std; const int NN = 200011; vector<int> adj[NN]; int n, m; int h[NN]; int xo[NN]; int g[NN]; int ans[NN]; int vst[NN]; int runs = 1; int calc(int u) { if (~g[u]) return g[u]; for (int v : adj[u]) calc(v); runs++; for (int v : adj[u]) vst[g[v]] = runs; for (int i = 0;; ...
#include <bits/stdc++.h> using namespace std; int n, m, SG[200005], Mx, h[200005], sum[200005]; bool vis[200005]; int fir[200005], nxt[200005], to[200005], tot; inline void line(int x, int y) { nxt[++tot] = fir[x]; fir[x] = tot; to[tot] = y; } void dfs(int u) { for (int i = fir[u]; i; i = nxt[i]) if (!SG[to...
#include <bits/stdc++.h> using std::cerr; using std::endl; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int MAXN = 200005; int ...
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { x = 0; char c = getchar(); bool flag = false; while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } if (flag)...
#include <bits/stdc++.h> using namespace std; const int c = 200002; queue<int> q; vector<int> sz[c], s[c], f[c]; long long n, m, be[c], me[c], t[c], xo[c], maxi; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> t[i], f[i].push_back(c); for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b;...
#include <bits/stdc++.h> using namespace std; void pr_init() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } vector<vector<long long> > tree; vector<long long> deg; void solve() { long long n; cin >> n; tree.assign(n + 1, vector<long long>()); deg.assign(n + 1, 0); for (long long i = 0; i < n...
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ind[n]; memset(ind, 0, sizeof(ind)); int u, v; for (int i = 0; i < n - 1; i++) { cin >> u >> v; u--; v--; ind[u]++; ind[v]++; } bool f = 1; int c1 = 0, c2 = 0; for (int i = 0; i < n; i++) { if (in...
#include <bits/stdc++.h> using namespace std; int power(long long int x, long long int y) { long long int temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } bool prime_check(long long int n) { long long int i, j; if (n == 1) { ...
#include <bits/stdc++.h> using namespace std; int n, m, i, j, p, q; vector<int> G[100005]; int main() { cin >> n; for (i = 0; i < n - 1; i++) { cin >> p >> q; G[p - 1].push_back(q - 1); G[q - 1].push_back(p - 1); } bool flag = false; for (i = 0; i < n; i++) { if (G[i].size() == 2) { flag...
#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...
#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"; ; ...
#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++) { ...
#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++) i...
#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() ...
#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 << "Y...
#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 =...
#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 mai...
#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"; ...
#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 <...
#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...
#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...
#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; c...
#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); } ...
#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] ...
#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_ba...
#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+...
#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;...
#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...
#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) { ...
#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...