void Graph::shortestPath(int src){ SI s; int dist[V]; bool visit[V]; fill(visit, visit + V, false); fill(dist, dist+V, INT_MAX); for(int i=0; i<V; i++){ if(!visit[i]) topologicalSort(i, visit, s); } dist[src] = 0; while(!s.empty()){ int u = s.top(); s.pop(); LN::iterator iter = adj[u].begin(); while(iter != adj[u].end()){ int v = (*iter).v; if(dist[u] != INT_MAX && dist[v] > dist[u] + (*iter).weight) dist[v] = dist[u] + (*iter).weight; iter++; } } for(int i=0; i<V; i++){ cout<<i<<" "<<dist[i]<<'\n'; } }
QWidget * qt_glue_widget_rep::as_qwidget() { QLabel *w= new QLabel(); w->setText(to_qstring(as_string(col))); QIcon icon; w->setPixmap (render ()); // w->setEnabled(false); return w; }
int main(){ scanf("%d%d", &noV, &noE); G = VVI(noV+1, VI(0, 0)); RG = VVI(noV+1, VI(0, 0)); ans = VVI(noV+1, VI(0,0)); vis = VB(noV+1, false); for(int i = 0 ; i < noE ; i++){ int u,v; scanf("%d%d", &u, &v); G[u].push_back(v); RG[v].push_back(u); } for(int i = 1 ; i <= noV ; i++){ if(!vis[i]){ vis[i] = true; dfs(i); st.push(i); } } vis = VB(noV+1, false); vInfoAns = VI(noV+1, -1); while(!st.empty()){ int here = st.top(); st.pop(); if(!vis[here]){ vis[here] = true; ans[noAns].push_back(here); vInfoAns[here] = noAns; dfsReverse(here); noAns++; sort(ans[noAns-1].begin(), ans[noAns-1].end()); } } vis = VB(noAns+1, false); printf("%d\n", noAns); for(int i = 1 ; i <= noV ; i++){ int here = vInfoAns[i]; // == noAns if(!vis[here]){ vis[here] = true; for(auto iter = ans[here].begin() ; iter != ans[here].end() ; iter++){ printf("%d ",*iter); } puts("-1"); } } return 0; }
void Graph::topologicalSort(int src, bool *visit, SI &s){ visit[src] = true; LN::iterator iter = adj[src].begin(); while(iter != adj[src].end()){ if(!visit[(*iter).v]){ topologicalSort((*iter).v, visit, s); } iter++; } s.push(src); }
bool isgoal(){ for(p=0;p<9;p+=3) for(q=0;q<9;q+=3){ for(i=p;i<p+3;i++) for(j=q;j<q+3;j++) if(M[i][j]!=0&&(s.insert(M[i][j])).second==false) return false; s.clear(); } for(i=0;i<9;i++){ s.clear(); for(j=0;j<9;j++) if(M[i][j]==0||(s.insert(M[i][j])).second==false) return false; } for(i=0;i<9;i++){ s.clear(); for(j=0;j<9;j++) if(M[j][i]==0||(s.insert(M[j][i])).second==false) return false; } return true; }
bool isgoal() { for(p=0; p<16; p+=4) for(q=0; q<16; q+=4) { for(i=p; i<p+4; i++) for(j=q; j<q+4; j++) if(M[i][j]!=0&&(s.insert(M[i][j])).second==false) return false; s.clear(); } for(i=0; i<16; i++) { s.clear(); for(j=0; j<16; j++) if(M[i][j]==0||(s.insert(M[i][j])).second==false) return false; } for(i=0; i<16; i++) { s.clear(); for(j=0; j<16; j++) if(M[j][i]==0||(s.insert(M[j][i])).second==false) return false; } return true; }
void dfs(int vertex){ for(auto iter = G[vertex].begin() ; iter != G[vertex].end() ; iter++){ int here = *iter; if(!vis[here]){ vis[here] = true; dfs(here); st.push(here); } } return ; }
int main(int argc, char const *argv[]) { int n, c, s; while(SC2(n, k) && n) { int lt = 0; ZERO(in); ZERO(out); for(int i = 1; i <= n; i++) { SC2(c, s); in[c] = i; out[s] = i; lt = max(lt, s+1); } while(!p.empty()) p.pop(); int i; for(i = 1; i < lt; i++) { if(out[i]){ if(p.empty() || p.top() != out[i]) break; else p.pop(); } if(in[i]){ if((int)p.size() < k) p.push(in[i]); else break; } } printf("%s\n", i == lt ? "Sim" : "Nao"); } }