BZOJ3037 创世纪(基环树DP)

拜拜、爱过 提交于 2019-11-30 04:22:28

基环树DP,攻的当受的儿子,f表选,g表不选。并查集维护攻受关系。若有环则记录,DP受的后把它当祖宗,再DP攻的。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); (a) <= (c); ++(a))
#define nR(a,b,c) for(register int a = (b); (a) >= (c); --(a))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))
#define ll long long
#define u32 unsigned int
#define u64 unsigned long long
 
#define ON_DEBUGG
 
#ifdef ON_DEBUGG
 
#define D_e_Line printf("\n----------\n")
#define D_e(x) cout << (#x) << " : " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#include <ctime>
#define TIME() fprintf(stderr, "\ntime: %.3fms\n", clock() * 1000.0 / CLOCKS_PER_SEC);
  
#else
 
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#define FileSave() ;
#define TIME() ;
//char buf[1 << 21], *p1 = buf, *p2 = buf;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
 
#endif
 
using namespace std;
struct ios{
    template<typename ATP>inline ios& operator >> (ATP &x){
        x = 0; int f = 1; char ch;
        for(ch = getchar(); ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
        while(ch >= '0' && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar();
        x *= f;
        return *this;
    }
}io;
 
template<typename ATP>inline ATP Max(ATP a, ATP b){
    return a > b ? a : b;
}
template<typename ATP>inline ATP Min(ATP a, ATP b){
    return a < b ? a : b;
}
template<typename ATP>inline ATP Abs(ATP a){
    return a < 0 ? -a : a;
}

const int N = 1000007;

int n, totCircle;
int ans, root;
int f[N], g[N], A[N], B[N];
struct Edge{
    int nxt, pre;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v){
    e[++cntEdge] = (Edge){ head[u], v}, head[u] = cntEdge;
}
int fa[N];
inline int Find(int x){
    return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
inline void DFS(int u){
    int t = 0x7fffffff;
    g[u] = 0;
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(v != root) DFS(v);
        g[u] += max(f[v], g[v]);
        t = min(t, max(f[v], g[v]) - g[v]);
    }
    f[u] = g[u] + 1 - t;
}
int main(){
//FileOpen();

    io >> n;
    R(i,1,n) fa[i] = i;
    R(i,1,n){
        int j;
        io >> j;
        int p = Find(i), q = Find(j);
        if(p != q){
            add(j, i);
            fa[q] = p;
        }
        else{
            A[++totCircle] = j, B[totCircle] = i;
        }
    }
    R(i,1,totCircle){
        DFS(A[i]), root = A[i];
        DFS(B[i]);
        int tmp = f[B[i]];
        f[A[i]] = g[A[i]] + 1;
        DFS(B[i]);
        ans += Max(tmp, g[B[i]]);
    }
    
    printf("%d",ans);
    
    return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!