最短路(堆优化)
#include <bits/stdc++.h> using namespace std; const int inf = 1000000000; void dij() { int n, m, s; cin >> n >> m >> s; vector<vector<pair<int, int> > > adj(n); for (int i = 0; i < m; ++i) { int x, y, w; cin >> x >> y >> w; adj[x].emplace_back(y, w); } vector<int> d(n, inf); d[s] = 0; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; q.emplace(0, s); while (!q.empty()) { auto o = q.top(); int x = o.second; int cur_dis = o.first; q.pop(); if (cur_dis > d[x]) { continue; } for (auto o : adj[x]) { int y = o.first; int w = o.second; if (d[y] > d[x] + w) { d[y] = d[x] + w; q.push(make_pair(d[y], y)); } } } for (int i = 0; i < n; ++i) { if (d[i] == inf) { cout << "INF" << ' '; } else { cout << d[i] << ' '; } } } int main() { dij(); return 0; }
最短路(朴素)
[luoguP3371](https://www.luogu.com.cn/problem/P3371)
#include <bits/stdc++.h> using namespace std; const int inf = numeric_limits<int> :: max(); void dij() { int n, m, s; cin >> n >> m >> s; s --; vector<vector<pair<int, int> > > adj(n); for (int i = 0; i < m; ++i) { int x, y, w; cin >> x >> y >> w; x --; y --; adj[x].emplace_back(y, w); } vector<int> d(n, inf); vector<bool> vis(n); d[s] = 0; for (int i = 0; i < n; ++i) { int x = -1; int Min = inf; for (int j = 0; j < n; ++j) { if (!vis[j] && Min > d[j]) { Min = d[j]; x = j; } } if (x == -1) { break; } vis[x] = true; for (auto o : adj[x]) { int y = o.first; int w = o.second; d[y] = min(d[y], d[x] + w); } } for (int i = 0; i < n; ++i) { cout << d[i] << " "[i == n - 1]; } } int main() { dij(); return 0; }
A
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; vector<int> a; while (q --) { int type; cin >> type; if (type == 0) { int x; cin >> x; a.emplace_back(x); } if (type == 1) { int p; cin >> p; cout << a[p] << ' '; } if (type == 2) { a.pop_back(); } } return 0; }
B
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vector<vector<int> > a(n); while (q --) { int type; cin >> type; if (type == 0) { int t, x; cin >> t >> x; a[t].emplace_back(x); } if (type == 1) { int t; cin >> t; if (a[t].empty()) { cout << ' '; } else { for (int i = 0; i < a[t].size(); ++i) { cout << a[t][i] << " "[i + 1 == a[t].size()]; } } } if (type == 2) { int t; cin >> t; a[t].clear(); } } return 0; }
C
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; vector<pair<string, int> > a(n); for (int i = 0; i < n; ++i) { cin >> a[i].first >> a[i].second; } queue<pair<string, int> > que; for (int i = 0; i < n; ++i) { que.emplace(a[i]); } int tot = 0; while (!que.empty()) { auto tmp = que.front(); que.pop(); if (tmp.second > q) { tot += q; tmp.second -= q; que.emplace(tmp); } else { tot += tmp.second; cout << tmp.first << ' ' << tot << ' '; } } return 0; }
D
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); priority_queue<int> q; while (true) { string opt; cin >> opt; if (opt == "insert") { int x; cin >> x; q.emplace(x); } if (opt == "extract") { cout << q.top() << ' '; q.pop(); } if (opt == "end") { break; } } return 0; }
E
#include <bits/stdc++.h> using namespace std; const int inf = 1000000000; void dij() { int n, m, s; cin >> n >> m >> s; vector<vector<pair<int, int> > > adj(n); for (int i = 0; i < m; ++i) { int x, y, w; cin >> x >> y >> w; adj[x].emplace_back(y, w); } vector<int> d(n, inf); d[s] = 0; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; q.emplace(0, s); while (!q.empty()) { auto o = q.top(); int x = o.second; int cur_dis = o.first; q.pop(); if (cur_dis > d[x]) { continue; } for (auto o : adj[x]) { int y = o.first; int w = o.second; if (d[y] > d[x] + w) { d[y] = d[x] + w; q.push(make_pair(d[y], y)); } } } for (int i = 0; i < n; ++i) { if (d[i] == inf) { cout << "INF" << ' '; } else { cout << d[i] << ' '; } } } int main() { dij(); return 0; }
F
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<vector<pair<int, int> > > adj(n); for (int i = 0; i < n - 1; ++i) { int x, y, w; cin >> x >> y >> w; adj[x].emplace_back(y, w); adj[y].emplace_back(x, w); } int ans = 0; auto dfs = [&] (auto &&f, int x, int par) -> int { int Max = 0; for (auto o : adj[x]) { int y = o.first; int w = o.second; if (y != par) { int v = f(f, y, x); ans = max(ans, v + w + Max); Max = max(Max, v + w); } } return Max; }; dfs(dfs, 0, -1); cout << ans << ' '; return 0; }
G
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 100010; ll sum[3]; ll ans; int n; vector<int> G[N]; void dfs(int u, int last, int dep) { if(last) --ans; ans += sum[(dep & 1) ^ 1]; ++sum[dep & 1]; for(int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if(v == last) continue; dfs(v, u, dep + 1); } } int main() { scanf("%d", &n); for(int i = 1; i < n; ++i) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } dfs(1, 0, 0); printf("%lld ", ans); return 0; }
H
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 5; vector<int> G[maxn]; int n; int vis[maxn]; char s[maxn]; void dfs(int u) { if(vis[u]) return; vis[u] = 1; for(int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; dfs(v); } } int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%s", s + 1); int len = strlen(s + 1); for(int j = 1; j <= len; ++j) { G[i].push_back(s[j] - 'a' + 1 + n); G[s[j] - 'a' + 1 + n].push_back(i); } } int ans = 0; for(int i = 1; i <= n; ++i) { if(!vis[i]) { ++ans; dfs(i); } } printf("%d ", ans); return 0; }
I
#include <bits/stdc++.h> using namespace std; class dsu { private: vector<int> par; vector<int> sz; public: dsu(int N) : par(N), sz(N) { iota(par.begin(), par.end(), 0); } int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } void Union(int x, int y) { if(same(x, y)) { return; } x = find(x); y = find(y); par[x] = y; sz[y] += sz[x]; } void set(int x) { sz[x] = 1; } int get(int x) { return sz[x]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; dsu dsu(n + m); for (int i = 0; i < n; ++i) { dsu.set(i); } for (int i = 0; i < m; ++i) { int k; cin >> k; for (int j = 0; j < k; ++j) { int x; cin >> x; x --; dsu.Union(x, i + n); } } for (int i = 0; i < n; ++i) { cout << dsu.get(dsu.find(i)) << " "[i == n - 1]; } return 0; }
J
#include<bits/stdc++.h> using namespace std; const int N = 100010; int n; double ans; double dp[N]; vector<int> G[N]; void dfs(int u, int last, int dis) { if(G[u].size() == 1) ans += dp[u] * (double)dis; for(int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if(v == last) continue; dp[v] = dp[u] * 1.0 / (double)(G[u].size() - (last != 0)); dfs(v, u, dis + 1); } } int main() { scanf("%d", &n); dp[1] = 1.0; for(int i = 1; i < n; ++i) { int u, v; scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } dfs(1, 0, 0); // for(int i = 1; i <= n; ++i) printf("dp[%d]=%.6f ", i, dp[i]); printf("%.8f ", ans); return 0; }
K
#include<iostream> #include<cstring> #include<vector> #include<queue> #include<cstdio> #define mp make_pair using namespace std; typedef long long ll; typedef pair<int,ll> PII; const ll inf=100000000000009; vector<int>ans; vector<PII>graph[200010]; priority_queue<PII,vector<PII>,greater<PII> >q; int n,m,u,v,w; ll d[200010],used[200010],pre[200010]; void print(int x) { if(x==1) return; ans.push_back(x); print(pre[x]); } void dijiestra() { q.push(mp(0,1)); memset(used,0,sizeof(used)); for(int i=0;i<n+1;i++) d[i]=inf; d[1]=0; while(!q.empty()) { PII x=q.top(); q.pop(); int u=x.second; if(used[u]) continue; used[u]=1; for(int i=0;i<graph[u].size();i++) { x=graph[u][i]; int v=x.first; // cout<<"v="<<v<<endl; int val=x.second; if(d[v]>d[u]+val) { // cout<<"-------"<<endl; d[v]=d[u]+val; pre[v]=u; q.push(mp(d[v],v)); } } } } int main() { cin>>n>>m; for(int i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&w); graph[u].push_back(mp(v,w)); graph[v].push_back(mp(u,w)); } dijiestra(); // for(int i=1;i<=n;i++) // cout<<d[i]<<" "; // cout<<endl; if(d[n]==inf) { cout<<-1<<endl; return 0; } print(n); ans.push_back(1); for(int i=ans.size()-1;i>=0;i--) printf("%d ",ans[i]); return 0; }
L
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while(T--) { int n; cin >> n; string s; cin >> s; int big = 0; int small = 0; for(auto c : s) { if(c == '>') { big += 1; } if(c == '<') { small += 1; } } if(big == 0 || small == 0) { cout << n << ' '; } else { vector<vector<int> > G(n); for(int i = 0; i < n; ++i) { if(s[i] == '-') { G[i].emplace_back((i + 1) % n); G[(i + 1) % n].emplace_back(i); } } vector<bool> vis(n); int ans = 0; for(int i = 0; i < n; ++i) { if(!vis[i]) { int cnt = 0; vis[i] = true; queue<int> q; q.emplace(i); while(!q.empty()) { int u = q.front(); q.pop(); cnt += 1; for(int v : G[u]) { if(!vis[v]) { vis[v] = true; q.emplace(v); } } } if(cnt > 1) { ans += cnt; } } } cout << ans << ' '; } } return 0; }