zoukankan      html  css  js  c++  java
  • 牛客 4C Alliances (dfs序)

    大意: 给定树, 有$k$个帮派, 第$i$个帮派所占据点为$c_i$, 以及$c_i$两两相连路径上的所有点. 一个点可能被多个帮派占领. $q$个询问, 第$i$个询问给定$t_i$个帮派, 给定点$u$, 求将$t_i$个帮派合并后, 点$u$到帮派的最近距离.

    先求出帮派合并后的$lca$, 若$u$不在$lca$所在子树内, 那么最短距离就是$dis(u,lca)$, 否则在帮派占据的点中, 找到$dfs$序与$u$最接近的点$x$, 显然$lca(u,x)$属于帮派, 所以最短距离就为$dis(u,lca(u,x))$.

    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <string.h>
    #include <bitset>
    #define REP(i,a,n) for(int i=a;i<=n;++i)
    #define PER(i,a,n) for(int i=n;i>=a;--i)
    #define hr putchar(10)
    #define pb push_back
    #define lc (o<<1)
    #define rc (lc|1)
    #define mid ((l+r)>>1)
    #define ls lc,l,mid
    #define rs rc,mid+1,r
    #define x first
    #define y second
    #define io std::ios::sync_with_stdio(false)
    #define endl '
    '
    #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
    ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
    ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
    ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
    inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
    //head
    
    
    
    const int N = 1e6+10;
    int n, k, q, qry[N], Top[N];
    vector<int> g[N], f[N];
    int no[N], son[N], L[N], R[N];
    int sz[N], top[N], dep[N], fa[N];
    
    void dfs(int x, int f, int d) {
    	no[L[x]=++*L]=x,fa[x]=f,dep[x]=d,sz[x]=1;
    	for (int y:g[x]) if (y!=f) {
    		dfs(y,x,d+1),sz[x]+=sz[y];
    		if (sz[y]>sz[son[x]]) son[x]=y;
    	}
    	R[x]=*L;
    }
    void dfs(int x, int tf) {
    	top[x]=tf;
    	if (son[x]) dfs(son[x],tf);
    	for (int y:g[x]) if (!top[y]) dfs(y,y);
    }
    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 dis(int x, int y) {
    	return dep[x]+dep[y]-2*dep[lca(x,y)];
    }
    int main() {
    	scanf("%d", &n);
    	REP(i,2,n) {
    		int u=rd(),v=rd();
    		g[u].pb(v),g[v].pb(u);
    	}
    	dfs(1,0,0),dfs(1,1);
    	scanf("%d", &k);
    	REP(i,1,k) {
    		int c=rd();
    		REP(j,1,c) {
    			int t=rd();
    			f[i].pb(L[t]);
    			if (j==1) Top[i]=t;
    			else Top[i]=lca(Top[i],t);
    		}
    		sort(f[i].begin(),f[i].end());
    	}
    	scanf("%d", &q);
    	while (q--) {
    		int u=rd(),sz=rd(),Lca=0,ans=1e9;
    		REP(i,1,sz) qry[i]=rd(),Lca=i==1?Top[qry[i]]:lca(Lca,Top[qry[i]]);
    		if (L[Lca]<=L[u]&&L[u]<=R[Lca]) {
    			REP(i,1,sz) {
    				auto t = lower_bound(f[qry[i]].begin(),f[qry[i]].end(),L[u]);
    				if (t!=f[qry[i]].end()) ans = min(ans, dis(u,lca(u,no[*t])));
    				if (t!=f[qry[i]].begin()) ans = min(ans, dis(u,lca(u,no[*--t])));
    			}
    		}
    		ans = min(ans,dis(u,Lca));
    		printf("%d
    ",ans);
    	}
    }
    
  • 相关阅读:
    SAP MM VL32N和MIGO对内向交货单做收货,都会更新其'总体货物移动状态'
    Several mentality of SAP project customers in private enterprises
    GIT·AccessToken的基础使用
    MSSQL·按照某个字段重复删除旧的一条数据
    MSSQL·WHERE INT列=''时的结果集
    技能Get·手动更新HP笔记本BIOS过程记录
    GIT·.NetFramework MVC项目默认的.gitignore文件备份
    .Net·发布过程中报:在应用程序之外使用注册为allowDefinition='MachineTOApplication'的节是错误...
    知识总结·多系统数据同步API调用设计原则
    MSSQL·查看表锁进程及杀死进程的脚本
  • 原文地址:https://www.cnblogs.com/uid001/p/10874987.html
Copyright © 2011-2022 走看看