CH6401 Genesis

description

God hands N (N≤10 ^ 6) kinds of the world elements, each element may be one element restriction Further, the i-th element of the kind of world world elements can be referred to as a limitation A [i]. Now, God put them in part to launch a new space to build the world. For peace and tranquility of the world, God wants all elements are put in the world has at least one element of the world have not been put in a limit it. God wants to know, in this context, he can put up to how many elements of the world?

Input Format

The first line is an integer N, the number of elements in the world.
The second line has N integers A1, A2, ..., AN. Ai represents the number of i-th element of the world can limit the elements of the world.

Output Format

An integer representing the number of elements in most of the world can deliver.

Sample input

6
2 3 1 3 6 5

Sample Output

3

Data range and Conventions

  • For 30% of the data:0 \ leq a, b \ leq 10000
  • For 60% of the data:0 \ leq a, b \ leq 2 ^ {31}
  • To 100% of the data:0 \ leq a, b \ leq 2 ^ {63}

Sample interpretation

For 30% of the data, N≤10.
To 100% of the data, N≤10 ^ 6,1≤Ai≤N.

source

Shijiazhuang [II] Nescafé 8 Cup race simulation NOIP

answer

Ball ring boss tree. Classic approach: twice DP, disconnect, once mandatory links (and assignment achieved by appropriate conditions).

For this problem, you can pre-DP values ​​son subtree, and then special judge for son no longer be classified when the DP, the assignment can be invoked.

Time complexity \ (O (n) \)

#include<bits/stdc++.h>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
    for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;

co int N=1e6+1,INF=0x3f3f3f3f;
int n,fa[N],t,k,f[N][2],s[N][2],ans;
int Head[N],Edge[N*2],Next[N*2],tot;

int get(int x) {return fa[x]==x?x:fa[x]=get(fa[x]);}
il void add(int x,int y){
    Edge[++tot]=y,Next[tot]=Head[x],Head[x]=tot;
}
void dfs(int x){
    int num=INF;
    f[x][0]=0;
    for(int i=Head[x];i;i=Next[i]){
        if(Edge[i]!=k) dfs(Edge[i]);
        f[x][0]+=max(f[Edge[i]][0],f[Edge[i]][1]);
        num=min(num,max(f[Edge[i]][0],f[Edge[i]][1])-f[Edge[i]][0]);
    }
    f[x][1]=f[x][0]+1-num;
}
int main(){
    read(n);
    for(int i=1;i<=n;++i) fa[i]=i;
    for(int i=1;i<=n;++i){
        int x=read<int>();
        int p=get(x),q=get(i);
        if(p==q) s[++t][0]=x,s[t][1]=i;
        else add(x,i),fa[q]=p;
    }
    for(int i=1;i<=t;++i){
        k=0;
        dfs(s[i][0]);
        k=s[i][0];
        dfs(s[i][1]);
        int now=max(f[s[i][1]][0],f[s[i][1]][1]);
        f[s[i][0]][1]=f[s[i][0]][0]+1;
        dfs(s[i][1]);
        ans+=max(now,f[s[i][1]][0]);
    }
    printf("%d\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/autoint/p/10936515.html