inlinevoidadd(int x, int y, int z) { to[++ cnt] = y; len[cnt] = z; nex[cnt] = head[x]; head[x] = cnt; }
int dis[N], fa[N], v[N], tot, f[N<<1];
inlinevoidbfs(int now) { memset(dis, -1, sizeof dis); queue<int>q; int i, x, y; dis[now] = 0, q.push(now); while (!q.empty()) { x = q.front(); q.pop(); for (i = head[x]; i; i = nex[i]) { y = to[i]; if (dis[y] != -1) continue; fa[y] = x; if (v[y]) dis[y] = dis[x]; else dis[y] = dis[x] + len[i]; //对于前两个bfs,我们都没有用到v,显然这么写是没有问题的 //而对于第三个bfs,我们的v数组表示,直径上的点的距离都是零(人为规定) q.push(y); } } }
inlineintpd(int mid, int s) { int l = 1, r = tot; while (l <= tot && f[1] - f[l + 1] <= mid) ++ l; while (r >= 1 && f[r] <= mid) -- r; return f[l] - f[r + 1] <= s; /* while (r >= 1 && f[r + 1] <= mid) -- r; return f[l] - f[r] <= s; 这么写也是可以的 */ }
inlineintthestars() { int i, x, y, z, n, s, From(0), To(0); scanf ("%d%d", &n, &s); for (i = 1; i < n; ++ i) { scanf ("%d%d%d", &x, &y, &z); add(x, y, z); add(y, x, z); } bfs(1); for (i = 1; i <= n; ++ i) if (dis[i] > dis[From]) From = i; bfs(From); for (i = 1; i <= n; ++ i) if (dis[i] > dis[To]) To = i; int d = dis[To]; f[++ tot] = dis[To]; v[To] = 1; while (From != To) { f[++ tot] = dis[fa[To]]; To = fa[To]; v[To] = 1; } bfs(To); int l = 0, r = d, mid; for (i = 1; i <= n; ++ i) l = max(l, dis[i]); while (l < r) { mid = (l + r) >> 1; //我们尝试在左右两边都减掉mid,然后再判断剩余的长度是否满足题目中要求的长度 if (pd(mid, s)) r = mid; else l = mid + 1; } cout << l; return0; }
//Author:XuHt #include<cstdio> #include<iostream> #include<algorithm> usingnamespace std; constint N = 2506; int n, m; structCOW { int l, r; booloperator < (const COW x) const { return l > x.l; } } cow[N]; structSPF { int s, c; booloperator < (const SPF x) const { return s > x.s; } } spf[N];
intmain(){ cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%d %d", &cow[i].l, &cow[i].r); for (int i = 1; i <= m; i++) scanf("%d %d", &spf[i].s, &spf[i].c); sort(cow + 1, cow + n + 1); sort(spf + 1, spf + m + 1); int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (spf[j].c && spf[j].s >= cow[i].l && spf[j].s <= cow[i].r) { ans++; spf[j].c--; break; } cout << ans << endl; return0; }
inlinevoiddfs(int x) { int i, y; for (i = 1; i <= S; ++ i) { if (f[x][i - 1]) f[x][i] = f[f[x][i - 1]][i - 1]; elsebreak; } for (i = head[x]; i; i = nex[i]) { y = to[i]; if (y != f[x][0]) { f[y][0] = x; dep[y] = dep[x] + 1; dfs(y); } } }
inlineintLCA(int x, int y) { int i; if (dep[x] < dep[y]) swap(x, y); int delta = dep[x] - dep[y]; for (i = 0; i <= S; ++ i) if ((1 << i) & delta) x = f[x][i]; if (x == y) return x; for (i = S; i >= 0; -- i) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; return f[x][0]; }
inlineintthestars() { int i, n, m, x, y; int a, b, c, d; scanf ("%d%d", &n, &m); for (i = 1; i < n; ++ i) { scanf ("%d%d", &x, &y); add(x, y), add(y, x); } dfs(1); for (i = 1; i <= m; ++ i) { scanf ("%d%d%d%d", &a, &b, &c, &d); x = LCA(a, b), y = LCA(c, d); if (dis(a, y) + dis(y, b) == dis(a, b) || dis(c, x) + dis(x, d) == dis(c, d)) puts("Y"); elseputs("N"); } return0; }
#include<cstdio> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cmath> #define ll long long usingnamespace std; template <typename T> inlinevoid _read(T& x){ char ch=getchar();bool sign=true; while(!isdigit(ch)){if(ch=='-')sign=false;ch=getchar();} for(x=0;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+ch-'0'; if(!sign)x=-x; } int n,k,K; int t[55],a[55],b[55],c[55]; int id[55][55]; int cnt[55]; int Type[55];