20250718 总结

· · 个人记录

A.AtCoder abc352_e

题意

给定 M 组点,每组点有 k_i 个点,连接其中两点的代价为 c_i。问能否构建出最小生成树 G,若能则输出 G 中所有边的权重和

思路

const int N = 2e5 + 10;

int n, m, fa[N], sum, ans;

struct P{ int k, c; vector<int> b; }a[N];

bool cmp(const P &a, const P &b){ return a.c < b.c; }

int Find(int x){ return (!fa[x] ? x : fa[x] = Find(fa[x])); }

signed main(){ ios::sync_with_stdio(0), cout.tie(0), cout.tie(0); cin >> n >> m; for(int i = 1; i <= m; i++){ cin >> a[i].k >> a[i].c; for(int j = 1, x; j <= a[i].k; j++){ cin >> x; a[i].b.push_back(x); } } sort(a + 1, a + 1 + m, cmp); for(int i = 1; i <= m; i++){ for(int j = 1; j < a[i].k; j++){ int FA = Find(a[i].b[0]), FB = Find(a[i].b[j]); if(FA != FB){ fa[FA] = FB; sum += a[i].c; ans++; } } } if(ans != n - 1){ cout << "-1"; } else{ cout << sum; } return 0; }

# B.洛谷 P10188
## 题意
一共有 $N$ 头奶牛,第 $i$ 头的初始奶量为 $a_i$。再给定一个模式串 $S$,对于第 $i$ 位,如果 $S_i = L$,则表示每分钟向左传递 $1$ 升牛奶;如果 $S_i = R$,则表示每分钟向右传递 $1$ 升牛奶。求 $M$ 分钟后,奶牛还总共还剩下几升牛奶。
## 思路
对于每头奶牛,对被其传奶的牛建立一条有向边,同时记录入度。接着拓扑排序,每头入度为零的奶牛每分钟会损失 $1$ 升牛奶。如果该头奶牛的奶已全部传完,则被它传奶的奶牛的入度减一。环不会损失牛奶因为入度始终为一。实际上,构建的图是个基环树。
## 代码
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long

const int N = 2e5 + 10;

int n, m;
vector<int> g[N];
int ind[N], a[N], ans, rec[N];
char ch;
queue<int> q;

signed main(){
  cin >> n >> m;
  for(int i = 1; i <= n; i++){
    cin >> ch;
    if(ch == 'L'){
      if(i == 1){
        g[i].push_back(n);
        ind[n]++;
      }
      else{
        g[i].push_back(i - 1);
        ind[i - 1]++;
      }
    }
    else if(ch == 'R'){
      if(i == n){
        g[i].push_back(1);
        ind[1]++;
      }
      else{
        g[i].push_back(i + 1);
        ind[i + 1]++;
      }
    }
  }
  for(int i = 1; i <= n; i++){
    cin >> a[i];
    ans += a[i];
  }
  for(int i = 1; i <= n; i++){
    if(ind[i] == 0){
      q.push(i);
    }
  }
  while(!q.empty()){
    int u = q.front();
    q.pop();
    if(a[u] < m - rec[u]){
      ans -= a[u];
      for(int i : g[u]){
        ind[i]--;
        rec[i] = max(rec[i], a[u] + rec[u]);
        if(!ind[i]){
          q.push(i);
        }
      }
    }else{
      ans -= m - rec[u];
    }
  }
  cout << ans;
  return 0;
}

C.AtCoder abc335_e

题意

给定一个有 N 个顶点、M 条边的连通无向图,第 i 条边连接顶点 U_i 和顶点 V_i(双向),顶点 v 上写着整数 A_v。问从 1 到 N 的简单非递减路径上,最多有几个不等元素。

思路

const int N = 2e5 + 10, INF = 1e9; int n, m, fa[N], a[N], ind[N]; queue<int> q; int ans[N], mx;

vector<int> g[N]; vector<pii> tmp;

int Find(int x){ return fa[x] == x ? x : fa[x] = Find(fa[x]); }

signed main(){ cin >> n >> m; for(int i = 1; i <= n; i++){ cin >> a[i]; } iota(fa + 1, fa + n + 1, 1); for(int i = 1, u, v; i <= m; i++){ cin >> u >> v; if(a[u] == a[v]){ int A = Find(u), B = Find(v); if(A != B){ fa[B] = A; } } else{ tmp.push_back({u, v}); } } for(pii i : tmp){ int x = Find(i.first), y = Find(i.second); if(a[x] < a[y]){ g[x].push_back(y); ind[y]++; } else{ g[y].push_back(x); ind[x]++; } } fill(ans + 1, ans + 1 + n, -INF); for(int i = 1; i <= n; i++){ if(!ind[i]){ q.push(i); } } while(!q.empty()){ int u = q.front(); if(u == Find(1)){ ans[u] = 1; } q.pop(); for(int v : g[u]){ int vv = v; ans[vv] = max(ans[vv], ans[u] + 1); ind[vv]--; if(!ind[vv]){ q.push(vv); } } } cout << max(ans[Find(n)], 0ll); return 0; }



# F.AtCoder abc235_e
## 题意
给定一个 $n$ 个点,$m$ 条边无向连通图,每条边有权值 $c_i$(各不相同),连接 $u_i,v_i$。在给定 $q$