"Algorithmic Learning Notes" 46. Topological sort priority Queue SJTU OJ 3010 complicated Buttons

Source: Internet
Author: User

Description

Kane encountered n buttons on the ruins expedition, and at first all the buttons were open, and Kane's experience told him that closing all the buttons would have a "good thing" happen, but some buttons would push the other closed buttons out of the box, and, after Kane's study, each button would have a fixed bouncing set, When this button is pressed, all the buttons in the pop-up set will be turned on. Now little K would like to know if all the buttons can be turned to a closed state. If you can, print the minimum number of steps and the scheme, otherwise print "no solution".

Input Format

The first line is an integer n, which represents n buttons.

The next n lines represent the open set of buttons numbered 1 to N.

Format for mi B1 B2 B3 ... Bmi

A button that represents the number I is pressed, which makes the number B1 B2 B3 ... The BMI button bounces off (note: There is no repetition).

1 <= n <= 30000记 M = m1+m2+…+mn,0 <= M <= 1000000, 对于70%的数据n <= 300
Output Format

If there is no solution, output "no solution".

Otherwise, the first line outputs a minimum number of steps ans, the second line outputs the number of ans separated by a space, which means that the buttons that are numbered in the order of the numbers can be resolved.

If there are multiple scenarios, the output dictionary is the least-ordered scheme.

Sample Input
62 2 302 4 5000
Sample Output
61 2 3 4 5 6

By pointing out, know that this problem mainly involves two things. One is the topological sort, the one called the priority queue (seemingly and heap is the same thing?). )
About topology Ordering: http://www.cnblogs.com/newpanderking/archive/2012/10/18/2729552.html
http://blog.csdn.net/fisher_jiang/article/details/941234
About priority queues: http://www.cppblog.com/shyli/archive/2007/04/06/21366.html
(One question is: Does this question use a priority queue instead of a queue in order to produce the result of a dictionary order, or can it also play an optimization role?) (last time in the shortest path, it seems to remember that the Dijkstra algorithm can be optimized by Priority queue optimization/heap optimization.) Always don't understand how a principle. ))
In this question, the effect is: when you press a button, make sure that all the buttons that make the button pop are pressed.
Since there are n items in the topological sequence, the first line of the output must be n
Then follow the process of topological sequence can be, one thing to note is to always determine whether the point being processed is a deleted point, even if it may not be necessary to judge not to omit the 6th Test point has been re, only to realize that the midpoint of the queue may have been deleted, this loop should still be a lot of.

Also note the use of dynamic arrays, as well as the modularity of the code.
#include <iostream>#include<queue>#include<cstdlib>#include<cstdio>#include<cstring>using namespacestd;//Global VariablesConst intMAXN =30000+5;//int g[maxn][maxn]={0};int**G;//adjacency Table Storage diagram Note G[i] indicates the condition of the I button g[i][0] The number of edges that are stored g[i][1~g[i][0]]int inch[maxn]={0};//In[i] This point is stored in the degree ofBOOLdel[maxn]={0};//Del[i] Indicates whether it has been deleted//Priority queue: Specifies that the comparison method is a small number of precedence to achieve dictionary orderpriority_queue<int,vector<int>,greater<int> > Q;//If you do not specify Greater<int> as the comparison function, the system will automatically call < to compareintN//Number of buttonsintans[maxn]={0};//Record Storage Results//Initialize input graphvoidinit () {scanf ("%d",&N); G=New int*[n+5];  for(inti =1; I <= N; ++i)//The edge of each node    {            intm =0; scanf ("%d", &m);//The number of edges that record this pointG[i] =New int[m+Ten]; g[i][0] =m;  for(intj =1; J <= g[i][0]; ++j) {scanf ("%d",&G[i][j]); inch[g[i][j]]++;//the entry of the J-Point plus one        }    }} //returns whether the topology can be sortedBOOLTopologicalsort () {//first put in all points with a 0 entry level     for(inti =1; I <= N; ++i)if(inch[i]==0) Q.push (i); if(q.size () = =0)//There is no point in the degree of 0 ...        return false; //The result of the topology sort must be n-bit, so specify the number of times with for     for(inti =1; I <= N; ++i) {intcur = q.top ();//the image of the heap came out.Q.pop (); if(Del[cur])return false; Del[cur]=true;//Delete this pointAns[i] =cur; //Delete all sides of this point         for(intj =1; J <= g[cur][0]; ++j) {intNXT =G[cur][j]; if(DEL[NXT])return false;//if it's connected to a point that has been removed, it indicates there is a ring .            inch[nxt]--;//Let it connect to the point of the degree minus one            if(inch[nxt]==0) Q.push (NXT); }    }     return true;}voiddestory () { for(inti =0; I < n+5; ++i) {Delete[] g[i]; }}intMainintargcChar Const*argv[])     {init (); if(Topologicalsort ()) {printf ("%d\n", N);  for(inti =1; I <= N; ++i) {printf ("%d", Ans[i]); } printf ("\ n"); }Elseprintf ("No solution\n");    Destory (); return 0;}/*AOV Network: Vertex active Network The topological ordering of a directed acyclic graph (DAG) shows that its precursor (necessary) activity has been completed in the event of an activity.    Topological ordering: The topology of the DAG is sorted. A linear sequence is obtained so that if u->v exists in the DAG, it is in front of V in U. In this question, aov means that when a button is pressed, all the buttons that can make the button pop are pressed. The steps to sort the topology are simple: 1. Loop to find a point with an entry level of 0, and remove it and its out edges from the diagram. 2. If there is a last point in the figure, the circuit is present. PS: Heap and priority queue. It seems to be a thing/. Intuitively, you can think of the queue as the horizontal axis of priority. Form a pile of sand and take things from top to bottom every time.*/




"Algorithmic Learning Notes" 46. Topological sort priority Queue SJTU OJ 3010 complicated Buttons

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.