• 洛谷P4630 [APIO2018] Duathlon 铁人两项 【圆方树】


    题目链接

    洛谷P4630

    题解

    看了一下部分分,觉得树的部分很可做,就相当于求一个点对路径长之和的东西,考虑一下能不能转化到一般图来?
    一般图要转为树,就使用圆方树呗

    思考一下发现,两点之间经过的点双,点双内所有点一定都可以作为中介点
    那么我们将方点赋值为点双大小,为了去重,剩余点赋值(-1)
    答案就是任意两点间权值和之和

    我们只需枚举每个点被经过多少次,这就很容易计算了

    复杂度(O(n))

    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define LL long long int
    using namespace std;
    const int maxn = 400005,maxm =1000005,INF = 1000000000;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    int hh[maxn],Ne = 1,h[maxn],ne = 1;
    struct EDGE{int from,to,nxt;}e[maxm],ed[maxm];
    inline void build(int u,int v){
    	e[++Ne] = (EDGE){u,v,hh[u]}; hh[u] = Ne;
    	e[++Ne] = (EDGE){v,u,hh[v]}; hh[v] = Ne;
    }
    inline void add(int u,int v){
    	ed[++ne] = (EDGE){u,v,h[u]}; h[u] = ne;
    	ed[++ne] = (EDGE){v,u,h[v]}; h[v] = ne;
    }
    int n,m,val[maxn],dfn[maxn],low[maxn],inst[maxn],st[maxm],top,cnt,N;
    void combine(int rt){
    	val[++N] = 1;
    	add(N,rt);
    	while (st[top] != rt && low[st[top]] >= dfn[rt]){
    		inst[st[top]] = false;
    		add(N,st[top--]),val[N]++;
    	}
    }
    void dfs(int u,int s){
    	dfn[u] = low[u] = ++cnt; st[++top] = u; inst[u] = true;
    	for (int k = hh[u],to; k; k = e[k].nxt){
    		if (k == s) continue;
    		if (!dfn[to = e[k].to]){
    			dfs(to,k ^ 1);
    			if (low[to] > dfn[u]){
    				top--; val[++N] = 2;
    				add(u,N); add(N,to);
    			}
    			else if (low[to] == dfn[u]) combine(u);
    			low[u] = min(low[u],low[to]);
    		}
    		else if (inst[to]) low[u] = min(low[u],dfn[to]);
    	}
    }
    int fa[maxn];
    LL siz[maxn],ans,sum;
    void DFS(int u){
    	siz[u] = u <= n ? 1 : 0;
    	Redge(u) if ((to = ed[k].to) != fa[u]){
    		fa[to] = u,DFS(to),siz[u] += siz[to];
    	}
    	LL tot = u <= n ? sum - 1 : 0;
    	Redge(u) if ((to = ed[k].to) != fa[u]){
    		tot += siz[to] * (sum - siz[to]);
    	}
    	tot += (sum - siz[u]) * siz[u];
    	ans += tot * val[u];
    }
    int main(){
    	N = n = read(); m = read();
    	REP(i,m) build(read(),read());
    	REP(i,n) val[i] = -1;
    	REP(i,n) if (!dfn[i]){
    		sum = cnt; top = 0; dfs(i,-1);
    		sum = cnt - sum; DFS(i);
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    
    

    总结

    1、圆方树的写法
    2、点双的写法
    3、一般图考虑树的做法的时候,可以考虑建圆方树

  • 相关阅读:
    IL指令详细
    读懂IL代码就这么简单(三)完结篇
    读懂IL代码就这么简单(二)
    读懂IL代码就这么简单(一)
    在 .NET Core Logging中使用 Trace和TraceSource
    使用JWT创建安全的ASP.NET Core Web API
    闭环思维
    一行代码,百万人民币打水漂
    网络接口库函数mpr.dll动态库
    使用MSF利用永恒之蓝漏洞远程控制主机——直接使用MSF即可RCE,我++,在docker里没有完成,GG!
  • 原文地址:https://www.cnblogs.com/Mychael/p/9190204.html
Copyright © 2020-2023  润新知