bzoj4316: 小C的独立集

bzoj4316: 小C的独立集

链接

bzoj

思路

不是环的边==没有上司的舞会。
其他的,把环拿出来,考虑与深度最小的点u的交界处的点选不选,进行两次dp更新f[u]

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int read() {
    int x = 0, f = 1; char s = getchar();
    for (; s > '9' || s < '0'; s = getchar()) if (s == '-') f = -1;
    for (; s >= '0' && s <= '9'; s = getchar()) x = x * 10 + s - '0';
    return x * f;
}
int n, m;
struct node {
    int v, nxt;
}e[N << 1];
int head[N << 1], tot;
void add(int u, int v) {
    e[++tot].v = v ;
    e[tot].nxt = head[u];
    head[u] = tot;
}
int low[N], dfn[N], cnt, fa[N], f[N][2] ;
void work(int u, int y) {
    int t0, t1, f0=0, f1=0;
    for(int i = y;i != u;i = fa[i]){
        t0 = f0 + f[i][0], t1 = f1 + f[i][1];
        f0 = max(t0, t1), f1 = t0;
    }
    f[u][0] += f0;
    f0 = 0, f1 = -1e9;
    for(int i = y;i != u;i = fa[i]){
        t0 = f0 + f[i][0], t1 = f1 + f[i][1];
        f0 = max(t0, t1), f1 = t0;
    }
    f[u][1] += f1;
}
void tarjan(int u, int father) {
    fa[u] = father;
    low[u] = dfn[u] = ++cnt;
    f[u][1] = 1, f[u][0] = 0;
    for (int i = head[u]; i; i = e[i].nxt) {
        int v = e[i].v;
        if (!dfn[v]) {
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
        } else if (v != father) low[u] = min(low[u], dfn[v]);
        if (dfn[u] < low[v]) {
            f[u][0] += max(f[v][1], f[v][0]);
            f[u][1] += f[v][0];
        }
    }
    for (int i = head[u]; i; i = e[i].nxt) {
        int v = e[i].v;
        if(u != fa[v] && dfn[v] > dfn[u])
            work(u, v);
    }
}
int main() {
    n = read(), m = read();
    for (int i = 0; i < m; ++i) {
        int u = read(), v = read();
        add(u, v), add(v, u);
    }
    tarjan(1, 0);
    printf("%d\n", max(f[1][0], f[1][1]));
    return 0;
}
全部评论

相关推荐

10-06 12:46
门头沟学院 Java
跨考小白:定时任务启动
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务