int BFS() { int i,tmp,min,tmppre; for(i=0;i<=m+1;i++) { vis[i]=false; pre[i]=-1; } vis[0]=true; while(!Q.empty()) Q.pop(); Q.push(0); while(!Q.empty()) { tmp=Q.front(); Q.pop(); for(i=1;i<=m+1;i++) { if(i==tmp || vis[i] || !map[tmp][i]) continue; vis[i]=true; pre[i]=tmp; Q.push(i); } } if(pre[m+1]==-1) return -1; min=INF,tmp=m+1; while((tmppre=pre[tmp]) != -1) { if(map[tmppre][tmp]<min) min=map[tmppre][tmp]; tmp=tmppre; } return min; }
int find(){ memset(map,0,sizeof(map)); while(!que.empty())que.pop(); begin.step=0; que.push(begin); int x,y,step; while(!que.empty()){ tmp=que.front(); x=tmp.x,y=tmp.y,step=tmp.step; que.pop(); if(x==end.x && y==end.y)break; for(int i=0;i<8;i++){ tmp.x=x+str[i][0]; if(tmp.x<0 || tmp.x>=m)continue; tmp.y=y+str[i][1]; if(tmp.y<0 || tmp.y>=m)continue; if(map[tmp.x][tmp.y])continue; map[tmp.x][tmp.y]=1; tmp.step=step+1; que.push(tmp); } } return step; }
void Spfa() { for (int i = 0; i <= n; i++) inq[i] = 0, dist[i] = INF; dist[st] = 0, inq[st] = 1; Q.push(st); while (!Q.empty()) { int u = Q.front(); Q.pop(); inq[u] = 0; for (Edge *i = adj[u]; i != NULL; i = i->next) { if (i->cap == 0 || dist[i->v] <= dist[u] + i->cost) continue; dist[i->v] = dist[u] + i->cost; if (!inq[i->v]) { inq[i->v] = 1; Q.push(i->v); } } } int ded = dist[ed]; for (int i = 0; i <= n; i++) dist[i] = ded - dist[i]; }
int main() { int n,d; while(scanf("%d",&n)==1){ while(n--){ scanf("%d",&d); s.push(d); t.push(d); } printf("队列正向输出:"); while(!s.empty()){ printf(" %d",s.front()); s.pop(); } printf("\n"); printf("堆栈逆向输出:"); while(!t.empty()){ printf(" %d",t.top()); t.pop(); } printf("\n"); } return 0; }
int bfs(){ q.push((node){a,0}); mp[a]=1; while(!q.empty()){ node t=q.front();q.pop(); if(t.t>=10)return -1; string S=t.s; for(int i=1;i<=n;i++){ int x=S.find(str[i][0],0); while(x!=string::npos){ S.replace(x,str[i][0].size(),str[i][1].c_str(),str[i][1].size()); if(!mp[S]){ if(b==S)return t.t+1; node d=(node){S,t.t+1}; q.push(d); mp[S]=1; } S=t.s; x=S.find(str[i][0],x+1); } } } return -1;//注意这里 }
void CReplay :: AddTimeSlot( uint16_t timeIncrement, queue<CIncomingAction *> actions ) { BYTEARRAY Block; Block.push_back( REPLAY_TIMESLOT ); UTIL_AppendByteArray( Block, (uint16_t)0, false ); UTIL_AppendByteArray( Block, timeIncrement, false ); while( !actions.empty( ) ) { CIncomingAction *Action = actions.front( ); actions.pop( ); Block.push_back( Action->GetPID( ) ); UTIL_AppendByteArray( Block, (uint16_t)Action->GetAction( )->size( ), false ); UTIL_AppendByteArrayFast( Block, *Action->GetAction( ) ); } // assign length BYTEARRAY LengthBytes = UTIL_CreateByteArray( (uint16_t)( Block.size( ) - 3 ), false ); Block[1] = LengthBytes[0]; Block[2] = LengthBytes[1]; m_CompiledBlocks += string( Block.begin( ), Block.end( ) ); m_ReplayLength += timeIncrement; }
int bfs(int ci,int x,int y) { w[ci][x][y].mark=1; node1 e; e.c1=ci; e.x1=x; e.y1=y; q.push(e); while(!q.empty()) { node1 r=q.front(); q.pop(); int step=w[r.c1][r.x1][r.y1].num; for(int ll=0;ll<6;ll++) { int cc,yy,xx; cc=r.c1+dir[ll][0]; xx=r.x1+dir[ll][1]; yy=r.y1+dir[ll][2]; if((cc<0||cc>=k||xx<0||xx>=m||yy<0||yy>=n)||w[cc][xx][yy].mark||s[cc][xx][yy]=='#')continue; else { w[cc][xx][yy].mark=1; w[cc][xx][yy].num=step+1; if(s[cc][xx][yy]=='E')return step+1; node1 t; t.c1=cc; t.x1=xx; t.y1=yy; q.push(t); } } } return 0; }
void processIPResponse(string myname, string msg, queue<string> &pendingPrivateMessages) { string host, port, ppm; int sock, iport; if (!pendingPrivateMessages.size()) { cerr << "bailing out of IPResponse. no pending messages" << endl; return; } if (!parse(msg, host, port)) { cerr << "Failed to send private message" << endl; pendingPrivateMessages.pop(); return; } iport = atoi(port.c_str()); if ((sock = connectToServer(host.c_str(), iport, SOCK_DGRAM)) < 0) { cerr << "Couldn't connect to " << host << ":" << port << " for private message." << endl; pendingPrivateMessages.pop(); return; } ppm = pendingPrivateMessages.front(); ppm = "PeerMessage " + myname + " " + ppm; pendingPrivateMessages.pop(); socketSend(sock, ppm); }
int augment_path(int s, int t) { memset(from, -1, sizeof(from)); from[s] = s; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int v = 0; v < MAX; v++) { if(from[v] != -1) continue; if(cap[u][v] == 0) continue; from[v] = u; Q.push(v); } } if(from[t] == -1) return 0; int flow = cap[from[t]][t]; for(int v = t; v != s; v = from[v]) { int u = from[v]; flow = min(flow, cap[u][v]); } for(int v = t; v != s; v = from[v]) { int u = from[v]; cap[u][v] -= flow; cap[v][u] += flow; } return flow; }
int reach(int limit) { memset(d, -1, sizeof(d)); while (!Q.empty()) Q.pop(); d[sx][sy] = 0; Q.push(MP(sx, sy)); while (!Q.empty()) { pair<int, int> cur = Q.front(); Q.pop(); int x = cur.first, y = cur.second; for (int k = 0; k < 4; k++) { int xx = x + dx[k], yy = y + dy[k]; if (!check(xx, yy)) continue; if (dist[xx][yy] < limit || d[xx][yy] >= 0) continue; d[xx][yy] = d[x][y] + 1; Q.push(MP(xx, yy)); } } return d[ex][ey] >= 0; }
/** * 2. Breadth First Search */ bool bfs(int src, int dest = V+1) { visited.clear(); visit_order.clear(); // optional queue<int>().swap(que); // clear queue `que` by swapping it with an empty one que.push(src); while(!que.empty()) { int u = que.front(); que.pop(); if(visited.count(u) > 0) continue; visited.insert(u); visit_order.push_back(u); // optional if(u == dest) return true; for(auto &v: g[u]) que.push(v); } return false; }
void insert(int &rt,int num) { if(rt==0) { rt=Q.front(); Q.pop(); LL(rt)=RR(rt)=0; tot[rt]=1; v[rt]=num; begin[rt]=end[rt]=num; ans[rt]=num; return; } tot[rt]++; if(num<v[rt]) insert(LL(rt),num); else insert(RR(rt),num); update(rt); if(num<v[rt]) Maintain(rt,false); else Maintain(rt,true); }
int TaskMgr::mcmf(int s,int t){ int flow=0; while(true){ memset(pre,-1,sizeof(pre)); memset(vis,false,sizeof(vis)); memset(wgt,0x7f,sizeof(wgt)); wgt[s]=0; que.push(s); while(!que.empty()){ int u=que.front(); que.pop(); vis[u]=false; for(int i=head[u];~i;i=E[i].next){ int v=E[i].vtx; if(E[i].cap&&wgt[u]+E[i].wgt<wgt[v]){ wgt[v]=wgt[u]+E[i].wgt; if(!vis[v]){ vis[v]=true; que.push(v); } pre[v]=u; cur[v]=i; } } } if(pre[t]==-1) break; int mf=0x7fffffff; for(int v=t;v!=s;v=pre[v]){ mf=min(mf,E[cur[v]].cap); } for(int v=t;v!=s;v=pre[v]){ E[cur[v]].cap-=mf; E[cur[v]^1].cap+=mf; } flow+=mf; } return flow; }
void pickup_from_queue(queue<midi_message_t>& queue, void *jack_midi_buffer, struct timespec& prev_cycle, struct timespec& cycle_period, jack_nframes_t nframes ) { jack_nframes_t last_framepos = 0; while(!queue.empty()) { midi_message_t msg = queue.front(); long nsec_since_start = diff(prev_cycle, msg.time).tv_nsec; long framepos = (nsec_since_start * nframes) / cycle_period.tv_nsec; if (framepos <= last_framepos) { framepos = last_framepos + 1; } if (framepos >= nframes) { framepos = nframes - 1; } if (state == LISTEN && &queue == &controller_queue) { process_controller_out_message(msg); } uint8_t *buffer = jack_midi_event_reserve(jack_midi_buffer, framepos, msg.buffer.size()); if (buffer) { memcpy(buffer, msg.buffer.data(), msg.buffer.size()); } else { fprintf(stderr, "failed to allocate %d bytes midi buffer at framepos %ld (nframes = %d)", msg.buffer.size(), framepos, nframes); } queue.pop(); } }
bool bfs() { memset(d,0x3f,sizeof(d)); q.empty(); for (int i=0; i<n; i++) currEdge[i]=lastEdge[i]; q.push(s); d[s]=0; while (!q.empty()) { int u = q.front(); q.pop(); for (int i=lastEdge[u]; i!=-1; i=ls[i].prev) if (ls[i].c-ls[i].f>0 && d[ls[i].dst]==inf) { int v=ls[i].dst; d[v]=d[u]+1; q.push(v); } } return d[t]!=inf; }
void bfs2() { while (!Q.empty()) Q.pop(); Q.push(sb); vis[sb.first][sb.second] = 0; while (!Q.empty()) { pii u = Q.front(); if (u.first == 1 || u.first == n || u.second == 1 || u.second == m) { printf("%d\n", vis[u.first][u.second] + 1); return; } Q.pop(); for (int i = 0; i < 4; i++) { int x = u.first + d[i][0]; int y = u.second + d[i][1]; if (x < 1 || x > n || y < 1 || y > m) continue; if (g[x][y] == '#') continue; if (f[x][y] != -1 && vis[u.first][u.second] + 1 >= f[x][y]) continue; if (vis[x][y] != -1) continue; vis[x][y] = vis[u.first][u.second] + 1; Q.push(make_pair(x, y)); } } printf("IMPOSSIBLE\n"); }
int main() { int n,m; scanf("%d%d",&n,&m); for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) scanf("%d",&a[i][j]); f[(1<<n)-1]=1; Q.push((1<<n)-1); while (!Q.empty()) { int x=Q.front(); Q.pop(); for (int i=1;i<=m;i++) { int y=x; for (int j=1;j<=n;j++) { if (a[i][j]==1) y&=~(1<<j-1); if (a[i][j]==-1) y|=1<<j-1; } if (f[y]==0) { f[y]=f[x]+1; Q.push(y); } } } if (f[0]==0) printf("The patient will be dead.\n"); else printf("%d\n",f[0]-1); return(0); }
BYTEARRAY CGameProtocol :: SEND_W3GS_INCOMING_ACTION( queue<CIncomingAction*> actions, uint16_t sendInterval ) { BYTEARRAY packet; packet.push_back( W3GS_HEADER_CONSTANT ); // W3GS header constant packet.push_back( W3GS_INCOMING_ACTION ); // W3GS_INCOMING_ACTION packet.push_back( 0 ); // packet length will be assigned later packet.push_back( 0 ); // packet length will be assigned later UTIL_AppendByteArray( packet, sendInterval, false ); // send interval // create subpacket if( !actions.empty() ) { BYTEARRAY subpacket; while( !actions.empty() ) { CIncomingAction* Action = actions.front( ); actions.pop( ); subpacket.push_back( Action->GetPID() ); UTIL_AppendByteArray( subpacket, static_cast<uint16_t>( Action->GetAction()->size() ), false ); UTIL_AppendByteArrayFast( subpacket, *Action->GetAction() ); } // calculate crc (we only care about the first 2 bytes though) BYTEARRAY crc32 = UTIL_CreateByteArray( m_GHost->m_CRC->FullCRC( (unsigned char*)( string( subpacket.begin(), subpacket.end() ).c_str() ), subpacket.size() ), false ); crc32.resize( 2 ); // finish subpacket UTIL_AppendByteArrayFast( packet, crc32 ); // crc UTIL_AppendByteArrayFast( packet, subpacket ); // subpacket } AssignLength( packet ); return packet; }
void spfa(double dist[],int src) { for(int i = 1; i <= n; ++i) dist[i] = INF,inQue[i] = 0; while(!que.empty()) que.pop(); que.push(src); dist[src] = 0; inQue[src] = true; while(!que.empty()) { int u = que.front();que.pop(); for(int i = head[u]; i + 1 ; i =edge[i].next) { int v = edge[i].u; int val = edge[i].cost; if(src == p && v == d) continue; if(dist[u] + val < dist[v]) { dist[v] = dist[u] + val; if(!inQue[v]) que.push(v),inQue[v] = true; } } inQue[u] = false; } }
void CReplay :: AddTimeSlot( uint16_t timeIncrement, queue<CIncomingAction *> actions ) { BYTEARRAY Block; Block.push_back( REPLAY_TIMESLOT ); UTIL_AppendByteArray( Block, (uint16_t)0, false ); UTIL_AppendByteArray( Block, timeIncrement, false ); while( !actions.empty( ) ) { CIncomingAction *Action = actions.front( ); actions.pop( ); BYTEARRAY ActionData = Action->GetAction( ); Block.push_back( Action->GetPID( ) ); UTIL_AppendByteArray( Block, (uint16_t)ActionData.size( ), false ); UTIL_AppendByteArray( Block, ActionData ); } // assign length BYTEARRAY LengthBytes = UTIL_CreateByteArray( (uint16_t)( Block.size( ) - 3 ), false ); Block[1] = LengthBytes[0]; Block[2] = LengthBytes[1]; m_Blocks.push( Block ); }
int main() { int i, P; cin >> n; data = new int[n]; for (i = 0; i < n; ++i) cin >> data[i]; P = 1; for (i = 0; i < n; ++i) { for (; P <= n + 1; ++P) { if (!input.empty()) if (input.top() == data[i]) { input.pop(); save.push('-'); break; } input.push(P); save.push('+'); } } if (input.empty() == true) { while (!save.empty()) { cout << save.front() << "\n"; save.pop(); } } else cout << "NO\n"; return 0; }
inline bool BF(int Source, int Sink) { /// BellmanFord for(int i = Source ; i <= Sink ; ++ i) dp[i] = oo; Q.push(Source); inQ[Source] = 1; dp[Source] = 0; while(!Q.empty()) { int Node = Q.front(); Q.pop(); inQ[Node] = 0; if(Node == Sink) continue; for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) if(C[Node][*it] > 0 && dp[*it] > dp[Node] + cost[Node][*it]) { dp[*it] = dp[Node] + cost[Node][*it]; Father[*it] = Node; if(inQ[*it]) continue; Q.push(*it); inQ[*it] = 1; } } return (dp[Sink] != oo); }
string wml::CupsdCtrl::getDirective (queue<pair<string, string> > containerId, const string& key, const bool valueOnly) const { string returnStr; bool containerFound = false; pair <string, string> temp = containerId.front(); containerId.pop(); list<CupsdDirContainer>::const_iterator iter; for (iter = this->directiveContainers.begin(); iter != this->directiveContainers.end(); iter++) { string containerIdString = temp.first + " " + temp.second; if (iter->getId() == containerIdString) { containerFound = true; iter->getDirective(containerId, key, returnStr, valueOnly); } } if (containerFound == false) { throw runtime_error("Container not found 1"); } return returnStr; }
int main() { int N, M; cin >> N >> M; int p, q; for (auto i = 0; i < M; i++) { cin >> p >> q; p--; q--; S.insert(make_pair(p, q)); S.insert(make_pair(q, p)); } A.push(0); for (auto i = 1; i < N; i++) { B.insert(i); } while (!A.empty()) { int x = A.front(); A.pop(); for (auto it = B.begin(); it != B.end(); ) { if (S.find(make_pair(x, *it)) == S.end()) { V.push_back(make_pair(x, *it)); A.push(*it); it = B.erase(it); } else { it++; } } } if ((int)V.size() != N-1) { cout << "No" << endl; } else { cout << "Yes" << endl; for (auto x : V) { cout << x.first+1 << " " << x.second+1 << endl; } } }
int main() { int n,a; scanf("%d",&n); for(int i=0;i<n*2;i++) { scanf("%d",&a);a--; if(czy[a]==0) { czy[a]=1; s.push(a); } else { while(s.top()!=a) { ruchy.push(s.size()); temp.push(s.top()); s.pop(); } s.pop(); while(!temp.empty()) { s.push(temp.top()); temp.pop(); } } } printf("%d\n",ruchy.size()); while(!ruchy.empty()) { printf("%d\n",ruchy.front()); ruchy.pop(); } return 0; }
void BFS(int s){ while(!q.empty()) q.pop(); if(!vis[s]) q.push(s); while(!q.empty()){ int front=q.front(); vis[front]=true; q.pop(); if(Node[front].level>L) return ; if(Node[front].level<=L && Node[front].level > 0) num_forward++; //if(Node[front].level > L) return ; for(int i=0;i<Node[front].child.size();i++){ int now=Node[front].child[i].id; Node[now].level=Node[front].level+1; if(!vis[now]) q.push(now); } } }
bool flow(int s){ memset(mtp, -1, sizeof(mtp)); while(qu.size()) qu.pop(); qu.push(s); mtp[s] = 0; bk[s] = pr[s] = -1; while(qu.size() && pr[s] == -1){ int u = qu.front(); qu.pop(); for(int v=0; v<V; v++){ if (el[u][v] == 0) continue; if (ffa(v) == ffa(u)) continue; if(pr[v] == -1){ do{ int t = pr[u]; pr[v] = u; pr[u] = v; v = t; u = t==-1?-1:bk[t]; }while( v != -1 ); break; }else if(mtp[v] == 0){ int w = lca(u, v); if(ffa(w) != ffa(u)) bk[u] = v; if(ffa(w) != ffa(v)) bk[v] = u; flower(u, w); flower(v, w); }else if(mtp[v] != 1){ bk[v] = u; mtp[v] = 1; mtp[pr[v]] = 0; qu.push(pr[v]); } } } return pr[s] != -1; }
int main() { int n,m,i,j,l,r,x; scanf("%d%d",&n,&m); for(i=0;i<m;i++) { l = input(l) - 1; r = input(r) - 1; a[l].PB(r); a[r].PB(l); } for(i=0;i<n;i++) color[i] = 0; for(j=0;j<n;j++) { if(color[j]) continue; color[j] = 1; Q.push(j); while(!Q.empty()) { int u = Q.front(); Q.pop(); for(i=0;i<a[u].size();i++) { int s = a[u][i]; if(!color[s]) { color[s] = 1; Q.push(s); } } } } return 0; }
//改变矩阵 void changeMatrix(vector<vector<int>>& matrix, queue<vector<int>> row_num_column_num) { int rows = (int)matrix.size(); int colums = (int)matrix[0].size(); int row_number, column_number; while(!row_num_column_num.empty()) { vector<int> temp = row_num_column_num.front(); row_number = temp[0]; column_number = temp[1]; for(int i = 0; i < rows; i++) { for(int j = 0; j < colums; j++) { if( i == row_number || j == column_number) //清零 { matrix[i][j] = 0; } } } row_num_column_num.pop(); } }
int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &k); for(int i=0; i<=n; i++)g[i].clear(); while(!q.empty())q.pop(); for(int i=0; i<n-1; i++) { int u, v; scanf("%d%d", &u, &v); q.push(Node(u, v, 1)); q.push(Node(v, u, 1)); g[u].push_back(v); g[v].push_back(u); } LL ans=0; int cnt=0; for(int i=0; i<2*k && !q.empty(); i++) { Node x=q.front(); q.pop(); ans+=x.len; for(int j=0; j<g[x.u].size() && cnt<=2*k+10; j++) if(g[x.u][j]!=x.v) { q.push(Node(g[x.u][j], x.u, x.len+1)); ++cnt; } } printf("%I64d\n", ans>>1); } return 0; }