#include<iostream> #include<vector> #include<queue> #include<algorithm> usingnamespace std; constint maxn = 105; int G[maxn][maxn]; int vis[maxn]; int n, m; voidadd_edge(int u, int v) { G[u][v] = 1; }
voiddfs(int u) { cout << u << " "; vector<int> vec; for (int i = 1; i <= n; i++) { if (G[u][i] && !vis[i]) { vec.push_back(i); } } sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { vis[vec[i]] = 1; dfs(vec[i]); } } voidbfs(int u) { queue<int> q; q.push(u);
while (!q.empty()) { int cur = q.front(); cout<<cur<<" "; q.pop(); vector<int> vec; for (int i = 1; i <= n; i++) { if (G[cur][i] && !vis[i]) { vec.push_back(i); } } sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { vis[vec[i]] = 1; q.push(vec[i]); } } } intmain() { cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; add_edge(u, v); } dfs(1); cout<<endl; memset(vis,0,sizeof(vis)); bfs(1); return0; }
#include<iostream> #include<algorithm> #include<vector> usingnamespace std; constint maxn = 1e5 + 5; int n, m; structEdge { int from, to; }; vector<int> G[maxn]; vector<Edge> edges; int vis[maxn]; voidadd_edge(int u, int v) { edges.push_back({u, v}); G[u].push_back(edges.size() - 1); } voidinput() { cin >> n >> m; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; add_edge(a, b); } } voiddfs(int u) { vis[u] = 1; // 我们G[u]存的是所有起点为u的边的编号,遍历拿到每一个编号edge_no for (auto edge_no : G[u]) { // 通过边的编号我们拿到这条边的终点,即u的相邻点 int to = edges[edge_no].to; if (!vis[to]) { dfs(to); } } } intmain() { return0; }
booltopo() { queue<int> q; for (int i = 1; i <= n; i++) if (!deg[i]) q.push(i);
int cnt = 0; while (!q.empty()) { auto u = q.front(); q.pop(); cnt++; for (auto v : G[u]) { deg[v]--; if (!deg[v]) q.push(v); } } return cnt == node_cnt; }
#include<iostream> #include<queue> #include<vector> #include<cstring> usingnamespace std; constint maxn = 1e5; structEdge { int from; int to; }; int n, m; vector<Edge> edges; vector<int> G[maxn]; int deg[maxn]; int Deg[maxn]; int vis[maxn];
voidadd_edge(int u, int v) { edges.push_back({u, v}); Deg[v]++; G[u].push_back(edges.size() - 1); }
vector<int> ans; inttopo_sort() { memcpy(deg,Deg,sizeof(Deg)); queue<pair<int, int>> q; for (int i = 1; i <= n; i++) if (deg[i] == 0) q.push({i, 1}); int res = 1; while (!q.empty()) { int cur = q.front().first; int depth = q.front().second; q.pop(); ans.push_back(cur);
for (auto edge_no : G[cur]) { Edge e = edges[edge_no]; deg[e.to]--; if (deg[e.to] == 0) { q.push({e.to, depth + 1}); res = max(res, depth + 1); } } } if (ans.size() < n) return-1; if (res < n) return0; return1; } chargetch() { char ch = getchar(); while (isspace(ch)) ch = getchar(); return ch; } intmain() { cin >> n >> m; for (int i = 1; i <= m; i++) { char u = getch(); getchar(); char v = getch(); add_edge(u - 'A' + 1, v - 'A' + 1); ans.clear(); int res = topo_sort(); if (res == 1) { cout << "Sorted sequence determined after" << " " << i << " relations: "; for (auto a : ans) { cout << (char)(a + 'A' - 1); } cout << "."; cout << endl; return0; } elseif (res == -1) { cout << "Inconsistency found after " << i << " relations.\n"; return0; } } cout << "Sorted sequence cannot be determined.\n"; return0; }
#include<iostream> #include<algorithm> #include<queue> #include<vector> #include<cstring> usingnamespace std; constint maxn = 1e3 + 5; structEdge { int from, to; }; vector<int> G[maxn]; vector<Edge> edges; int n, m; int vis[maxn]; int G2[maxn][maxn]; vector<int> Trans[maxn]; int deg[maxn]; voidadd_edge(int u, int v) { if (G2[u][v]) return; G2[u][v] = 1; deg[v]++; edges.push_back({u, v}); G[u].push_back(edges.size() - 1); } inttopo() { memset(vis, 0, sizeof(vis)); // 记录层数 queue<pair<int, int>> q; for (int i = 1; i <= n; i++) if (!deg[i]) q.push({i, 1}); int res = 1; while (!q.empty()) { int cur = q.front().first; int dep = q.front().second; q.pop(); for (auto e_no : G[cur]) { int v = edges[e_no].to; deg[v]--; if (!deg[v]) { q.push({v, dep + 1}); res = max(res, dep + 1); } } } return res; }
voidinput() { cin >> n >> m; for (int i = 1; i <= m; i++) { int cnt; cin >> cnt; memset(vis, 0, sizeof(vis)); for (int j = 1; j <= cnt; j++) { int cur; cin >> cur; Trans[i].push_back(cur); vis[cur] = 1; } for (int j = Trans[i].front(); j <= Trans[i].back(); j++) { if (vis[j]) continue; // 没有走到的车站,优先级肯定比走到的车站优先级低,我们依次连接已经走到的车站。 for (auto v : Trans[i]) add_edge(j, v); } } } intmain() { input(); cout << topo() << endl; return0; }
#include<iostream> #include<vector> #include<algorithm> #include<cstring> constint maxn = 1e5 + 5; usingnamespace std; #define int long long namespace Kruskal { int n, m; structEdge { int from, to, w; booloperator<(const Edge &e) const { return w < e.w; } };
vector<Edge> edges;
int par[maxn];
voidadd_edge(int u, int v, int d) { edges.push_back({u, v, d}); } voidinit() { for (int i = 1; i <= n; i++) par[i] = i; } intfind(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
intkruskal() { int res = 0; sort(edges.begin(), edges.end()); for (auto e : edges) { int a = find(e.from), b = find(e.to); if (a == b) continue; par[a] = b; res += e.w; } return res; } }
signedmain() { cin >> Kruskal::n >> Kruskal::m; Kruskal::init(); for (int i = 1; i <= Kruskal::m; i++) { int a, b, c; cin >> a >> b >> c; Kruskal::add_edge(a, b, c); } cout << Kruskal::kruskal(); return0; }
#include<iostream> #include<vector> #include<algorithm> #include<cstring> #define int long long constint maxn = 2e5 + 5; usingnamespace std; int n, m; int a[maxn]; int b[maxn]; vector<int> A[maxn];
namespace Kruskal { int n, m; structEdge { int from, to, w; booloperator<(const Edge &e) const { return w > e.w; } };
vector<Edge> edges;
int par[maxn];
voidadd_edge(int u, int v, int d) { edges.push_back({u, v, d}); } voidinit() { for (int i = 1; i <= n; i++) par[i] = i; } intfind(int x) { return x == par[x] ? x : par[x] = find(par[x]); }
intkruskal() { int res = 0; sort(edges.begin(), edges.end()); for (auto e : edges) { int a = find(e.from), b = find(e.to); if (a == b) continue; par[a] = b; res += e.w; } return res; } } voidinput() { cin >> m >> n; for (int i = 1; i <= m; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= m; i++) { int cnt; cin >> cnt; for (int j = 1; j <= cnt; j++) { int tmp; cin >> tmp; A[i].push_back(tmp); } } } signedmain() { input(); Kruskal::n = n + m + 1; Kruskal::init(); int ans = 0; for (int i = 1; i <= m; i++) for (auto j : A[i]) { ans += a[i] + b[j]; Kruskal::add_edge(j, i + n, a[i] + b[j]); }
cout << ans - Kruskal::kruskal() << endl; return0; }
usingnamespace std; constint maxn = 1e5 + 5; int n, m; structEdge { int from, to, dist; }; vector<int> G[maxn]; vector<Edge> edges; int dist[maxn]; int vis[maxn]; voidadd_edge(int u, int v, int c) { edges.push_back({u, v, c}); G[u].push_back(edges.size() - 1); } voidspfa() { memset(dist, -0x3f, sizeof(dist)); queue<int> q; q.push(0); vis[0] = 1; dist[0] = 0; while (!q.empty()) { int cur = q.front(); q.pop(); vis[cur] = 0; for (auto edge_no : G[cur]) { auto e = edges[edge_no]; if (dist[e.to] < dist[cur] + e.dist) { dist[e.to] = dist[cur] + e.dist; if (!vis[e.to]) { q.push(e.to); vis[e.to] = 1; } } } } } voidinput() { cin >> n >> m; for (int i = 1; i <= m; i++) { int a, b, c; cin >> b >> a >> c; add_edge(b - 1, a, c); } for (int i = 1; i <= n; i++) { add_edge(i - 1, i, 0); add_edge(i, i - 1, -1); } } intmain() { input(); spfa(); cout << dist[n]; return0; }
#include<iostream> #include<algorithm> #include<vector> usingnamespace std; constint maxn = 1e5 + 5; int n, m, start, dest; structEdge { int from, to; int d; }; vector<int> G[maxn]; vector<Edge> edges; int vis[maxn];
voidadd_edge(int u, int v, int d) { edges.push_back({u, v, d}); G[u].push_back(edges.size() - 1); } voidinput() { cin >> n >> m >> start >> dest; for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; add_edge(a, b, c); add_edge(b, a, 0); } } intdfs(int u, int t, int flow) { if (u == t) return flow; vis[u] = true; int res = 0; for (auto e_no : G[u]) { auto &e = edges[e_no]; if (vis[e.to] || e.d <= 0) continue; if ((res = dfs(e.to, t, min(flow, e.d))) > 0) { edges[e_no].d -= res; edges[e_no ^ 1].d += res; return res; } } return0; }
intmax_flow(int s, int t) { int res = 0; while (true) { memset(vis,0,sizeof(vis)); int tmp = dfs(s, t, 0x3f3f3f3f); if (tmp) res += tmp; else break; } return res; } intmain() { input(); cout << max_flow(start, dest); return0; }
#include<iostream> #include<algorithm> #include<vector> #include<cstring> #include<queue> #define int long long usingnamespace std; constint maxn = 1e5 + 5; int n, m, start, dest; structEdge { int from, to; int flow; }; vector<int> G[maxn]; vector<Edge> edges; int vis[maxn]; int A[maxn]; int last[maxn];
voidadd_edge(int u, int v, int d) { edges.push_back({u, v, d}); G[u].push_back(edges.size() - 1); } voidinput() { cin >> n >> m >> start >> dest; for (int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; add_edge(a, b, c); add_edge(b, a, 0); } }
intmax_flow(int s, int t) { int res = 0; while (true) { queue<int> q; q.push(s); memset(A, 0, sizeof(A)); memset(last, -1, sizeof(last)); A[s] = 0x3f3f3f3f;
while (!q.empty()) { int cur = q.front(); q.pop(); for (auto e_no : G[cur]) { auto e = edges[e_no]; if (last[e.to] == -1 && e.flow) { last[e.to] = e_no; A[e.to] = min(e.flow, A[e.from]); q.push(e.to); } } } // 到汇点无流量,即可退出 if (!A[t]) return res; res += A[t]; // 找到的路径榨流量 for (int cur = t; cur != s; cur = edges[last[cur]].from) { edges[last[cur]].flow -= A[t]; edges[last[cur] ^ 1].flow += A[t]; } } } signedmain() { input(); cout << max_flow(start, dest); return0; }
#include<iostream> #include<vector> #include<queue> #include<cstring> usingnamespace std; namespace Dinic { int s, t; constint maxn = 20005; constint maxm = 1000100; int head[maxn], cur[maxn], depth[maxn]; int cnt = 1, n = 0;
structEdge { int to, next, cap; }; Edge edges[maxm];
void _add_edge(int u, int v, int c) { edges[++cnt].to = v; edges[cnt].cap = c; edges[cnt].next = head[u]; head[u] = cnt; } voidadd(int u, int v, int c) { // printf("{%d,%d,%d}\n",u,v,c); _add_edge(u, v, c); _add_edge(v, u, 0); } boolbfs() { memset(depth, -1, sizeof(depth)); depth[s] = 0;
for (int i = 0; i <= n; i++) cur[i] = head[i]; cur[t]= head[t]; cur[s]= head[s]; queue<int> q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = edges[i].next) { int to = edges[i].to; if (depth[to] == -1 && edges[i].cap > 0) depth[to] = depth[u] + 1, q.push(to); } } return depth[t] != -1; } intdfs(int from, int flow) { if (from == t) return flow;
int res = flow;
for (int i = cur[from]; i && res; i = edges[i].next) { cur[from] = i;
int to = edges[i].to; int cap = edges[i].cap; if (cap > 0 && depth[to] == depth[from] + 1) { int c = dfs(to, min(cap, res)); res -= c; edges[i].cap -= c; edges[i ^ 1].cap += c; } } return flow - res; } longlongdinic(int _s,int_t,int _n) { s=_s; t=_t; n=_n; longlong ret = 0; while (bfs()) { ret += dfs(s, 0x3f3f3f3f); } return ret; } } // namespace Dinic intmain(){ int n,m,s,t; cin>>n>>m>>s>>t; for(int i=1;i<=m;i++) { int a,b,c; cin>>a>>b>>c; Dinic::add(a,b,c); } cout<<Dinic::dinic(s,t,n); return0; }