版本比较
12 14 无向图上环计数问题学习笔记
正文
增加 50 行删除 50 行
29 行未改动30313233343435363738394040414243444546474848495050515253546 行未改动61626364656566676869707171727374757677787980818182838485868787888990912 行未改动949596979898991001001011021031042 行未改动1071081091101111111121131141151161161171181191209 行未改动1301311321331341341351361371371381391401411421431441451451461471481491501511521521531541551562 行未改动15916016116216316316416516616716816917017117217217317417417517617717821 行未改动2002012022032042042052062072086 行未改动21521621721821921922022122122222322422511 行未改动2372382392402412412422432442452462472482482492502512522532542552552562572582598 行未改动2682692702712722722732742752762762772782792802812822822832842852862872882882892902912922932942952952962972982993003013013023033043043053063073082 行未改动31131231331431531531631731831931932032132232332432532532632732832912 行未改动3423433443453463463473483493503 行未改动3543553563573583583593603613623633643643653663673683693693703713723723733743753767 行未改动3843853863873883883893903913923933943953953963973983993994004014024034044054064074084084094104114124134144144154164174184194204204214224234246 行未改动43143243343443543543643743843944044144244244344444544644744744844944945045145229 行未改动 cin >> u >> v; edge[i] = m_p(u, v); d[u]++, d[v]++; }
for (int i = 1; i <= m; i++){ if (d[edge[i].fi] > d[edge[i].se] || (d[edge[i].fi] == d[edge[i].se] && edge[i].fi > edge[i].se)) swap(edge[i].fi, edge[i].se); g[edge[i].fi].push_back(edge[i].se); }
for (int i = 1; i <= n; i++){ for (auto j : g[i]) vis[j] = i; for (auto j : g[i]) for (auto k : g[j]) ans += (vis[k] == i); }
cout << ans << endl;
return 0;}```
6 行未改动#include <vector>#define fi first#define se secondusing namespace std;
typedef long long ll;const int N = 1e5 + 10;ll n, m, ans = 0, a[N], d[N], vis[N];pair<int, int> edge[N*3];vector<int> g[N];
int main(){ cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1, u, v; i <= m; i++){ cin >> u >> v; edge[i] = make_pair(u, v); d[u]++, d[v]++; }
for (int i = 1; i <= m; i++){ if (d[edge[i].fi] > d[edge[i].se]) swap(edge[i].fi, edge[i].se); g[edge[i].fi].push_back(edge[i].se); }
for (int i = 1; i <= n; i++){ for (auto j : g[i]) vis[j] = i; for (auto j : g[i])2 行未改动 continue; ans += max({a[i], a[j], a[k]}); } }
cout << ans << endl;
return 0;}```
2 行未改动#include <cstdio>#include <iostream>#include <vector>using namespace std;
const int N = 1e5 + 10, M = 2e5 + 10;int n, m, ans = 0, d[N], cnt[N];int tot1 = 0, head[N], to[M*2], nxt[M*2];int tot2 = 0, head_[N], to_[N*2], nxt_[N*2];
void Faster_Init(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);9 行未改动 to_[++tot2] = b; nxt_[tot2] = head_[a]; head_[a] = tot2;}
int main(){ Faster_Init();
cin >> n >> m; for (int i = 1, u, v; i <= m; i++){ cin >> u >> v; add(u, v); add(v, u); d[u]++, d[v]++; }
for (int u = 1; u <= n; u++) for (int i = head[u]; i; i = nxt[i]){ int v = to[i]; if (d[u] > d[v] || (d[u] == d[v] && u < v)) add_(u, v); }
for (int a = 1; a <= n; a++){ for (int i = head_[a]; i; i = nxt_[i]){ int b = to_[i]; for (int j = head[b]; j; j = nxt[j]){2 行未改动 continue; ans += cnt[c]++; } }
for (int i = head_[a]; i; i = nxt_[i]){ int b = to_[i]; for (int j = head[b]; j; j = nxt[j]){ int c = to[j]; cnt[c] = 0; } } }
cout << ans << endl;
return 0;}```
21 行未改动 cin >> u >> v; g[u].push_back(v); g[v].push_back(u); }
for (int i = 1; i <= n; i++) dp[1<<(i-1)][i] = 1; for (int s = 0; s < (1 << n); s++) for (int i = 1; i <= n; i++)6 行未改动 ans += dp[s][i]; }else dp[s|(1<<(j-1))][j] += dp[s][i]; } }
cout << (ans - m) / 2 << endl;
return 0;}```
11 行未改动 int head[N], tot, to[N*2], pre[N*2], nxt[N*2]; Graph(){ tot = 0; }
void add(int a, int b){ to[++tot] = b; nxt[tot] = head[a]; pre[head[a]] = tot; head[a] = tot; }
void inc(int a, int i){ nxt[pre[i]] = nxt[i]; pre[nxt[i]] = pre[i]; if (head[a] == i) head[a] = nxt[i]; }
void output(){ for (int i = 1; i <= n; i++){ for (int j = head[i]; j; j = nxt[j]) cout << to[j] << " " ;8 行未改动 cin >> u >> v; G.add(u, v), G.add(v, u); G_.add(u, v), G_.add(v, u); }
//G.output(); //cout << endl; //G_.output();
q = queue<int>(); dis[s] = 1, q.push(s); while (!q.empty()){ int u = q.front(); q.pop();
for (int i = G.head[u]; i; i = G.nxt[i]){ if (!dis[G.to[i]]) dis[G.to[i]] = dis[u] + 1, q.push(G.to[i]); } }
for (int i = 1; i <= n; i++) if (dis[i]){ --dis[i]; ans[i] = min(a*dis[i], b*(dis[i]>>1) + a*(dis[i]&1)); dis[i] = 0; }
q = queue<int>(); dis[s] = 1, q.push(s); while (!q.empty()){ int u = q.front(); q.pop();
for (int i = G.head[u]; i; i = G.nxt[i]) vis[G.to[i]] = 1;
for (int i = G.head[u]; i; i = G.nxt[i]){ int v = G.to[i]; for (int j = G_.head[v]; j; j = G_.nxt[j]){ int w = G_.to[j];2 行未改动 dis[w] = dis[u] + 1, q.push(w); G_.inc(v, j); } }
for (int i = G.head[u]; i; i = G.nxt[i]) vis[G.to[i]] = 0; }
for (int i = 1; i <= n; i++){ if (dis[i]) ans[i] = min(ans[i], b*(dis[i]-1)); cout << ans[i] << endl; }
return 0;}```
12 行未改动vector<int> G[N], G_[N];
ll GetAns1(){ ll sum = 0;
for (int u = 1; u <= n; u++) for (auto v : G_[u]){ int x = min(u, v), y = max(u, v); sum += A * (x - 1) * (n - x - 1);3 行未改动 sum += A * (x - 1) * (x - 2) / 2; sum += B * (y + x - 2) * (y - x - 1) / 2; sum += C * (n + y - 1) * (n - y) / 2; }
return sum;}
ll GetAns2(){ ll sum = 0;
for (int u = 1; u <= n; u++){ G[u].push_back(u); sort(G[u].begin(), G[u].end()); int tn = G[u].size() - 1;
for (int i = 0; i <= tn; i++){ int v = G[u][i];
if (v != u){ if (v < u){ sum += A * (v-1) * (tn-i-1); sum += B * (v-1) * i;7 行未改动 sum += C * i * (i-1) / 2 * (u-1); } } }
return sum;}
int vis[N];ll GetAns3(){ ll sum = 0;
for (int u = 1; u <= n; u++){ for (auto v : G_[u]) vis[v] = u;
for (auto v : G_[u]) for (auto w : G_[v]) if (vis[w] == u){ int t[] = {u, v, w}; sort(t, t + 3); sum += A*(t[0]-1) + B*(t[1]-1) + C*(t[2]-1); } }
return sum;}
ll GetAll(){ ll sum = 0;
for (int i = 1; i <= n; i++){ sum += A * (n-i) * (n-i-1) / 2 * (i-1); sum += B * (i-1) * (n-i) * (i-1); sum += C * (i-1) * (i-2) / 2 * (i-1); }
return sum;}
int main(){6 行未改动 G[fr[i]].push_back(to[i]); G[to[i]].push_back(fr[i]); ++d[fr[i]], ++d[to[i]]; }
for (int i = 1; i <= m; i++){ int u = fr[i], v = to[i]; if (d[u] > d[v] || (d[u] == d[v] && u > v)) swap(u, v); G_[u].push_back(v); }
ll total = 0, sum = 0; total = GetAll(); sum = GetAns1() - GetAns2() + GetAns3(); //cout << GetAll() << " " << GetAns1() << " " << GetAns2() << " " << GetAns3() << endl;
cout << total - sum << endl;
return 0;}```