• 完美理论(最大权闭合子图)


    /*
    最大权闭合子图模型 
    枚举根, 然后选择包含根的连通块
    那么就是选择儿子必须选择它的父亲
    依赖关系就能够建立了
    可以在这里提交 https://vijos.org/d/fastle/p/1011 
    */
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<queue>
    #define ll long long 
    #define M 111
    #define mmp make_pair
    using namespace std;
    int read()
    {
    	int nm = 0, f = 1;
    	char c = getchar();
    	for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    	for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
    	return nm * f;
    }
    const int inf = 0x3e3e3e3e;
    int head[M], to[M * M], nxt[M * M], a[M], cap[M * M], flow[M * M], deep[M], s, t, n, cnt;
    vector<int> to1[M], to2[M];
    void push(int vi, int vj, int fl)
    {
    //	cout << vi << " " << vj << " " << fl << "
    ";
    	cnt++, to[cnt] = vj, nxt[cnt] = head[vi], head[vi] = cnt, cap[cnt] = fl, flow[cnt] = 0;
    	cnt++, to[cnt] = vi, nxt[cnt] = head[vj], head[vj] = cnt, cap[cnt] = 0, flow[cnt] = 0; 
    }
    
    
    
    void init()
    {
    	n = read(), s = n + 1, t = s + 1;
    	for(int i = 1; i <= n; i++) vector<int>().swap(to1[i]), vector<int>().swap(to2[i]);
    	for(int i = 1; i <= n; i++) a[i] = read();
    	for(int i = 1; i < n; i++)
    	{
    		int vi = read(), vj = read();
    		to1[vi].push_back(vj);
    		to1[vj].push_back(vi);
    	}
    	for(int i = 1; i < n; i++)
    	{
    		int vi = read(), vj = read();
    		to2[vi].push_back(vj);
    		to2[vj].push_back(vi);
    	}
    }
    
    bool bfs(int be, int ed)
    {
    	memset(deep, 0, sizeof(deep));
    	deep[be] = 1;
    	queue<int> q;
    	q.push(be);
    	while(!q.empty())
    	{
    		int now = q.front();
    		q.pop();
    		for(int i = head[now]; i; i = nxt[i])
    		{
    			int vj = to[i];
    			if(deep[vj] || flow[i] >= cap[i]) continue;
    			deep[vj] = deep[now] + 1;
    			q.push(vj);
    			if(vj == ed) return true;
    		}
    	}
    	return false;
    }
    
    int dfs(int now, int ed, int fl)
    {
    	if(now == ed || fl == 0) return fl;
    	int tot = 0, f;
    	for(int i = head[now]; i; i = nxt[i])
    	{
    		int vj = to[i];
    		if(deep[vj] != deep[now] + 1) continue;
    		if(f = dfs(vj, ed, min(fl, cap[i] - flow[i])))
    		{
    		 	tot += f;
    		 	flow[i] += f;
    		 	flow[i ^ 1] -= f;
    		 	fl -= f;
    		}
    		if(fl == 0) break;
    	}
    	if(tot == 0) deep[now] = -1;
    	return tot;
    }
    
    void dfs1(int now, int fa)
    {
    	if(fa) push(now, fa, inf);
    	for(int i = 0; i < to1[now].size(); i++)
    	{
    		int vj = to1[now][i];
    		if(vj == fa) continue;
    		dfs1(vj, now);
    	}
    }
    
    void dfs2(int now, int fa)
    {
    	if(fa) push(now, fa, inf);
    	for(int i = 0; i < to2[now].size(); i++)
    	{
    		int vj = to2[now][i];
    		if(vj == fa) continue;
    		dfs2(vj, now);
    	}
    }
    
    int work(int now)
    {
    	cnt = 1;
    	memset(head, 0, sizeof(head));
    	dfs1(now, 0);
    	dfs2(now, 0);
    	int ans = 0;
    	for(int i = 1; i <= n; i++) if(a[i] >= 0) push(s, i, a[i]), ans += a[i];
    	else push(i, t, -a[i]);
    	while(bfs(s, t)) ans -= dfs(s, t, inf);
    	return ans;
    }
    
    
    int main()
    {
    	int T = read();
    	while(T--)
    	{
    		init();
    		int now = 0;
    		for(int i = 1; i <= n; i++) now = max(now, work(i));
    		cout << now << "
    ";
    	} 
    	return 0;
    }
    
  • 相关阅读:
    计算广告(1):计算广告基础
    shell定时任务
    shell脚本运行报错:-bash: xxx: /bin/sh^M: bad interpreter: No such file or directory
    spark文件读取与保存(scala实现)
    spark中的共享变量(广播变量和累加器)
    spark基本概念与运行架构
    用二维数组输出螺旋方阵(JAVA实现)
    InputStream、InputStreamReader和BufferedReader
    Arrays.sort()自定义排序的实现
    BufferedReader和Scanner
  • 原文地址:https://www.cnblogs.com/luoyibujue/p/10639697.html
Copyright © 2020-2023  润新知