本文主要是介绍洛谷P3931 SAC E#1 - 一道难题 Tree (最大流/最小割),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2020.7.11
今天晚上还有比赛,打完这场就不打了,从五月到今天天天刷题也太自闭了,一定得出去玩一阵子。
这道题貌似并不难,带权的有根树,问树根到叶节点不连通的最小代价(要素察觉),那么不就是把树根连源,叶子连汇然后跑一边最大流不就行了??交上去wa到只剩20,后来注意到给出的边的顺序不一定是树里面的顺序,所以肯定需要处理一下才行。我说我今天写这道题的时候怎么有种树链剖分的感觉呢??处理手法直接从树剖那里照搬过来,叶子节点的size大小肯定为1,在第一次dfs的时候连边,连完边顺便检查一下size是不是等于1,连汇就行了,再把树根连上超级源点就行了,思路还是蛮清晰的。
代码:
#include <bits/stdc++.h>
using namespace std;
#define limit (100000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(ll i = a; i <= b ; ++i)
#define per(i, a, b) for(ll i = b ; i >= a ; --i)
#define MOD 998244353
#define traverse(u) for(int i = head[u]; ~i ; i = edge[i].next)
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
#define FOUT freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\dabiao.txt", "wt", stdout)
typedef long long ll;
typedef unsigned long long ull;
ll read(){ll sign = 1, x = 0;char s = getchar();while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}while(s >= '0' && s <= '9'){x = (x << 3) + (x << 1) + s - '0';s = getchar();}return x * sign;
}//快读
void write(ll x){if(x < 0) putchar('-'),x = -x;if(x / 10) write(x / 10);putchar(x % 10 + '0');
}
int n,m,vs,ve,k;
int layer[limit],head[limit], cnt;
struct node{int to ,next;ll flow;
}edge[limit];
ll max_flow;
inline void add_one(int u , int v, ll flow){edge[cnt].to = v;edge[cnt].next = head[u];edge[cnt].flow = flow;head[u] = cnt++;
}
inline void add(int u, int v, ll flow){add_one(u,v,flow);add_one(v, u,0);
}
inline void init(bool flag = true){if(flag){memset(head, -1, sizeof(head));max_flow = cnt = 0;}else{memset(layer, -1, sizeof(layer));}
}
inline bool bfs(){init(false);queue<int>q;layer[vs] = 0;//从第0层开始q.push(vs);while (q.size()){int u = q.front();q.pop();traverse(u){int v = edge[i].to,flow = edge[i].flow;if(layer[v] == -1 && flow > 0){layer[v] = layer[u] + 1;//迭代加深q.push(v);}}}return ~layer[ve];
}
ll dfs(int u, ll flow){if(u == ve)return flow;ll rev_flow = 0,min_flow;traverse(u){int v =edge[i].to;ll t_flow = edge[i].flow;if(layer[v] == layer[u] + 1 && t_flow > 0){min_flow = dfs(v, min(flow, t_flow));flow -= min_flow;edge[i].flow -= min_flow;rev_flow += min_flow;edge[i^1].flow += min_flow;if(!flow)break;}}if(!rev_flow)layer[u] = -1;return rev_flow;
}
inline void dinic(){while (bfs()){max_flow += dfs(vs,inf);}
}
struct treenode{int to,next,val;
}edges[limit];
int sizes[limit],fa[limit],tot, head2[limit],rt;
inline void init2(){memset(head2, -1, sizeof(head2));tot = 0;
}
inline void add_tree_edge(int u, int v, int val = 0){edges[tot].to = v;edges[tot].next = head2[u];edges[tot].val = val;head2[u] = tot++;//存边
}
void dfs1(int u, int pre){sizes[u] = 1;fa[u] = pre;for(int i = head2[u] ; ~i ; i = edges[i].next){int v = edges[i].to, val = edges[i].val;if(v != pre){add(u,v,val);dfs1(v,u);sizes[u] += sizes[v];}}if(sizes[u] == 1)add(u,ve,INF);
}
int main() {
#ifdef LOCALFOPEN;
#endifinit();n = read(), rt = read();//树边和根init2();vs = n * 2 + 1, ve = vs + 1;rep(i ,1,n-1){int x = read(), y = read(), val = read();add_tree_edge(x,y,val);add_tree_edge(y,x,val);}add(vs,rt,INF);dfs1(rt, 0);dinic();write(max_flow);return 0;
}
这篇关于洛谷P3931 SAC E#1 - 一道难题 Tree (最大流/最小割)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!