Topology Ordering:
Two queues, one put does not need to restart in the degree of 0, a put need to restart the degree of 0 .... Starting from a queue that does not need to be restarted, each popup number will be updated to the next level, encountering a read as 0 to join the corresponding queue, when the queue is empty, record the number of restarts +1, swap queue. Up to two queues are empty
Smart Software InstallerTime
limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 32
Problem DescriptionThe Software Installation is becoming + more complex. An automatic tool was often useful to manage this process. An IT company was developing a System Management utility to install a set of software packages automatically with the Depen Dencies. They found that reboot was often required to take effect after installing some software. A software package cannot is installed until all software packages it depends on is installed and take effect.
In the beginning, they implemented a simple installation algorithm, but the system would reboot many times during the Inst Allation process. This is a great impact on the user experience. After some study, they think the this process can is further optimized by means of installing as much packages as Possibl e before each reboot.
Now, could. Implement this algorithm for them to minimize the number of restart during the entire INS Tallation process?
Inputthe first line is a integer n (1 <= n <=), which is the number of the test cases. The second is blank. The input of both test cases is separated by a blank line.
Each test case contains m (1 <= n <=) continuous lines and all line is no longer than. Each line starts with a package name and a comma (:). If an asterisk (*) exists between the package name and the comma, the reboot operation are required for the. The remaining line was the other packaged names it depends on, separated by whitespace. Empty means that there are no dependency for this software. For example, "A:B" means package B was required to being installed before package a. Package names consist of letters, digits and underscores, excluding other special symbols.
Assume all packages this need to being installed and all referenced packages would be listed in a individual line to define T He reboot property. It should is noted that cyclic dependencies is not allowed in this problem.
Outputfor Each test case, you should output a line starting with "Case #:" (# is the No. Of the test case, starting from 1) and containing the reboot count for this test case. (Refer to the sample format)
Sample Input
2glibc:gcc*: glibcuefi*:gcc*:raid_util*: uefigpu_driver*: UEFIOPENCL_SDK:GPU_DRIVERGCC
Sample Output
SOURCE2014 Shanghai National Invitational Competition--re-title (thanks to Shanghai University for its topics)
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < string> #include <sstream> #include <map> #include <queue>using namespace std;const int maxn=2000; map<string,int> msi;int nm;bool restart[maxn];int degree[maxn];int Hash (string name) {int ret=msi[name]; if (ret==0) {msi[name]=nm++; Ret=nm-1; } return ret;} struct edge{int to,next;} Edge[maxn*maxn];int adj[maxn],size;void init () {memset (adj,-1,sizeof (ADJ)); size=0;} void Add_edge (int u,int v) {edge[size].to=v; edge[size].next=adj[u]; adj[u]=size++;} int Tuopu () {queue<int> q[2]; int a=0,b=1; int n=nm-1; for (int i=1;i<=n;i++) {if (degree[i]==0) {if (restart[i]==true) Q[b].push (i); else Q[a].push (i); }} int time=0; while (!q[a].empty () | |! Q[b].empty ()) {while (!q[a].empty ()) {int U=q[a].front (); Q[a].pop (); For (int i=adj[u];~i;i=edge[i].next) {int v=edge[i].to; degree[v]--; if (degree[v]==0) {if (restart[v]==true) Q[b].push (v); else Q[a].push (v); }}} if (Q[b].empty () ==true) continue; Time++;swap (A, b); } return time; int main () {int t_t,cas=1; scanf ("%d", &t_t); GetChar (); GetChar (); while (t_t--) {init (); Msi.clear (); Nm=1; memset (restart,false,sizeof (restart)); memset (degree,0,sizeof (degree)); String Line,name; while (Getline (cin,line)) {if (line[0]==0) break; Istringstream sin (line); sin>>name; BOOL Flag=false; int sz=name.size (); if (name[sz-2]== ' * ') {flag=true; name[sz-2]=0; Name.resize (sz-2); } else { name[sz-1]=0; Name.resize (sz-1); } int To=hash (name); Restart[to]=flag; while (sin>>name) {int From=hash (name); Add_edge (from,to); degree[to]++; }} printf ("Case%d:%d\n", Cas++,tuopu ()); } return 0;}
Hdoj 5098 Smart software Installer topology sequencing