int main(){ int tc, n, g, u, v; cin >> tc; while (tc--){ cin >> n >> g; AdjList.assign(n, vi()); for (int i = 0; i < g; ++i){ cin >> u >> v; --u; --v; AdjList[u].push_back(v); } if (n == 0){ cout << 0 << endl; continue; } dfs_num.assign(n, DFS_WHITE); dfs_low.assign(n, 0); visited.assign(n, 0); scc_weight.assign(n, 0); scc_id.assign(n, -1); dfsCounter = numSCC = 0; for (int i = 0; i < n; i++) if (dfs_num[i] == DFS_WHITE) tarjanSCC(i); SccAdj.assign(numSCC, vi()); for (int u = 0; u < n; ++u) for (auto &v : AdjList[u]) if (scc_id[v] != scc_id[u]) SccAdj[scc_id[u]].push_back(scc_id[v]); dfs_num.assign(numSCC, DFS_WHITE); topoSort.clear(); for (int i = 0; i < numSCC; i++) if (dfs_num[i] == DFS_WHITE) dfs(i); vi dist(numSCC, 0); int best = 1; for (int u = topoSort.size() - 1; u >= 0; --u){ dist[topoSort[u]] = min(dist[topoSort[u]], scc_weight[topoSort[u]]); best = min(best, dist[topoSort[u]]); for (auto &v : SccAdj[topoSort[u]]){ dist[v] = min(dist[v], dist[topoSort[u]] + scc_weight[v]); best = min(best, dist[v]); } } cout << -best << endl; } }
int main() { ios::sync_with_stdio(0); int tc; cin >> tc; for(int i = 1; i <= tc; i++) { cout << "Case #" << i << ": "; int m; cin >> n >> m >> k; graph.assign(n, vi(n, inf)), works.assign(k, ii(0, 0)); for(int i = 0; i < n; i++) graph[i][i] = 0; for(int i = 0; i < m; i++) { ll a, b, c; cin >> a >> b >> c; a--, b--; graph[a][b] = graph[b][a] = min(graph[a][b], c); } for(int i = 0; i < k; i++) { int a, b; cin >> a >> b; works[i].first = a - 1, works[i].second = b - 1; } for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]); memo.assign(k + 1, vi(n + 1, -1)); int ans = solve(0, works[0].first) + graph[0][works[0].first]; if(ans >= inf) cout << -1 << endl; else cout << ans << endl; } return 0; }
int main(){ FILE *f; f =fopen("input/OrderingTasks.in", "r"); #ifdef ONLINE_JUDGE f = stdin; #endif int m,n; while(fscanf(f, "%d %d", &n, &m) == 2 && n){ g.clear(); g.assign(n, vi()); for(int i = 0 ; i < m ; i++){ int u,v; fscanf(f, "%d %d", &u,&v); g[u-1].push_back(v-1); } vi sortedV; for(int i = 0 ; i < n ; i++){ if(!visited[i]) dfs(i, sortedV); } memset(visited, 0, sizeof visited); for(int i = sortedV.size() -1 ; i >= 0 ; i--){ printf("%d", sortedV[i] + 1); if(i)printf(" "); else printf("\n"); } } return 0; }
int main(){ int tc, cs=1; cin >> tc; while( tc-- ){ memset(res,0,sizeof res); MP.clear(); mf=0; string s1, s2; adj.assign(MAX,vi()); cin >> m; n=1; s=0; t=MAX-1; for(int i=0 ; i<m ; i++){ cin >> s1; res[mp(s1)][t]=1; adj[mp(s1)].push_back(t); } cin >> m; int devs = m; for(int i=0 ; i<m ; i++){ cin >> s1 >> s2; mp(s1); res[mp(s1)][mp(s2)]=1; adj[mp(s1)].push_back(mp(s2)); adj[mp(s2)].push_back(mp(s1)); res[s][mp(s1)]=1; adj[s].push_back(mp(s1)); } cin >> m; for(int i=0 ; i<m ; i++){ cin >> s1 >> s2; res[mp(s1)][mp(s2)]=INF; adj[mp(s1)].push_back(mp(s2)); adj[mp(s2)].push_back(mp(s1)); } while(1){ f=0; memset(par,-1,sizeof par); dist.assign(MAX,INF); dist[s]=0; queue<int> q; q.push(s); while(!q.empty()){ int u=q.front(); q.pop(); if(u==t) break; for(int i=0 ; i<adj[u].size(); i++){ int v=adj[u][i]; if(res[u][v]>0 && dist[v]==INF){ dist[v]=dist[u]+1; q.push(v); par[v]=u; } } } agument(t,INF); if(f==0) break; mf+=f; } cout << devs-mf << endl; if(tc) cout << endl; } return 0;}
int main(){ int T, R, N, x, y; cin >> T; for(int Ti = 1; Ti <= T; Ti++){ cin >> N >> R; nodes.assign(N, ii()); EdgeList.clear(); for(int i = 0; i < N; i++){ cin >> x >> y; nodes[i].first = x; nodes[i].second = y; for(int j = i-1; j >= 0; j--){ EdgeList.push_back(make_pair(dist(i,j), ii(j,i))); } } sort(EdgeList.begin(),EdgeList.end()); double roads = 0, railroads = 0; int states = 1; bool fRoad = true; UnionFind UF(N); for(int i = 0; i < (int)EdgeList.size(); i++){ pair<double, ii> front = EdgeList[i]; if(!UF.isSameSet(front.second.first, front.second.second)){ if(front.first > R){ railroads += front.first; if(fRoad){ states = UF.count(); fRoad = false; } } else{ roads+= front.first; } UF.unionSet(front.second.first, front.second.second); if(UF.count() == 1) break; } } cout << "Case #" << Ti << ": " << states << ' ' << (int)(roads+.5) << ' ' << (int)(railroads+.5)<<'\n'; } }
void getGraph() { data.assign(2, pii(-1,-1)); for(int i=0;i < n;i++) for(int j=0;j<m;j++) { if(grip[i][j]=='S') { data[0]=pii(i,j); graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] ='.'; } else if(grip[i][j]=='G') { data[1]=pii(i,j); graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] ='.'; } else { graph[i][j][0]=graph[i][j][1]=graph[i][j][2]=graph[i][j][3] =grip[i][j]; if(getDir(graph[i][j][0])>=0) { data.push_back(pii(i,j)); graph[i][j][1] = fun(graph[i][j][0]); graph[i][j][2] = fun(graph[i][j][1]); graph[i][j][3] = fun(graph[i][j][2]); } } } for(int i=2;i<data.size();i++) { int dire=getDir(graph[data[i].first][data[i].second][0]); for(int k=0;k<4;k++) { int dir=(dire+k)%4; int ni=data[i].first+dy[dir]; int nj=data[i].second+dx[dir]; while(validDisp(ni,nj,k) ) { graph[ni][nj][k]='x'; ni+=dy[dir]; nj+=dx[dir]; } } } }
int main() { //READ("in.txt"); //WRITE("out.txt"); int n; while(~getI(n) && n) { getchar(); AdjList.assign(n+4,vi()); string st; while(getline(cin,st)) { stringstream ss(st); int x,src; ss >> src; if(src==0) break; while(ss>>x) { if(x==0) break; AdjList[src].PB(x); } } int t; getI(t); for(int i=1;i<=t;i++) { int src; getI(src); visited.assign(n+4,0); int res = n-BFS(src); printf("%d",res); for(int i=1;i<=n;i++) { if(visited[i]==0) printf(" %d",i); } printf("\n"); } } return 0; }
int main() { // READ("in.txt"); //WRITE("out.txt"); while(~getI(e)) { AdjList.assign(e+4,vi()); for(int i=0;i<e;i++) { int n; getI(n); for(int j=0;j<n;j++) { int x; getI(x); AdjList[i].PB(x); } } int t; getI(t); for(int i=0;i<t;i++) { int n; getI(n); if(AdjList[n].size()==0) printf("0\n"); else { int dis = BFS(n); int cnt=0; for(int i=0;i<e;i++) { if(dist[i]==dis) cnt++; } printf("%d %d\n",cnt,dis); } } } return 0; }
int main() { // READ("in.txt"); // WRITE("out.txt"); int t; getI(t); for(int ci=1;ci<=t;ci++) { int v,e; getII(v,e); AdjList.assign(v+4,vi()); for(int i=0;i<e;i++) { int x,y; getII(x,y); AdjList[x].PB(y); AdjList[y].PB(x); } color.assign(v+4,-1); int cnt=0; for(int i=0;i<v;i++) { if(color[i]==-1) { int k = BFS(i); //debug(i,k) if(k==-1) { cnt = -1; break; } cnt += k; } } printf("%d\n",cnt); } return 0; }
int main() { // READ("in.txt"); //WRITE("out.txt"); int t; getI(t); for(int ci=1;ci<=t;ci++) { int n,m; getII(n,m); CLR(result); AdjList.assign(n+4,vi()); for(int i=0;i<m;i++) { int x,y; getII(x,y); AdjList[x].PB(y); AdjList[y].PB(x); } printf("Case %d: ",ci); if(n==1) { printf("0\n"); continue; } CLR(result); BFS(1); int cnt=0; for(int i=1;i<=n;i++) { if(result[i]==1) cnt++; } printf("%d\n",cnt); } return 0; }
void st_create(vii &t, const vi &A) { int n = A.size(); t.assign(n * 4, make_pair(-1, -1)); st_build(t, A, 1, 0, n - 1); }