基环树入门

需要强调点是基环树可能是个森林。

1.对一个n点n边的图找出任意两点不想连的最大全值,相当于最大权独立集:

dps找到环的位置,然后求得环中某个位置点头u和尾v,分别按树跑一次dp,max(dp[u][0],dp[v][0])就是最大值

因为v与u必定不要一个,求不要其中一个的最大值就是结果

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
const int N = 1e6+10;   //基环树中点的个数
const int E = 1e6+10;   //基环树中无向边的个数
struct Edge{int next,to;}e[E*2];
int head[N], cnt;
bool vis[N];
int val[N], ringPt1, ringPt2, not_pass; //ringPt1,ringPt2 -> not_pass边的端点
long long dp[2][N];
void add(int u,int v){
    e[++cnt].next = head[u];
    e[cnt].to = v;
    head[u] = cnt;
}
void getDP(int rt, int fa) {
    dp[0][rt] = 0,  dp[1][rt] = val[rt];
    for(int i=head[rt];i!=-1;i=e[i].next) {
        if(e[i].to == fa)   continue;
        if(i == not_pass || i == (not_pass^1))  continue;
        getDP(e[i].to, rt);
        //
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值