inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 107
ll gcd(ll a, ll b){return b ? gcd(b, a % b) : a;}
bool vis[N];
int a[N], deg[N];
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) ++deg[a[i] = rd()]; ll ans = 1; for (int i = 1; i <= n; ++i) { if (!deg[i]) {puts("-1"); return0;} if (!vis[i]) { int len = 0; for (int p = i; !vis[p]; p = a[p], ++len) vis[p] = true; if (!(len & 1)) len /= 2; ans = ans / gcd(ans, len) * len; } } printf("%lld\n", ans); return0; }
B -
Arpa's weak amphitheater and Mehrdad's valuable Hoses
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 1007
structDSU { int f[N]; inlinevoidreset(int n){for (int i = 1; i <= n; ++i) f[i] = i;} intfind(int x){return x == f[x] ? x : f[x] = find(f[x]);} inlineboolmerge(int x, int y){ x = find(x); y = find(y); return x == y ? false : (f[x] = y, true); } } dsu;
int w[N], v[N], f[N], g[N];
vector<int> s[N];
intmain(){ int n = rd(), m = rd(), tot = rd(); dsu.reset(n); for (int i = 1; i <= n; ++i) w[i] = rd(); for (int i = 1; i <= n; ++i) v[i] = rd(); for (int i = 1; i <= m; ++i) dsu.merge(rd(), rd()); for (int i = 1; i <= n; ++i) s[dsu.find(i)].push_back(i);
auto upd = [&](int W, int V) { for (int i = W; i <= tot; ++i) g[i] = max(g[i], f[i - W] + V); };
for (int i = 1; i <= n; ++i) { if (s[i].empty()) continue; int sumw = 0, sumv = 0; for (auto x : s[i]) { sumw += w[x]; sumv += v[x]; upd(w[x], v[x]); } upd(sumw, sumv); for (int i = 1; i <= tot; ++i) f[i] = max(f[i], g[i]); } printf("%d\n", f[tot]); return0; }
C -
Arpa’s overnight party and Mehrdad’s silent entering
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007 #define pb push_back
int ty[N], a[N], b[N];
vector<int> e[N];
voiddfs(int u){ for (auto v : e[u]) if (!ty[v]){ty[v] = 3 - ty[u]; dfs(v);} }
intmain(){ int n = rd(); for (int i = 1; i <= n; ++i) { a[i] = rd(); b[i] = rd(); e[a[i]].pb(b[i]); e[b[i]].pb(a[i]); int u = i * 2, v = i * 2 - 1; e[u].pb(v); e[v].pb(u); } for (int i = 1; i <= n * 2; ++i) if (!ty[i]) {ty[i] = 1; dfs(i);} for (int i = 1; i <= n; ++i) printf("%d %d\n", ty[a[i]], ty[b[i]]); return0; }
D
- Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
一棵树每条边上有一个字符 (a - v) ,每次询问 \(u_i\)
子树内最长的简单路径,满足其上的字符重排可形成回文串。
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
voiddfs(int u, int fa, int S){ sz[u] = 1; dep[u] = dep[fa] + 1; if (u != 1) S ^= (1 << ch[u]); sta[u] = S; for (auto v : son[u]) { dfs(v, u, S); sz[u] += sz[v]; if (sz[v] > sz[mxs[u]]) mxs[u] = v; } }
inlinevoidupd(int u){ // adding information of u into data structures mxd[sta[u]] = max(mxd[sta[u]], dep[u]); }
inlinevoiddel(int u){ // deleting information of u from data structures mxd[sta[u]] = 0; res = 0; }
voidupd(int u, int fa){ upd(u); for (auto v : son[u]) if (v != fa) upd(v, u); }
voiddel(int u, int fa){ del(u); for (auto v : son[u]) if (v != fa) del(v, u); }
voidupdans(int u, int del){ // 枚举配对的状态需要保证存在!!! if (mxd[sta[u]]) res = max(res, dep[u] + mxd[sta[u]] - 2 * del); for (int i = 0; i < 22; ++i) if (mxd[sta[u] ^ (1 << i)]) res = max(res, dep[u] + mxd[sta[u] ^ (1 << i)] - 2 * del); }
voidgetans(int u, int del){ updans(u, del); for (auto v : son[u]) getans(v, del); }
voiddsu(int u, int fa){ for (auto v : son[u]) if (v != fa && v != mxs[u]) {dsu(v, u); del(v, u);} if (mxs[u]) dsu(mxs[u], u); updans(u, dep[u]); upd(u); for (auto v : son[u]) if (v != fa && v != mxs[u]) { getans(v, dep[u]); res = max(res, ans[v]); upd(v, u); } ans[u] = res; }
intmain(){ int n = rd(); for (int i = 2; i <= n; ++i) { son[rd()].pb(i); char c = getchar(); while (!isalpha(c)) c = getchar(); ch[i] = (c - 'a'); } dfs(1, 1, 0); dsu(1, 1); for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); return0; }
E - Arpa’s
abnormal DNA and Mehrdad’s deep interest