标签:重构 ch int Kruskal dep ATP BZOJ3732 top define
Kruskal重构树上\(x\)和\(v\)的\(lca\)的权值即为它们最长路最小值
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long
#define ON_DEBUGG
#ifdef ON_DEBUGG
#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC)
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
#endif
using namespace std;
struct ios{
template<typename ATP>inline ios& operator >> (ATP &x){
x = 0; int f = 1; char ch;
for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
x *= f;
return *this;
}
}io;
template<typename ATP>inline ATP Max(ATP a, ATP b){
return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
return a < 0 ? -a : a;
}
const int N = 30007;
struct Edge{
int nxt, pre;
}e[N << 2];
int head[N], cntEdge;
inline void add(int u, int v){
e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}
struct node{
int x, y, w;
bool operator < (const node &com) const{
return w < com.w;
}
}a[N << 1];
int n, m;
int val[N];
namespace KRUS{
int fa[N];
inline int Find(int x){
return x == fa[x] ? x : fa[x] = Find(fa[x]);
}
inline void Kruskal(){
sort(a + 1, a + m + 1);
int tot = n + 1, lim = n << 1;
R(i,1,lim) fa[i] = i;//, siz[i] = 1;
R(i,1,m){
int p = Find(a[i].x), q = Find(a[i].y);
if(p != q){
fa[p] = fa[q] = tot;
val[tot] = a[i].w;
add(tot, p), add(tot, q);
// add(p, tot), add(q, tot);
if(++tot >= lim) break;
}
}
}
}
namespace TCP{
int fa[N], top[N], son[N], siz[N], dep[N];
inline void DFS_First(int u, int father){
fa[u] = father, siz[u] = 1, dep[u] = dep[father] + 1;
for(register int i = head[u]; i;i = e[i].nxt){
int v = e[i].pre;
if(v == father) continue;
DFS_First(v, u);
siz[u] += siz[v];
if(siz[v] > siz[son[u]]) son[u] = v;
}
}
inline void DFS_Second(int u, int TP){
top[u] = TP;
if(!son[u]) return;
DFS_Second(son[u], TP);
for(register int i = head[u]; i; i = e[i].nxt){
int v = e[i].pre;
if(v != fa[u] && v != son[u])
DFS_Second(v, v);
}
}
inline int LCA(int x, int y){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) Swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
}
int main(){
freopen("3732Network.in", "r", stdin);
freopen("3732Network.out", "w", stdout);
int Q;
io >> n >> m >> Q;
R(i,1,m){
io >> a[i].x >> a[i].y >> a[i].w;
}
KRUS::Kruskal();
int root = (n << 1) - 1; // root is 2 * n - 1
TCP::DFS_First(root, 0);
TCP::DFS_Second(root, root);
while(Q--){
int u, v;
io >> u >> v;
add(u, v);
add(v, u);
printf("%d\n", val[TCP::LCA(u, v)]);
}
return 0;
}
标签:重构,ch,int,Kruskal,dep,ATP,BZOJ3732,top,define 来源: https://www.cnblogs.com/bingoyes/p/11616957.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。