注视一切的终结
注视一切的终结
题目大意
给出一个
一条路径的权值就是相邻的两条边的
有
简单路径:路径上的所有顶点不重复。
思路
倍增、
显然,对于两个点之间的边,我们只用维护至多
设
那么
转移是枚举
对于每个询问
设他们的
若其中一个点是
代码超级难调
code
#include <bits/stdc++.h>
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
#define fd(x , y , z) for(int x = y ; x >= z ; x --)
using namespace std;
const int N = 5e5 + 5 , M = 1e6 + 5;
int ecnt , hd[N] , fa[N][21] , dep[N] , vis[N] , cnt[N] , col[N][4] , f[N][21][4][4] , n , m;
struct E {
int to , nt , w;
} e[M << 1];
void add (int x , int y , int z) { e[++ecnt].to = y , e[ecnt].w = z , e[ecnt].nt = hd[x] , hd[x] = ecnt; }
int Lca (int x , int y) {
if (dep[x] < dep[y]) swap (x , y);
fd (i , 20 , 0)
if (dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if (x == y)
return x;
fd (i , 20 , 0)
if (fa[x][i] != fa[y][i])
x = fa[x][i] , y = fa[y][i];
return fa[x][0];
}
void dfs1 (int x) {
vis[x] = 1;
fu (i , 1 , 20)
fa[x][i] = fa[fa[x][i - 1]][i - 1];
int y;
for (int i = hd[x] ; i ; i = e[i].nt) {
y = e[i].to;
if (y == fa[x][0]) {
if (cnt[x] == 3) continue;
int flg = 0;
fu (j , 1 , cnt[x])
if (col[x][j] == e[i].w) {
flg = 1;
break;
}
if (flg) continue;
col[x][++cnt[x]] = e[i].w;
}
if (vis[y]) continue;
dep[y] = dep[x] + 1;
fa[y][0] = x;
dfs1 (y);
}
}
void dfs2 (int x) {
vis[x] = 1;
int y;
if (x != 1) {
y = fa[x][0];
fu (j , 1 , cnt[x])
fu (k , 1 , cnt[fa[x][0]])
f[x][0][j][k] = (col[x][j] != col[fa[x][0]][k]);
}
int z;
for(int i=1;(1<<i)<=dep[x];i++) {
y = fa[x][i - 1] , z = fa[x][i];
fu (a , 1 , cnt[x]) {
fu (b , 1 , cnt[y]) {
fu (c , 1 , cnt[z]) {
f[x][i][a][c] = max (f[x][i][a][c] , f[x][i - 1][a][b] + f[y][i - 1][b][c]);
}
}
}
}
for (int i = hd[x] ; i ; i = e[i].nt) {
y = e[i].to;
if (vis[y]) continue;
dfs2 (y);
}
}
int jump (int x , int d , int c[]) {
int cc[5] , y;
fd (i , 20 , 0) {
if (dep[fa[x][i]] > d) {
y = fa[x][i];
memset (cc , 0 , sizeof (cc));
fu (a , 1 , cnt[x])
fu (b , 1 , cnt[y])
cc[b] = max (cc[b] , c[a] + f[x][i][a][b]);
x = fa[x][i];
memcpy (c , cc , sizeof (cc));
}
}
return x;
}
int main () {
// freopen ("a.in" , "r" , stdin);
// freopen ("c.out" , "w" , stdout);
int u , v , w;
scanf ("%d%d" , &n , &m);
fu (i , 1 , m) {
scanf ("%d%d%d" , &u , &v , &w);
add (u , v , w) , add (v , u , w);
}
dep[1] = 1;
dfs1 (1);
fu (i , 1 , n) vis[i] = 0;
dfs2 (1);
int T , x , y , lca , fx , fy , cx[5] , cy[5] , ans;
scanf ("%d" , &T);
int ans1 = 0;
while (T --) {
memset (cx , 0 , sizeof (cx)) , memset (cy , 0 , sizeof (cy));
fx = fy = 0;
scanf ("%d%d" , &x , &y);
lca = Lca (x , y);
if (x != lca) fx = jump (x , dep[lca] , cx);
if (y != lca) fy = jump (y , dep[lca] , cy);
ans = 0;
if (x == lca)
fu (i , 1 , cnt[fy])
ans = max (ans , cy[i]);
else if (y == lca)
fu (i , 1 , cnt[fx])
ans = max (ans , cx[i]);
else {
fu (i , 1 , cnt[fx]) {
fu (j , 1 , cnt[fy]) {
ans = max (ans , cx[i] + cy[j] + (col[fx][i] != col[fy][j]));
}
}
}
printf ("%d\n" , ans);
}
return 0;
}
如果人生会有很长,愿有你的荣耀永不散场
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端