LuoguP3128 [USACO15DEC]最大流Max Flow (树上差分)

跟LOJ10131暗的连锁 相似,只是对于(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 Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#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("
----------
") 
#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, "
time: %.3fms
", 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 = 500007;

struct Edge{
	int nxt, pre;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v){
	e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}

int siz[N], son[N], fa[N], dep[N], top[N];
inline void DFS_First(int u, int father){
	dep[u] = dep[father] + 1, fa[u] = father, siz[u] = 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(!son[u] || 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 n, m;
long long ans, sum[N];
inline void DFS_Ans(int u){
	for(register int i = head[u]; i; i = e[i].nxt){
		int v = e[i].pre;
		if(v == fa[u]) continue;
		DFS_Ans(v);
		sum[u] += sum[v];
		ans = max(ans, sum[v]);
	}
}
int main(){
//FileOpen();

	io >> n >> m;
	R(i,2,n){
		int u, v;
		io >> u >> v;
		add(u, v);
		add(v, u);
	}
	
	DFS_First(1, 0);
	DFS_Second(1, 1);
	
	R(i,1,m){
		int u, v;
		io >> u >> v;
		int lca = LCA(u, v);
		++sum[u], ++sum[v];
		--sum[lca], --sum[fa[lca]];
	}
	
	DFS_Ans(1);
	
	printf("%lld
", ans);
	
	return 0;
}
原文地址:https://www.cnblogs.com/bingoyes/p/11496309.html