基环树上dp

//https://vjudge.csgrandeur.cn/contest/532011#problem/F
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define all(a) a.begin(), a.end()
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
using namespace std;

const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-4;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;

/*
基环树上dp方法:
方法1.先考虑树上dp, 再考虑环上dp(将环边链)
方法2.断开环上的一条边(枚举这条边两个端点的情况),再做树形dp
*/

int n, m, xx, yy, dp[N][2];
vector<int> g[N];

void dfs(int x, int fa)
{
    //cout << "???? " << x << '\n';
    for (int &to : g[x])
    {
        if (to == fa || x == xx && to == yy || x == yy && to == xx)
            continue;
        dfs(to, x);
        dp[x][0] += dp[to][1];
        dp[x][1] += min(dp[to][0], dp[to][1]);
    }
    dp[x][1]++;
}

void solve()
{
    cin >> n >> m;
    xx = 0, yy = 1;
    for (int i = 1, u, v; i <= n + m; i++)
    {
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int ans = inf;
    memset(dp, 0, sizeof dp);
    dp[yy][1] = inf;
    dfs(xx, -1);
    ans = min(ans, dp[xx][1]);
    memset(dp, 0, sizeof dp);
    dp[yy][0] = inf;
    dfs(xx, -1);
    ans = min(ans, min(dp[xx][0], dp[xx][1]));
    cout << ans << '\n';
}

signed main()
{
    IOS;
    int t = 1;
    // cin >> t;
    while (t--)
        solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值