• BZOJ3037 创世纪(基环树DP)


    基环树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("
    ----------
    ")
    #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, "
    time: %.3fms
    ", 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;
    }
    

  • 相关阅读:
    多线程中变量的内存分配
    VS2019解决Cannot resolve symbol ' '问题
    排序算法——选择排序
    观察者模式
    c# 之linq——小白入门级
    c# 迭代器
    MySql学习
    rabbitmq安装及简单demo练习
    VMware Workstation 12 PRo密钥
    远程过程调用——RPC
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11559458.html
Copyright © 2020-2023  润新知