"ZJOI2012" Disaster-lca+ topology sequencing

Source: Internet
Author: User

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 that if the grasshopper was killed by them, then the birds eat grasshopper will starve to death, and the birds of prey will also be extinct, causing a series of ecological disasters.

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.

Ideas

A biological extinction only when all its food is extinct, and all the eating

Objects are extinct only when their LCA is extinct, so we put each creature

is connected to the LCA of all its food, that is, the parent section of each node

The point is the LCA of all its food. There is one condition for this.

It was the creature that had all its food added before it joined, so

To add a little bit of topological order, the last statistic is the tree prefix and.

#include <bits/stdc++.h>using namespacestd;Const intMAXN =100000+Ten;intn,deg[maxn],dep[maxn],father[maxn][ -],size[maxn];vector<int>Edge1[maxn],edge2[maxn],tree[maxn];queue<int>Q;inlineintLcaintAintb) {if(Dep[a] <dep[b]) swap (A, b);  for(inti = -; I >=0; i--)        if(Dep[father[a][i]] >= dep[b]) A =Father[a][i]; if(A = = b)returnA;  for(inti = -; I >=0; i--)        if(Father[a][i]! =Father[b][i]) {a=Father[a][i]; b=Father[b][i]; }    returnfather[a][0];} InlinevoidDfsintNowintFA) {Size[now]=1;  for(size_t i =0; i < tree[now].size (); i++)        if(Tree[now][i]! =FA)            {DFS (Tree[now][i],now); Size[now]+=Size[tree[now][i]]; }}intMain () {scanf ("%d",&N);  for(inti =1, X;i <= n;i++)         for(SCANF ("%d", &x); X;SCANF ("%d",&x)) {edge1[x].push_back (i);            Edge2[i].push_back (x); Deg[i]++; } dep[n+1] =1;  for(inti =1; I <= n;i++)        if(!Deg[i])            {Q.push (i); Edge2[i].push_back (n+1); }     while(Q.size ()) {intnow = Q.front (), LCA = edge2[now][0];        Q.pop ();  for(size_t i =1; i < edge2[now].size (); i++) LCA =LCA (Lca,edge2[now][i]); Dep[now]= dep[lca]+1; father[now][0] =LCA;        Tree[lca].push_back (now);  for(inti =1; I <= -; i++) Father[now][i] = father[father[now][i-1]][i-1];  for(size_t i =0; i < edge1[now].size (); i++) {Deg[edge1[now][i]]--; if(!Deg[edge1[now][i]])        Q.push (Edge1[now][i]); }} DFS (n+1,0);  for(inti =1; I <= n;i++) printf ("%d\n", size[i]-1); return 0;}

"ZJOI2012" Disaster-lca+ topology sequencing

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.