#include<bits/stdc++.h> usingnamespace std; int n, m, k; vector<vector<int>> G, temp;
voiddfs(int a){ temp[a][a] = INT_MAX; for (int i = 1; i <= n; i++) { if (temp[i][i] == 1 && temp[a][i] != INT_MAX) { dfs(i); } } return; }
intmain(){ cin >> n >> m >> k; G.resize(n + 1, vector<int>(n + 1, INT_MAX)); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; G[a][b] = 1; G[b][a] = 1; } for (int i = 0; i < k; i++) { int cur; cin >> cur; temp = G; for (int j = 1; j <= n; j++) { temp[j][j] = 1; if (temp[j][cur] != INT_MAX) { temp[j][cur] = INT_MAX; temp[cur][j] = INT_MAX; } } int ans = 0; for (int i = 1; i <=n; i++) { if (temp[i][i] == 1) { dfs(i); ans++; } } cout << ans - 1; if (i < k - 1) cout << endl; } return0; }
#include<bits/stdc++.h> usingnamespace std; int n, d = 0, maxD = 0; vector<int> vis, ans; vector<vector<int>> G;
voiddfs(int a, int depth){ vis[a] = true; d = max(d, depth); for (int i = 0; i < G[a].size(); i++) { if (vis[G[a][i]] == false) { dfs(G[a][i], depth + 1); } } return; }
intmain(){ cin >> n; G.resize(n + 1); vis.resize(n + 1, false); for (int i = 0; i < n-1; i++) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } int cnt = 0; for (int i = 1; i <= n; i++) { if (vis[i] == false) { dfs(i, 1); cnt++; } } if (cnt > 1) cout << "Error: " << cnt << " components"; else { for (int i = 1; i <= n; i++) { // 注意这里不能用vis.resize(n + 1, false) // 大小已经为(n + 1),不会进行重置,无法改变vis的值 fill(vis.begin(), vis.end(), false); d = 0; dfs(i, 1); if (d >= maxD) { if (d > maxD) { ans.clear(); maxD = d; } ans.push_back(i); } } sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) { cout << ans[i]; if (i != ans.size() - 1) cout << endl; } } return0; }
#include<bits/stdc++.h> usingnamespace std; int n, l; vector<int> vis; vector<vector<int>> G;
voiddfs(int a, int depth, int &tot){ vis[a] = true; if (depth > l) return; for (int i = 0; i < G[a].size(); i++) { int v = G[a][i]; // cout << v << endl; if (vis[v] == false) { tot++; dfs(v, depth + 1, tot); } } }
voidbfs(int a, int &tot){ queue<int> q; q.push(a); vis[a] = true; int layer = -1; while (!q.empty()) { int num = q.size(); layer++; if (layer > l) return; for(int i = 0; i < num; i++) { int cur = q.front(); q.pop(); if (layer) tot++; // layer=0时访问的是发博的用户本身,不计入 for (int j = 0; j < G[cur].size(); j++) if (vis[G[cur][j]] == false) { vis[G[cur][j]] = true; q.push(G[cur][j]); } } } }
intmain(){ cin >> n >> l; int num, k; G.resize(n + 1); vis.resize(n + 1, false); for (int i = 1; i <= n; i++) { cin >> num; for (int j = 1; j <= num; j++) { cin >> k; G[k].push_back(i); } } cin >> num; for (int i = 0; i < num; i++) { fill(vis.begin(), vis.end(), false); cin >> k; int tot = 0; bfs(k, tot); cout << tot; if (i != num - 1) cout << endl; } return0; }
// BellmanFord + DFS structNode { int v; int dis; Node(int _v, int _dis): v(_v), dis(_dis) {} }; int n, m, c1, c2; vector<int> team, d, t, num; vector<set<int>> pre; vector<vector<Node>> Adj;
boolBellman_Ford(int s){ d[s] = 0; for (int i = 0; i < n - 1; i++) { for (int u = 0; u < n; u++) { for (int k = 0; k < Adj[u].size(); k++) { if (d[u] == INT_MAX) continue; int v = Adj[u][k].v; int dtc = Adj[u][k].dis; if (d[u] + dtc < d[v]) { d[v] = d[u] + dtc; pre[v].clear(); pre[v].insert(u); } elseif (d[u] + dtc == d[v]) { pre[v].insert(u); } } } } for (int u = 0; u < n; u++) { for (int k = 0; k < Adj[u].size(); k++) { int v = Adj[u][k].v; int dtc = Adj[u][k].dis; if (d[u] + dtc < d[v]) returnfalse; } } returntrue; }
int maxSum = INT_MIN, cnt = 0; vector<int> tempPath, ansPath; voiddfs(int v){ if (v == c1) { tempPath.push_back(v); int curSum = 0; for (int i = tempPath.size() - 1; i >= 0; i--) { curSum += team[tempPath[i]]; } if (curSum > maxSum) { maxSum = curSum; ansPath = tempPath; } cnt++; tempPath.pop_back(); return; } tempPath.push_back(v); set<int>::iterator it; for (it = pre[v].begin(); it != pre[v].end(); it++) { dfs(*it); } tempPath.pop_back(); return; }
intmain(){ cin >> n >> m >> c1 >> c2; // t.resize(n, 0); // num.resize(n, 0); d.resize(n, INT_MAX); pre.resize(n); Adj.resize(n); for (int i = 0; i < n; i++) { int cur; cin >> cur; team.push_back(cur); } for (int i = 0; i < m; i++) { int u, v, l; cin >> u >> v >> l; Adj[u].push_back(Node(v, l)); Adj[v].push_back(Node(u, l)); } bool flag = Bellman_Ford(c1); // bool flag = Single_Bellman_Ford(c1); if (!flag) { cout << "No Solution"; return0; } // cout << num[c2] << " " << t[c2]; dfs(c2); cout << cnt << " " << maxSum; return0; }
structNode { int v; int w; Node(int _v, int _w): v(_v), w(_w) {} }; constint MAX = 0x3fffffff; vector<bool> inq; vector<int> team, d, innum; vector<set<int>> pre; vector<vector<Node>> Adj; int n, m, c1, c2;
int cnt = 0, maxSum = INT_MIN; vector<int> tempPath, ansPath; voiddfs(int v){ if (v == c1) { tempPath.push_back(v); int curSum = 0; for (int i = tempPath.size() - 1; i >= 0; i--) curSum += team[tempPath[i]]; if (curSum > maxSum) { maxSum = curSum; ansPath = tempPath; } cnt++; tempPath.pop_back(); return; } tempPath.push_back(v); for (auto p: pre[v]) dfs(p); tempPath.pop_back(); return; }
intmain(){ cin >> n >> m >> c1 >> c2; pre.resize(n); team.resize(n); Adj.resize(n); for (int i = 0; i < n; i++) cin >> team[i]; for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; Adj[u].push_back(Node(v, w)); Adj[v].push_back(Node(u, w)); } bool flag = SPFA(c1); if (!flag) cout << "No Solution!"; else { dfs(c2); cout << cnt << " " << maxSum; } return0; }
int ansCost = INT_MAX; vector<pair<int, int>> tempPath, ansPath; voiddfs(pair<int, int> v){ tempPath.push_back(v); if (v.first == st) { int cost = 0; // 注意这里是从起始点(最后一个元素)到倒二个元素,计算边权 for (int i = tempPath.size() - 1; i > 0; i--) { cost += tempPath[i].second; } if (cost < ansCost) { ansCost = cost; ansPath = tempPath; } tempPath.pop_back(); return; } for (auto p: pre[v.first]) dfs(p); tempPath.pop_back(); return; }
intmain(){ cin >> n >> m >> st >> ed; pre.resize(n); Adj.resize(n); for (int i = 0; i < m; i++) { int u, v, w, c; cin >> u >> v >> w >> c; Adj[u].push_back(Node(v, w, c)); Adj[v].push_back(Node(u, w, c)); } SPFA(st); dfs(make_pair(ed, 0)); for (int i = ansPath.size() - 1; i >= 0; i--) cout << ansPath[i].first << " "; printf("%d %d", d[ed], ansCost); return0; }