• 【CF786E】ALT


    题目

    题目链接:https://codeforces.com/problemset/problem/786/E
    给出一棵 (n) 个点的树,有 (m) 个人,第 (i) 个人要从 (a_i) 走到 (b_i)
    树上的路径不是很安全,所以你可以给某一条边分配狗一条,或者给某一个人分配狗一条。
    如果一个人有狗一条,或者这个人在树上所走过的每一条边都有狗一条,那么这个人是安全的。
    求最少需要多少条狗使得所有人都是安全的。

    思路

    将原点向所有人连边,流量为 (1);所有边向汇点连边,流量为 (1)
    然后对于一个人的路径 (a o b),设 (operatorname{lca}(a,b)=p),那么需要将这个人向 (a o p)(p o b) 中所有路连一条流量为 (+infty) 的边。
    但是这样边数是 (O(nm)) 的,发现将每个人上行和下行道路分开后,均是树上连续的边,所以可以用倍增连边。
    具体的,将每一条边所对应网络图中的点拆成 (log) 份,第 (i) 份表示该点开始往上 (2^i) 条边。
    这样网络图中边数和点数就是 (O((n+m)log n)) 的了。

    代码

    #include <bits/stdc++.h>
    #define mp make_pair
    using namespace std;
    
    const int N=20010,LG=18,Inf=1e9;
    int n,m,S,T,maxf,tot=1,f[N][LG+1],dep[N*LG*2],head[N*LG*2],cur[N*LG*2],rID[N];
    vector<pair<int,int> > to[N];
    
    struct edge
    {
    	int next,to,flow;
    }e[N*LG*10];
    
    void add(int from,int to,int flow)
    {
    	e[++tot].to=to;
    	e[tot].flow=flow;
    	e[tot].next=head[from];
    	head[from]=tot;
    }
    
    int ID(int x,int k)
    {
    	return m+1+rID[x]+k*n;
    }
    
    void dfs(int x,int fa)
    {
    	f[x][0]=fa; dep[x]=dep[fa]+1;
    	if (x!=1) add(ID(x,0),T,1),add(T,ID(x,0),0);
    	for (int i=1;i<=LG;i++)
    	{
    		f[x][i]=f[f[x][i-1]][i-1];
    		if (f[x][i])
    		{
    			add(ID(x,i),ID(x,i-1),Inf); add(ID(x,i-1),ID(x,i),0);
    			add(ID(x,i),ID(f[x][i-1],i-1),Inf); add(ID(f[x][i-1],i-1),ID(x,i),0);
    		}
    	}
    	for (int i=0;i<to[x].size();i++)
    		if (to[x][i].first!=fa)
    		{
    			rID[to[x][i].first]=to[x][i].second;
    			dfs(to[x][i].first,x);
    		}
    }
    
    int lca(int x,int y)
    {
    	if (dep[x]<dep[y]) swap(x,y);
    	for (int i=LG;i>=0;i--)
    		if (dep[f[x][i]]>=dep[y]) x=f[x][i];
    	if (x==y) return x; 
    	for (int i=LG;i>=0;i--)
    		if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
    	return f[x][0];
    }
    
    void addedge(int id,int x,int p)
    {
    	for (int i=LG;i>=0;i--)
    		if (dep[f[x][i]]>=dep[p])
    		{
    			add(id,ID(x,i),Inf); add(ID(x,i),id,0);
    			x=f[x][i];
    		}
    }
    
    struct Dinic
    {
    	bool bfs()
    	{
    		memset(dep,0x3f3f3f3f,sizeof(dep));
    		memcpy(cur,head,sizeof(head));
    		queue<int> q;
    		q.push(S); dep[S]=0;
    		while (q.size())
    		{
    			int u=q.front(); q.pop();
    			for (int i=head[u];~i;i=e[i].next)
    			{
    				int v=e[i].to;
    				if (e[i].flow && dep[v]>dep[u]+1)
    				{
    					dep[v]=dep[u]+1;
    					q.push(v);
    				}
    			}
    		}
    		return dep[T]<Inf;
    	}
    	
    	int dfs(int x,int flow)
    	{
    		if (x==T)
    		{
    			maxf+=flow;
    			return flow;
    		}
    		int ret,used=0;
    		for (int i=cur[x];~i;i=e[i].next)
    		{
    			cur[x]=i;
    			int v=e[i].to;
    			if (e[i].flow && dep[v]==dep[x]+1)
    			{
    				ret=dfs(v,min(flow-used,e[i].flow));
    				e[i].flow-=ret; e[i^1].flow+=ret;
    				used+=ret;
    				if (used==flow) return flow;
    			}
    		}
    		return used;
    	}
    	
    	void work()
    	{
    		while (bfs()) dfs(S,Inf);
    	}
    	
    	void print()
    	{
    		queue<int> q;
    		for (int i=head[S];~i;i=e[i].next)
    			if (dep[e[i].to]>Inf) q.push(e[i].to);
    		printf("%d ",q.size());
    		for (;q.size();q.pop()) printf("%d ",q.front());
    		printf("
    ");
    		
    		for (int i=head[T];~i;i=e[i].next)
    			if (dep[e[i].to]<Inf) q.push(e[i].to-m-1);
    		printf("%d ",q.size());
    		for (;q.size();q.pop()) printf("%d ",q.front());
    	}
    }dinic;
    
    int main()
    {
    	S=N*LG*2-1; T=N*LG*2-2;
    	memset(head,-1,sizeof(head));
    	scanf("%d%d",&n,&m);
    	for (int i=1,x,y;i<n;i++)
    	{
    		scanf("%d%d",&x,&y);
    		to[x].push_back(mp(y,i));
    		to[y].push_back(mp(x,i));
    	}
    	dfs(1,0);
    	for (int i=1,x,y;i<=m;i++)
    	{
    		add(S,i,1); add(i,S,0);
    		scanf("%d%d",&x,&y);
    		int p=lca(x,y);
    		addedge(i,x,p); addedge(i,y,p);
    	}
    	dinic.work();
    	printf("%d
    ",maxf);
    	dinic.print();
    	return 0;
    }
    
  • 相关阅读:
    mysql在第一次查询的时候很慢,第二次查询就比较快的原因?
    mysql的递归(使用函数)
    什么样的男人才是女人眼中最帅的男人
    面试题总结
    java的重载总结
    arduino读取GPIO数据
    electron+react项目改为typescript
    百度AI训练营笔记
    python读取文件出现ufeff问题
    大端小端
  • 原文地址:https://www.cnblogs.com/stoorz/p/13886215.html
Copyright © 2020-2023  润新知