codeforces 387D George and Interesting Graph(二分图最大匹配)

本文介绍了一个关于 InterestingGraph 的问题解决方法,通过构建特殊的二分图并利用最大匹配算法来确定最少的操作次数,使得 InterestingGraph 在移除特定中心节点及连接后成为每个节点度数均为1的图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:
。。。
思路:
interesting graph去掉中心点和与其相连的边之后,成为一个所有点出度和入度都是1的图。
那么,可以用二分图的左半表示出度,右半表示入度。
则这个二分图的最大匹配是max_match。
与中心点相连的边有cnti条,other = m - cnti
则在我们构造的二分图中
需要删去的边:other - max_match
需要添加的边: n - max_match

int n, m, cx[Maxn+5], cy[Maxn+5], vis[Maxn+5], g[Maxn+5][Maxn+5], other;

int dfs(int x, int u) {
    rep(i, 1, n) if (i != x && !vis[i] && g[u][i]) {
        vis[i] = 1;
        if (cy[i] == -1 || dfs(x, cy[i])) {
            cx[u] = i; cy[i] = u; return 1;
        }
    }
    return 0;
}

void pre(int ce) { // 贪心优化
    rep(i, 1, n) if (i != ce && cx[i] == -1)
        rep(j, 1, n) if (j != ce && cy[j] == -1 && g[i][j]) {
            cx[i] = j; cy[j] = i; break;
        }
}

int go (int ce) {
    int cnt = 0, now = 0;
    rep(i, 1, n) if (ce != i) {
        if (!g[i][ce]) ++ cnt; else ++ now;
        if (!g[ce][i]) ++ cnt; else ++ now;
    }
    other = m - now; if (!g[ce][ce]) ++ cnt; else -- other;
    memset(cx, -1, sizeof(cx));
    memset(cy, -1, sizeof(cy));

    pre(ce);

    rep(i, 1, n) if (i != ce && cx[i] == -1) {
        memset(vis, 0, sizeof(vis));
        dfs(ce, i);
    }

    int max_match = 0;
    rep(i, 1, n) if (cx[i] != -1) ++ max_match;
    return cnt + other - max_match + n - 1 - max_match;
}

int solve() {
    int ans = inf;
    rep(i, 1, n) {
        ans = min (ans, go(i));
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
#endif
    cin >> n >> m;
    rep(i, 1, m) {
        int x, y; cin >> x >> y;
        g[x][y] = 1;
    }
    cout << solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值