看到数据范围,考虑网络流..但考的时候完全不知道怎么建图
考虑流量表示选的点个数,费用表示选点的收益,跑最大费用最大流
那么我用一个点x表示某树中的询问点x,刨去它子孙询问点的子树后的子树
对于树1,连边S->x,流量为x的限定数-孩子询问的限定数,费用为0
对于树2,连边x->T,流量为x的限定数-孩子询问的限定数,费用为0
以限制数量
之后对于每个点,给它在树1中最近的询问祖先到树2中最近的询问祖先 连边,流量为1,费用为点权
表示选这个点
如果S的出流量和不等于T的入流量和,或者最后没跑满,则说明无解
1 #include2 #define CLR(a,x) memset(a,x,sizeof(a)) 3 #define MP make_pair 4 using namespace std; 5 typedef long long ll; 6 typedef unsigned long long ull; 7 typedef pair pa; 8 const int maxn=505; 9 10 inline ll rd(){ 11 ll x=0;char c=getchar();int neg=1; 12 while(c<'0'||c>'9'){ if(c=='-') neg=-1;c=getchar();} 13 while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); 14 return x*neg; 15 } 16 17 int teg[2][maxn*2][2],tegh[2][maxn],tect[2]; 18 int dem[2][maxn],cfa[2][maxn],rt[2]; 19 inline void adteg(int o,int a,int b){ 20 teg[o][++tect[o]][0]=b,teg[o][tect[o]][1]=tegh[o][a],tegh[o][a]=tect[o]; 21 teg[o][++tect[o]][0]=a,teg[o][tect[o]][1]=tegh[o][b],tegh[o][b]=tect[o]; 22 } 23 struct Edge{ 24 int b,l,c,ne; 25 }eg[maxn*6]; 26 int N,egh[maxn*2],ect=1,S=1004,T=1005; 27 int val[maxn],inl,outl; 28 29 inline void ext(){ 30 printf("-1\n"); 31 exit(0); 32 } 33 34 inline void adeg(int a,int b,int l,int c){ 35 if(a==S) inl+=l; 36 if(b==T) outl+=l; 37 eg[++ect]=(Edge){b,l,c,egh[a]},egh[a]=ect; 38 eg[++ect]=(Edge){a,0,-c,egh[b]},egh[b]=ect; 39 } 40 inline int dfs(int o,int x,int f,int cl){ 41 if(dem[o][x]) cl=x; 42 cfa[o][x]=cl; 43 int n=0; 44 for(int i=tegh[o][x];i;i=teg[o][i][1]){ 45 int b=teg[o][i][0];if(b==f) continue; 46 n+=dfs(o,b,x,cl); 47 } 48 if(dem[o][x]){ 49 if(dem[o][x]