vector <int> ve[maxn]; int root; int pa[maxlog][maxn]; //向上走2^k步到达的节点(超过根设为-1) int dep[maxn]; //节点深度,从0开始
voiddfs(int u, int p, int d){ pa[0][u] = p; dep[u] = d; for (int i = 0; i < ve[u].size(); i ++){ if (ve[u][i] != p) dfs(ve[u][i], u, d + 1); } }
voidinit(){ //memset(pa, -1, sizeof(pa)); //预处理出pa[0]和dep数组 dfs(root, -1, 0); //预处理pa数组 for (int k = 0; k + 1 < maxlog; k ++){ for (int i = 1; i <= n; i ++){ if (pa[k][i] < 0) pa[k + 1][i] = -1; else pa[k + 1][i] = pa[k][pa[k][i]]; } } }
intlca(int u, int v){ //让u和v先到达同一深度 if (dep[u] > dep[v]) swap(u, v); for (int k = 0; k < maxlog; k ++){ if ((dep[v] - dep[u]) >> k & 1){ v = pa[k][v]; } } if (u == v) return u; //二分搜索计算lca for (int k = maxlog - 1; k >= 0; k --){ if (pa[k][u] != pa[k][v]){ u = pa[k][u]; v = pa[k][v]; } } return pa[0][u]; }
#include<iostream> #include<cstdio> #include<vector> #include<cstring> usingnamespace std; int n,m; #define maxn 4010 #define maxlog 13 vector <int> ve[maxn]; int ans[maxn]; int ru[maxn]; int root; int pa[maxlog][maxn]; int dep[maxn]; voiddfs(int u, int p, int d){ pa[0][u] = p; dep[u] = d; for (int i = 0; i < ve[u].size(); i ++){ if (ve[u][i] != p) dfs(ve[u][i], u, d + 1); } } voidinit(){ //memset(pa, -1, sizeof(pa)); dfs(root, -1, 0); for (int k = 0; k + 1 < maxlog; k ++){ for (int i = 1; i <= n; i ++){ if (pa[k][i] < 0) pa[k + 1][i] = -1; else pa[k + 1][i] = pa[k][pa[k][i]]; } } } intlca(int u, int v){ if (dep[u] > dep[v]) swap(u, v); for (int k = 0; k < maxlog; k ++){ if ((dep[v] - dep[u]) >> k & 1){ v = pa[k][v]; } } if (u == v) return u; //二分搜索计算lca for (int k = maxlog - 1; k >= 0; k --){ if (pa[k][u] != pa[k][v]){ u = pa[k][u]; v = pa[k][v]; } } return pa[0][u]; } intmain(){ while (~scanf("%d", &n)){ memset(ans, 0, sizeof(ans)); memset(ru, 0, sizeof(ru)); int u,pnum,v; for (int i = 1; i <= n; i ++){ scanf("%d:(%d)", &u, &pnum); ve[u].clear(); for (int j = 0; j < pnum; j ++){ scanf("%d", &v); ve[u].push_back(v); ru[v] ++; } } for (int i = 1; i <= n; i++){ if (ru[i] == 0) { root = i; break; } } init(); scanf("%d", &m); char c; for (int i = 0; i < m; i ++){ while(~scanf("%c", &c)){ if (c == '('){ scanf("%d %d)", &u, &v); ans[lca(u,v)] ++; break; } } } for (int i = 1; i <= n; i ++){ if (ans[i] > 0){ printf("%d:%dn", i, ans[i]); } } } return0; }
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<vector> usingnamespace std; structInfo{ int v,len; Info(int a,int b){ v = a; len = b; } }; #define maxn 100100 #define maxlogv 20 vector <Info> ve[maxn]; int n,m; int pa[maxlogv][maxn]; int dep[maxn]; int dp[maxlogv][maxn]; voiddfs(int u, int p, int d){ pa[0][u] = p; dep[u] = d; for (int i = 0; i < ve[u].size(); i ++){ if (ve[u][i].v != p){ dp[0][ve[u][i].v] = ve[u][i].len; dfs(ve[u][i].v, u, d + 1); } } } voidinit(){ memset(dp, 0, sizeof(dp)); dfs(1, -1, 0); for (int k = 0; k + 1 < maxlogv; k ++){ for (int v = 1; v <= n; v ++){ if (pa[k][v] < 0) { pa[k + 1][v] = -1; dp[k + 1][v] = dp[k][v]; }else { pa[k + 1][v] = pa[k][pa[k][v]]; dp[k + 1][v] = dp[k][v] + dp[k][pa[k][v]]; } } } } intlca(int u,int v){ int ret = 0; if (dep[u] > dep[v]) swap(u, v); for (int k = 0; k < maxlogv; k ++){ if ((dep[v] - dep[u]) >> k & 1){ ret += dp[k][v]; v = pa[k][v]; } } if (u == v) return ret; for (int k = maxlogv; k >= 0; k --){ if (pa[k][u] != pa[k][v]){ ret += dp[k][u]; ret += dp[k][v]; u = pa[k][u]; v = pa[k][v]; } } //return pa[0][u]; return ret + dp[0][u] + dp[0][v]; } intmain(){ char s[100]; int u,v,len; while (~scanf("%d%d", &n, &m)){ for (int i = 1; i <= n; i ++){ ve[i].clear(); } for (int i = 1; i <= m; i ++){ scanf("%d%d%d%s", &u, &v, &len, s); ve[u].push_back(Info(v,len)); ve[v].push_back(Info(u,len)); } init(); int k; scanf("%d", &k); for (int i = 0; i < k; i ++){ scanf("%d%d", &u, &v); printf("%dn", lca(u,v)); } } return0; }