inlineboolgetmin(int &a, int b){return (a > b ? (a = b, true) : false);} inlineboolgetmax(int &a, int b){return (a < b ? (a = b, true) : false);}
constint N = 110007; constint inf = 1000000000;
namespace Hopcroft_Karp {
bool vis[N]; vector<int> e[N]; int nl, nr, ml[N], mr[N], dl[N], dr[N]; // m for match, d for distance
inlineboolbfs(){ staticint q[N], hd, tl; hd = 1; tl = 0; memset(dl, -1, sizeof(int) * (nl + 1)); memset(dr, -1, sizeof(int) * (nr + 1)); for (int i = 1; i <= nl; ++i) if (!ml[i]) {dl[i] = 0; q[++tl] = i;} int dT = inf; while (hd <= tl) { int u = q[hd++]; if (dl[u] >= dT) break; for (auto v : e[u]) if (dr[v] == -1) { dr[v] = dl[u] + 1; if (!mr[v]) getmin(dT, dr[v] + 1); else {dl[mr[v]] = dr[v] + 1; q[++tl] = mr[v];} } } return dT != inf; }
booldfs(int u){ for (auto v : e[u]) { if (vis[v] || dl[u] + 1 != dr[v]) continue; vis[v] = true; if (!mr[v] || dfs(mr[v])) {mr[v] = u; ml[u] = v; returntrue;} } returnfalse; }
inlinevoidadd(int u, int v){e[u].push_back(v);}
inlineintmax_matching(){ int ans = 0; while(bfs()) { memset(vis, 0, sizeof(bool) * (nr + 1)); for (int i = 1; i <= nl; ++i) if (!ml[i]) ans += dfs(i); } return ans; } }
int tot, cnts;
bool vis[N];
unordered_map<string, int> tr;
map<vector<int>, int> id;
intmain(){ int n; cin >> n; for (int i = 1; i <= n; ++i) { int k; cin >> k; vector<int> course(k); for (int j = 0; j < k; ++j) { string str; cin >> str; if (!tr[str]) tr[str] = ++cnts; course[j] = tr[str]; } sort(all(course)); vector<int> subset(1 << k); for (int S = 1; S < (1 << k); ++S) { vector<int> tmp(__builtin_popcount(S)); for (int j = 0, tmpcnt = 0; j < k; ++j) if (S & (1 << j)) tmp[tmpcnt++] = course[j]; if (!id[tmp]) id[tmp] = ++tot; subset[S] = id[tmp]; } for (int S = 1; S < (1 << k); ++S) { int nwid = subset[S]; if (vis[nwid]) continue; vis[nwid] = true; for (int s = (S & (S - 1)); s; s = (S & (s - 1))) Hopcroft_Karp::add(nwid, subset[s]); } } Hopcroft_Karp::nl = Hopcroft_Karp::nr = tot; printf("%d\n", tot - Hopcroft_Karp::max_matching()); return0; }
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; }
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; }
intrd(){ 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; }
intrd(){ 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; }
int n,te;char s[maxn+5],t[maxn+5]; int pl,ro,son[maxt+5][maxi],fai[maxt+5],MAX[maxt+5];
inlineintnewnode(){pl++;return pl;} intExtend(int p,int c){ int np=newnode();MAX[np]=MAX[p]+1; while (p && !son[p][c]) son[p][c]=np,p=fai[p]; if (!p) {fai[np]=ro;return np;} int q=son[p][c];if (MAX[p]+1==MAX[q]) {fai[np]=q;return np;} int nq=newnode();MAX[nq]=MAX[p]+1; for (int i=0;i<maxi;i++) son[nq][i]=son[q][i]; fai[nq]=fai[q];fai[q]=fai[np]=nq; while (p && son[p][c]==q) son[p][c]=nq,p=fai[p]; return np; } intmain(){ scanf("%s",s+1);ro=newnode(); for (int i=1,p=ro;s[i];i++) p=Extend(p,s[i]-'A'); for (scanf("%d",&te);te;te--){ scanf("%s",t+1); int ans=0; for (int i=1,p=ro;t[i];i++){ p=son[p][t[i]-'A']; if (!p){ p=son[ro][t[i]-'A'];ans++; if (!p) {ans=-2;break;} } } printf("%d\n",ans+1); } return0; }
intrd(){ 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; }
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= m; ++i) for (int k = rd(); k; --k) e[i].pb(rd()); q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : e[u]) { if (!deg[v]) q.push(v); ++deg[v]; } } int ans2 = 0; for (int i = m + 1; i <= n; ++i) ans2 += (deg[i] > 0); q.push(1); f[1] = 1; while (!q.empty()) { int u = q.front(); q.pop(); for (auto v : e[u]) { f[v] = (f[v] + f[u]) % mod; --deg[v]; if (!deg[v]) q.push(v); } } int ans1 = 0; for (int i = m + 1; i <= n; ++i) ans1 = (ans1 + f[i]) % mod; printf("%d %d\n", ans1, ans2); return0; }
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; }
voidbuild(int rt, int l, int r){ if (l == r) {mx[rt] = h[l]; return;} build(ls, l, mid); build(rs, mid + 1, r); pushup(rt); }
voidupd(int rt, int l, int r, int p, int x){ if (l == r) {mx[rt] = x; return;} p <= mid ? upd(ls, l, mid, p, x) : upd(rs, mid + 1, r, p, x); pushup(rt); }
intqmax(int rt, int l, int r, int L, int R){ if (L <= l && r <= R) return mx[rt]; int ans = 0; if (L <= mid) ans = max(ans, qmax(ls, l, mid, L, R)); if (R > mid) ans = max(ans, qmax(rs, mid + 1, r, L, R)); return ans; }
intnxtr(int rt, int l, int r, int L, int w){ if (l == r) return l; if (L <= mid && mx[ls] > w) { int res = nxtr(ls, l, mid, L, w); if (res > 0) return res; } if (mx[rs] <= w) return-1; return nxtr(rs, mid + 1, r, L, w); }
intnxtl(int rt, int l, int r, int L, int w){ if (l == r) return l; if (L > mid && mx[rs] > w) { int res = nxtl(rs, mid + 1, r, L, w); if (res > 0) return res; } if (mx[ls] <= w) return-1; return nxtl(ls, l, mid, L, w); }
unordered_map<int, int> pos;
intmain(){ int n = rd(), m = rd(), mxpos = 1; for (int i = 1; i <= n; ++i) { h[i] = rd(); pos[h[i]] = i; } build(1, 1, n); for (int i = 1; i <= m; ++i) { char c = getchar(); while (!isalpha(c)) c = getchar(); if (c == 'U') { int p = rd(); pos[h[p] = rd()] = p; upd(1, 1, n, p, h[p]); } else { int p = rd(); int lmx = qmax(1, 1, n, 1, p); if (c == 'L' && lmx == h[p]) {printf("%d\n", p); continue;} int rmx = qmax(1, 1, n, p, n); if (c == 'R' && rmx == h[p]) {printf("%d\n", p); continue;} if (lmx < rmx) printf("%d\n", nxtr(1, 1, n, pos[lmx], lmx)); elseprintf("%d\n", nxtl(1, 1, n, pos[rmx], rmx)); } } return0; }
intrd(){ 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; }
intrd(){ 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; }
intmain(){ int n = rd(), m = rd(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { char c = getchar(); while (!isalpha(c)) c = getchar(); a[i][j] = (c == 'G'); f[i][j] = (a[i][j] == a[i][j - 1] ? f[i][j - 1] + 1 : 1); } int ans = 0; for (int j = 1; j <= m; ++j) { for (int i = 1; i <= n; ++i) { int len = 0; while (!s.empty() && f[s.top().fr][j] >= f[i][j]) { len += s.top().sc; ans = max(ans, min(f[s.top().fr][j], len)); s.pop(); } s.push(mp(i, len + 1)); } int len = 0; while (!s.empty()) { len += s.top().sc; ans = max(ans, min(f[s.top().fr][j], len)); s.pop(); } } printf("%d\n", ans * ans); return0; }
intrd(){ 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; }
intmain(){ int n = rd(), k = rd(), ans = 0; for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = n, cnt = 0; i; --i) { if (a[i + 1] - a[i] > k) cnt = 0; ++cnt; ans = max(ans, cnt); } printf("%d\n", ans); return0; }