int main() { // freopen("data.txt","r",stdin); int T, M; scanf("%d",&T); while(T--) { memset(g,0,sizeof(g)); scanf("%d%d",&n,&M); while(M--) { int a, b; scanf("%d%d",&a,&b); g[a][b]=1; } if(!toposort()) printf("-1"); else { for(int i = 1;i < n;i++) printf("%d ",pack[i]); printf("%d",pack[n]); } putchar('\n'); } return 0; }
int main() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); while (scanf("%d%d", &n, &m) == 2) { memset(vis, 0, sizeof(vis)); memset(result, 0, sizeof(result)); memset(G, 0, sizeof(G)); int i; t = n; for (i = 0; i < m; i++) { scanf("%s", s); G[s[0] - 'a'][s[2] - 'a'] = 1; } if(!toposort()) return 0; for (i = 0; i < n; i++) { printf("%c", 'a'+result[i]); if (i != n - 1) printf(" < "); } } return 0; }
void dfs(int G[6][6], char E[6][6]){ int visited[6]={0,0,0,0,0,0},i,j,time=0; int start[6] = {0,0,0,0,0,0}; int finish[6] = {0,0,0,0,0,0}; for(i=0;i<6;i++){ if(!visited[i]) dfs_visit(i,G,E,visited,start,finish); } printf("start :"); for(i=0;i<6;i++) printf("%d ",start[i]); printf("\nfinish:"); for(i=0;i<6;i++) printf("%d ",finish[i]); printf("\n"); for(i=0;i<6;i++) { for(j=0;j<6;j++){ if(G[i][j]){ if(start[j]<start[i] && start[i]<finish[i] && finish[i]<finish[j]) E[i][j] = 'B'; if(start[i]<start[j] && start[j]<finish[j] && finish[j]<finish[i] && E[i][j]!='T') E[i][j] = 'F'; if(start[j]<finish[j] && finish[j]<start[i] && start[i]<finish[i]) E[i][j] = 'C'; printf("%c ",E[i][j]); } else printf("* "); } printf("\n"); } toposort(finish); }
int main(){ while(true){ scanf("%d%d", &N, &E); if(N == 0 && E == 0) break; char c = getchar(); while(c != '\n') c = getchar(); for(int i = 0; i < N; i++){ first_e[i] = -1; show[i] = 0; } int num_v = 0; bool is_stop = false; for(int i = 0; i < E; i++){ char str[5]; gets(str); if(is_stop) continue; e[i].b = str[0] - 'A'; e[i].e = str[2] - 'A'; next_e[i] = first_e[e[i].b]; first_e[e[i].b] = i; if(show[e[i].b] == 0){ show[e[i].b] = 1; num_v++; } if(show[e[i].e] == 0){ show[e[i].e] = 1; num_v++; } switch(toposort(i+1, num_v)){ case SORTED: printf("Sorted sequence determined after %d relations: ", i+1); for(int j = 0; j < N; j++){ printf("%c", 'A' + out[j]); } printf(".\n"); is_stop = true; break; case NOT_DETERMINED: break; case INCONSISTENCY: printf("Inconsistency found after %d relations.\n", i+1); is_stop = true; break; } } if(!is_stop) printf("Sorted sequence cannot be determined.\n"); } return 0; }
int main(void){ bool n, p; int i; while(gets(c[0])){ n = 1, p = 0; memset(d,-1,32); memset(a,0,1024); while(gets(c[n]) && c[n][0] != '#'){ c2 = &c[n][0]; c1 = &c[p][0]; while((*c1 && *c2) && (*c1 == *c2)) c1++, c2++; if(*c1 && *c2){ char pc = *c1 - 'A', nc = *c2 - 'A'; if(!a[pc][nc]){ //printf("%c->%c\n",*c1,*c2); if(d[pc] == -1) d[pc] = 0; if(d[nc] == -1) d[nc] = 0; a[pc][nc] = 1; d[nc]++; } } n = !n; p = !p; } for(i = 0; i < 26; i++) if(d[i] == 0){ toposort(i); break; } putc(10, stdout); } return 0; }
void solve(int n) { for (int i = 0; i < n; i++) if (!reach[i]) dfs(i); build_newgraph(n); memset(reach, false, sizeof (reach));//reuse reach for (int i = 0; i < sccCnt; i++) if (!reach[i]) toposort(i); reverse(tms, tms + pt);//Topological Sort }
void toposort(int k){ putc(k+'A', stdout); int i, q[32], qs = 0; for(i = 0; i < 26; i++){ if(a[k][i] && --d[i] == 0) q[qs++] = i; } for(i = 0; i < qs; i++) toposort(q[i]); }
int main(){ int t; scanf("%d", &t); while (t--){ // Init. memset(map, 0, sizeof(map)); memset(num, 0, sizeof(num)); memset(in, 0, sizeof(in)); // Read. scanf("%d%d", &c, &r); for (int i = 0; i < r * c; i++){ char str[N]; scanf("%s", str); int move = 1, f = 0, t = 0; if (str[0] == '='){ int len = strlen(str); str[len] = '+'; str[len + 1] = '\0'; while (str[move]){ if (is_alph(str[move])) f = f * 26 + str[move] - 'A' + 1; else if (is_num(str[move])) t = t * 10 + str[move] - '0'; else{ map[i][in[i]++] = (t - 1) * c + f - 1; t = f = 0; } move++; } } else sscanf(str, "%d", &num[i]); } // Handle. toposort(); // Printf. for (int i = 0; i < r * c; i++){ if (i && !(i % c)) printf("\n"); if (i % c) printf(" "); printf("%d", num[i]); } printf("\n"); } return 0;}
int main ( ) { int x, y; while ( scanf ( "%d%d", &n, &m ) != EOF ) { if ( !n && !m ) break; memset ( G, 0, sizeof ( G ) ); while ( m-- ) { scanf ( "%d%d", &x, &y ); G[x][y] = 1; } toposort ( ); for ( int i = 0; i < n-1; ++i ) printf ( "%d ", topo[i] ); printf ( "%d\n", topo[n-1] ); } }
int main() { scanf("%d%d", &n, &m); memset(G, 0, sizeof(G)); for(int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); G[u][v] = 1; } if(toposort()) { for(int i = 0; i < n; i++) printf("%d\n", topo[i]); } else printf("No\n"); }
int main() { int i, ll; scanf("%d", &n); init_graph(); ll = toposort(); if (ll > 0) printf("%d", a[l[0]]); for (i = 1; i < ll; ++i) { printf(" %d", a[l[i]]); } printf("\n"); return 0; }
int toposort(int cur){ int i,j; if (cur==n){ return 1; } for (i=1;i<=n;i++) if (ru[i]==0){ ru[i]=-1; for (j=0;j<child[i];j++) ru[next[i][j]]--; ans[cur]=i; if (toposort(cur+1)) return 1; ru[i]=0; for (j=0;j<child[i];j++) ru[next[i][j]]++; } return 0; }
int main(){ int T; int i,m,a,b; scanf("%d",&T); while (T--){ memset(ru,0,sizeof(ru)); memset(child,0,sizeof(child)); scanf("%d%d",&n,&m); while (m--){ scanf("%d%d",&a,&b); ru[b]++; next[a][child[a]++]=b; } if (toposort(0)) print(); else printf("-1\n"); } return 0; }
int main(void){ int i,x,y,k; while(scanf("%d%d",&n,&m)==2&&m+n){ memset(head,0,sizeof(head)); memset(degree,0,sizeof(degree)); for(i=k=1;i<=m;i++){ scanf("%d%d",&x,&y); degree[y]++; val[k]=y; next[k]=head[x]; head[x]=k++; } if(toposort()) for(i=1;i<=n;i++) printf("%d\n",ans[i]); else puts("IMPOSSIBLE"); } return 0; }
int main() { int x, y; while (scanf("%d %d", &n, &m) != EOF) { memset(map, 0, sizeof(map)); memset(index, 0, sizeof(index)); memset(used, 0, sizeof(used)); for (int i = 1; i <= m; i++) { scanf("%d %d", &x, &y); if (map[x][y] == 0) { map[x][y] = 1; index[y]++; } } toposort(); printf("\n"); } return 0; }
int main() { #ifndef ONLINE_JUDGE freopen("input/uva10305.in", "r", stdin); #endif while(scanf("%d%d", &n, &m) == 2 && n) { memset(G, 0, sizeof(G)); for(int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--; v--; G[u][v] = 1; } if(toposort()) { for(int i = 0; i < n-1; i++) printf("%d ", topo[i]+1); printf("%d\n", topo[n-1]+1); } else printf("No\n"); // 题目没说无解输出什么,应该是保证有解吧 } }
main() { int i; vertex u,v; edge e; string s; Digraph dg; dg.read(cin); int *pos = new int[dg.n()+1]; vertex *vert = new vertex[dg.n()+1]; toposort(dg,pos,vert); Digraph inOrder(dg.n(),dg.m()); cout << "# "; for (i = 1; i <= dg.n(); i++) { u = vert[i]; cout << Util::node2string(u,dg.n(),s) << "->"; cout << Util::node2string(pos[u],dg.n(),s) << " "; if ((i%10) == 0) cout << "\n# "; for (e = dg.firstOut(u); e != 0; e=dg.nextOut(u,e)) { v = dg.head(e); inOrder.join(pos[u],pos[v]); } } inOrder.sortAdjLists(); cout << endl << inOrder.toString(s); }
int main() { while(scanf("%d%d", &n, &m) == 2 && (n || m)) { memset(E, 0, sizeof(E)); int i, j; int cntdn = m; while(cntdn--) { scanf("%d%d", &i, &j); E[i][j] = 1; } if(toposort()) { for(int i = 0; i < n; i++) { printf("%d", topo[i]); if(i != n-1) putchar(' '); } } else printf("No"); putchar('\n'); } return 0; }
list< pair<int,int> > MATCH( const vector<double> & P, const vector<double> & Q, double length ) { list< pair<int,int> > final_match ; /* sort indices of P and Q into place_data situated on a segment */ segment_type segment ; segment[0.] ; segment[length] ; // create the endpoints for ( int i=0 ; i < P.size() ; i++ ) { double y = P[i] ; assert ( y >= 0. && y <= length ) ; segment[y].P_indices.push_back( i ) ; } for ( int i=0 ; i < Q.size() ; i++ ) { double y = Q[i] ; assert ( y >= 0. && y <= length ) ; segment[y].Q_indices.push_back( i ) ; } /* pre-process the place_data to determine immediate matches */ for ( segment_type::iterator it = segment.begin() ; it != segment.end() ; ++it ) { it->second.annihilate( final_match ) ; } cout << "segment = " << segment << endl ; cout << "PREMATCH = " << final_match << endl ; Arrangement value_arr = segment_cost_characterization( segment ) ; list< interval_struct > interval_data ; Arrangement::Interval * I_curr = & *value_arr.vertices.begin()->second.left ; // gross while ( true ) { interval_data.push_back( I_curr->local_data ) ; if ( I_curr->right == NULL ) break ; I_curr = & *I_curr->right->right ; } cout << "lines: " << interval_data << endl ; /* prepare the instance graph */ intgraph g ; vector< segment_type::iterator > places ; // index of a place *is* it's graph vertex id /* assign a graph vertex to each node */ for ( segment_type::iterator it = segment.begin() ; it != segment.end() ; ++it ) { g.new_vertex() ; places.push_back( it ) ; } /* construct the instance graph */ vector<int> edge_weight ; // index is edge id { // don't want to intrude on local scope int level = 0 ; // someday, will be z_r, instead segment_type::iterator lbound, rbound ; rbound = segment.begin() ; int idx = 0 ; // left node index while ( true ) { lbound = rbound++ ; if ( rbound == segment.end() ) break ; // no intervals left place_data & temp = lbound->second ; level += temp.P_indices.size() - temp.Q_indices.size() ; // possibly make an edge int e ; if ( level > 0 ) { e = g.new_edge( idx, idx+1 ) ; edge_weight.push_back( level ) ; } else if ( level < 0 ) { e = g.new_edge( idx+1, idx ) ; edge_weight.push_back( -level ) ; } idx++ ; } } cout << "graph " << g << endl ; list<int> order_list = toposort( g ) ; cout << "order list " << order_list << endl ; /* enumerate the places in the topological order and push the queues around */ vector<int> order( order_list.begin(), order_list.end() ) ; for ( int i=0 ; i < order.size() ; i++ ) { int u = order[i] ; segment_type::iterator it = places[u] ; // annihilate if you can place_data & source = it->second ; source.annihilate( final_match ) ; // split your lists --- // remember, P is *always* the positive queue after annihilation (b/c topo) // to be "fair", make sure to pre-pend the source list in *front* of the target one set<int> & u_out_edges = g.AdjV[u] ; for ( set<int>::iterator e_it = u_out_edges.begin() ; e_it != u_out_edges.end() ; ++e_it ) { int e = *e_it ; int v = g.AdjE[e].second ; place_data & target = places[v]->second ; int capacity = edge_weight[e] ; list<int> & sP = source.P_indices, &tP = target.P_indices ; if ( capacity < sP.size() ) // then, we *have* to suffer linear time { list<int>::iterator stop ; stop = sP.begin() ; advance( stop, capacity ) ; tP.splice( tP.begin(), sP, sP.begin(), stop ) ; } else // this branch is constant time! { tP.splice( tP.begin(), sP ) ; } } } cout << "final match " << final_match << endl ; return final_match ; }
string alienOrder(vector<string>& words) { if (words.size() == 1) return words[0]; graph g = make_graph(words); return toposort(g); }
void toposort(int x) { reach[x] = true; for (edge *i = gscc[x]; i != NULL; i = i->nxt) if (!reach[i->v]) toposort(i->v); tms[pt++] = x; }