"BZOJ2815" [ZJOI2012] disaster extension sort +lca

Source: Internet
Author: User

"BZOJ2815" [ZJOI2012] Disaster title description

Amoeba is a good friend of Xiao Qiang.

The amoeba and the cockroach catch the Grasshopper on the Prairie. Xiao Qiang suddenly thought, the fruit grasshopper by their catch extinction, then eat grasshopper bird will starve to death, and birds of prey will also follow extinction, resulting in a series of ecological disaster.

Learning the amoeba of a creature tells the little strong that the grassland is an extremely stable ecological system. If the grasshopper is extinct, the bird can eat other insects, so the extinction of a species does not necessarily cause a major catastrophe.

We now look at this problem from a professional point of view. We use a graph called the food web to describe the relationship between organisms:

A food net has n points, which represent n species, and if biological x can eat biological y, then a forward edge from Y to X is attached.

This figure has no ring.

There are some points in the graph that are not connected to the edges, which represent the creatures that are producers that can survive through photosynthesis, while the dots that connect the edges represent the consumers who must survive by eating other organisms.

If all the food of a consumer is extinct, it will go extinct.

We define the "disaster value" of a creature in the food web as if it were suddenly extinct, then it would follow the species of extinct organisms.

For example: On a pasture, the relationship between organisms is:

Such as

If Xiao Qiang and amoeba frighten all the sheep on the Prairie, wolves will be extinct because there is no food, and cockroaches and amoeba can survive by eating cows and cows. So, the goat's disaster value is 1. However, if the grass suddenly extinction, then the entire grassland of the 5 species are not spared, so the disaster value of the grass is 4.

Given a food net, you ask for the disaster value of each creature.

Input/output format

Input format:

The first line of the input file catas.in is a positive integer N, which represents the number of mobs. Creature from 1 mark

Number to N.

Next N rows, each line describes a list of other organisms that a creature can eat, in the form of an empty

A number representing a creature, and the last number is 0 for the column

The end of the table.

Output format:

The output file Catas.out contains n rows, one integer per line, representing the disaster value for each mob.

Input and Output Sample input example # #:
501 01 02 3 02 0
Sample # # of output:
41000
Description

"Sample description"

The sample input describes an example of a problem description cited.

"Data Size"

For 50% of the data, n≤10000.

For 100% of the data, 1≤n≤65534.

The input file size does not exceed 1M. Ensure that the input food net is not ring.

problem: Bzoj No test instructions, I got the original question from Luo gu

Violence? Run n Edge, Time complexity O (n*n)

So how can you get everything done in one optimization?

Initial idea: To make this to a non-circular map into a tree, so that the tree in order to find the size of the tree, but some point may be many points of the son, then we actually let its father who?

Answer: All the LCA pointing to its point

We might as well make this tree a "extinction tree", meaning that all the points in X's subtree will be extinct after X is extinct. Then we sort by the order of the producers to the consumers. So find out all of X's food in the extinction tree LCA, let X in the extinction tree to become the son of LCA, which ensures that all the food that can make x extinct is the ancestor of X, conversely, all ancestors not X's food will not cause x extinction. Then we dynamically maintain the multiplication of the LCA, and finally ask for the subtree size.

Note the array of edges is going to be bigger.

#include <cstdio> #include <cstring> #include <iostream> #include <queue>using namespace std; const int Maxn=70000;int TO[10000000],NEXT[10000000],HEAD[MAXN],FA[MAXN][20],DEP[MAXN],D[MAXN],Q[MAXN],SIZ[MAXN]; int n,m,cnt;queue<int> q;void Add (int a,int b) {to[cnt]=b;next[cnt]=head[a];head[a]=cnt++;} int main () {memset (head,-1,sizeof (head)), int i,j,a,b,u;scanf ("%d", &n), for (i=1;i<=n;i++) {while (1) {scanf ("%d ", &a); if (!a) Break;add (a,i);d [i]++;}} for (i=1;i<=n;i++) fa[i][0]=-1;for (i=1;i<=n;i++) if (!d[i]) Q.push (i), Fa[i][0]=0;while (!q.empty ()) {U=q.front ( ), Q.pop (); Q[++q[0]]=u;dep[u]=dep[fa[u][0]]+1;for (j=1;j<=18;j++) fa[u][j]=fa[fa[u][j-1]][j-1];for (i=head[u];i!=-1;i= Next[i]) {d[to[i]]--;if (!d[to[i]) Q.push (To[i]), if (fa[to[i]][0]==-1) fa[to[i]][0]=u;else{a=fa[to[i]][0],b=u;if ( DEP[A]&GT;DEP[B]) swap (A, b), for (j=18;j>=0;j--) if (Dep[fa[b][j]]>=dep[a]) b=fa[b][j];if (b==a) {Fa[to[i]][0]=a ; continue;} for (j=18;j>=0;j--) {if (Fa[a][j]!=fa[b][j]) a=fa[a][j],b=fa[b][j];} FA[to[i]][0]=fa[a][0];}}} for (i=q[0];i>=1;i--) siz[q[i]]++,siz[fa[q[i]][0]]+=siz[q[i]];for (i=1;i<=n;i++) printf ("%d\n", siz[i]-1); return 0;}

"BZOJ2815" [ZJOI2012] disaster extension sort +lca

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.