#include<bits/stdc++.h>usingnamespace std;
constint N = 3e4 + 5, M = 6e4 + 5;
int h[N], e[M], ne[M], idx;
int depth[N], f[N][20], fa[N], mark[N], dfn[M], low[M], timestamp;
structPoint {
int a, b;
}Data[N][2];
intsum_t, ans_ver;
voidadd(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
voiddfs(int u, int pa){
dfn[u] = ++ timestamp;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (j == pa)
continue;
fa[j] = u;
dfs(j, u);
}
low[u] = timestamp;
}
voidbfs(int u){
memset(depth, 0x3f, sizeof depth);
queue<int> q;
q.push(u);
depth[0] = 0, depth[u] = 1;
while (q.size()) {
int t = q.front();
q.pop();
for (int i = h[t]; i != -1; i = ne[i]) {
int j = e[i];
if (depth[j] > depth[t] + 1) {
depth[j] = depth[t] + 1;
f[j][0] = t;
q.push(j);
for (int k = 1; k <= 13; k ++)
f[j][k] = f[f[j][k - 1]][k - 1];
}
}
}
}
intlca(int a, int b){
if (depth[a] < depth[b])
swap(a, b);
for (int i = 13; i >= 0; i --) {
if (depth[f[a][i]] >= depth[b]) {
a = f[a][i];
}
}
if (a == b)
return a;
for (int i = 13; i >= 0; i --) {
if (f[a][i] != f[b][i]) {
a = f[a][i];
b = f[b][i];
}
}
return f[a][0];
}
intexgcd(int a, int b, int &x, int &y){
if (!b) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= (a / b) * x;
return d;
}
voidcal(int u, Point A, Point B){
int a = A.a, b = -B.a, m1 = A.b, m2 = B.b, c = m2 - m1, x, y, d;
c = (c % B.a + B.a) % B.a;
d = exgcd(a, b, x, y);
if (c % d)
return;
a /= d, b /= d, c /= d;
x *= c, y *= c;
b = abs(b);
x = (x % b + b) % b;
if (x * A.a + m1 < sum_t) {
sum_t = x * A.a + m1;
ans_ver = u;
}
}
boolcheck(int a, int b){
return dfn[b] <= dfn[a] && dfn[a] <= low[b];
}
intmain(){
std::ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t --) {
memset(h, -1, sizeof h);
memset(f, 0, sizeof f);
memset(mark, 0, sizeof mark);
idx = timestamp = 0;
int n, m;
cin >> n >> m;
for (int i = 0; i < n - 1; i ++) {
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
dfs(1, -1);
bfs(1);
for (int i = 1; i <= m; i ++) {
int sa, ta, sb, tb;
cin >> sa >> ta >> sb >> tb;
if (sa == sb) {
cout << sa << endl;
continue;
}
int la = lca(sa, ta), lb = lca(sb, tb);
if (depth[la] > depth[lb]) {
swap(sa, sb);
swap(ta, tb);
swap(la, lb);
}
if (!check(sa, lb) && !check(ta, lb)) {
cout << -1 << endl;
continue;
} else {
int da = depth[sa] + depth[ta] - 2 * depth[la], db = depth[sb] + depth[tb] - 2 * depth[lb];
int p = sa;
while (1) {
Data[p][0] = (Point){2 * da, depth[sa] - depth[p]};
Data[p][1] = (Point){2 * da, 2 * da - (depth[sa] - depth[p])};
mark[p] = i;
if (p == la)
break;
p = fa[p];
}
p = ta;
while (p != la) {
Data[p][0] = (Point){2 * da, da - (depth[ta] - depth[p])};
Data[p][1] = (Point){2 * da, da + (depth[ta] - depth[p])};
mark[p] = i;
p = fa[p];
}
sum_t = 0x3f3f3f3f;
ans_ver = -1;
p = sb;
while (1) {
Point A = (Point){2 * db, depth[sb] - depth[p]};
Point B = (Point){2 * db, 2 * db - (depth[sb] - depth[p])};
if (mark[p] == i) {
cal(p, Data[p][0], A), cal(p, Data[p][0], B);
cal(p, Data[p][1], A), cal(p, Data[p][1], B);
}
if (p == lb)
break;
p = fa[p];
}
p = tb;
while (p != lb) {
Point A = (Point){2 * db, db - (depth[tb] - depth[p])};
Point B = (Point){2 * db, db + (depth[tb] - depth[p])};
if (mark[p] == i) {
cal(p, Data[p][0], A), cal(p, Data[p][0], B);
cal(p, Data[p][1], A), cal(p, Data[p][1], B);
}
p = fa[p];
}
cout << ans_ver << endl;
}
}
}
return0;
}
#include<bits/stdc++.h>usingnamespace std;
intmain(){
std::ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t --) {
int n, m;
cin >> n >> m;
unordered_map<int, string> mp;
for (int i = 1; i <= n; i ++) {
string s, s2;
cin >> s;
s2 = s;
s = s + s;
for (int j = 0; j < m; j ++) {
string s3 = s.substr(j, m);
if (s2 > s3)
s2 = s3;
}
mp[i] = s2;
}
int q;
cin >> q;
while (q --) {
int a, b;
cin >> a >> b;
if (mp[a] == mp[b])
cout << "Yes" << endl;
else cout << "No" << endl;
}
}
return0;
}
4|01009 Assertion
1|0题意:
Alice断言:m个物品分成n组,一定存在一组物品一定大于等于d。判断Alice的话对不对。
1|0分析:
抽屉原理,至少有一堆有有 + 1个物品,判断 + 1于d的大小关系即可
1|0代码:
#include<bits/stdc++.h>usingnamespace std;
intmain(){
std::ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t --) {
int n, m, d;
cin >> n >> m >> d;
int res = (m - 1) / n + 1;
if (res >= d)
cout << "Yes" << endl;
else cout << "No" << endl;
}
return0;
}
#include<bits/stdc++.h>usingnamespace std;
typedeflonglong LL;
constint N = 2e5 + 5, M = 4e5 + 5, mod = 1e9 + 7;
int h[N], e[M], ne[M], idx;
int f1[N], f2[N];
voidadd(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
LL qmi(LL m, LL k){
LL res = 1, t = m;
while (k) {
if (k & 1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}
voiddfs1(int u, int fa){
f1[u] = 0;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (j == fa)
continue;
dfs1(j, u);
f1[u] ^= (f1[j] + 1);
}
}
voiddfs2(int u, int fa){
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (j == fa)
continue;
f2[j] = f1[j] ^ ((f2[u] ^ (f1[j] + 1)) + 1);
dfs2(j, u);
}
}
intmain(){
std::ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t;
cin >> t;
while (t --) {
memset(h, -1, sizeof h);
memset(f1, 0, sizeof f1);
memset(f2, 0, sizeof f2);
idx = 0;
int n;
cin >> n;
for (int i = 0; i < n - 1; i ++) {
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
dfs1(1, -1);
f2[1] = f1[1];
dfs2(1, -1);
LL m = 0;
for (int i = 1; i <= n; i ++)
if (f2[i])
m ++;
cout << (m * qmi(n, mod - 2)) % mod << endl;
}
return0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效