zoukankan      html  css  js  c++  java
  • 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;
    }
    

  • 相关阅读:
    [CareerCup] 13.7 Node Pointer 节点指针
    [LeetCode] Bulls and Cows 公母牛游戏
    [CareerCup] 13.6 Virtual Destructor 虚析构函数
    [CareerCup] 13.5 Volatile Keyword 关键字volatile
    [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝
    [CareerCup] 13.3 Virtual Functions 虚函数
    [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map
    [CareerCup] 13.1 Print Last K Lines 打印最后K行
    [CareerCup] 12.6 Test an ATM 测试一个自动取款机
    [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11559458.html
Copyright © 2011-2022 走看看