Description
Applepi has a book, Genesis, in which a story is recorded ...
In God's hand there are n kinds of things called "the World Element", now he wants to put a part of them into a new space to build the world. Every world element can limit another world element, so God wants to have at least one world element that has not been put in place to limit it so that God can maintain control of the world.
Because of the famous question about whether God can make a two-back proposition about a big stone that he cannot lift, we know that God is not omnipotent, and that he is not omnipotent, and that he has something to ask of you--God wants to know how many world elements he could run, but he would only O (2^n) Level of the algorithm. Although God has an infinite number of hours, he is also a short temper. You need to help God solve this problem.
Input
The first line is an integer n, which represents the number of world elements.
The second line has N integers A1, A2, ..., an. Ai represents the number of world elements that the first world element can limit.
Output
An integer that represents the maximum number of world elements that can be served.
1. For a leaf node, delete it and parent node and answer +1 is optimal (parent node pairing with other points will not be better)
2. For a ring with a point (not connected to other points), the contribution to the answer is a>>1
In the topology sort process, press 1. Processing, after sorting the rest of the ring press 2. Processing
#include <cstdio>Const intn=1000010, r=9000000;BOOLEd[n];intN,fa[n],inch[n],q[n],ql=0, qr=0, ans=0;Charbuf[r],*ptr=buf-1; inlineint_int () {intx=0, c=*++ptr; while(c< -) c=*++ptr; while(c> -) x=x*Ten+c- -, c=*++ptr; returnx;}intMain () {fread (buf,1, R-4, stdin); N=_int (); for(intI=1; i<=n;i++) + +inch[fa[i]=_int ()]; for(intI=1; i<=n;i++)if(!inch[i]) q[qr++]=i; while(ql!=qr) { intw=q[ql++]; if(Ed[w])Continue; intf=Fa[w]; if(!--inch[F]) q[qr++]=F; ED[W]=1; if(!Ed[f]) {Ed[f]=1; ++ans; F=Fa[f]; if(!--inch[F]) q[qr++]=F; } } for(intI=1; i<=n;i++)if(!Ed[i]) { intC=1, w=Fa[i]; ED[W]=1; while(w!=i) { ++C; ED[W]=1; W=Fa[w]; } ans+=c>>1; } printf ("%d", ans); return 0;}
bzoj3037: Genesis