第四周组题

· · 生活·游记

组题

A

因为对于 S'=\{0\},都有 \operatorname{mex}(S')=1,所以输出 \sum_{i\in S}\max\{i,1\}。

B

将员工上下级关系看成森林,输出 \max depth_i 即可,因为这一条链的 depth_i 个人要满足两两组别不同。

C

模拟即可。

D

切比雪夫距离+模拟。

E

计算出每种颜色的最高位置牌,每次更改将中间的第一张牌后移一位即可。

F

字符画解释

-1:
   l  a/b  r
   |---|---|
 |--x--|

0:
   l  a(b) r
   |---|---|

1:
   l a   b r
   |-|---|-|
      |-|(长为x)

2:
   l      a b r
   |------|-|-|
   |----|(长为x)

  c是游标(初始为a)

   l      c b r  初始
   |------|-|-|

  l(c)      b r 第一步
   |--------|-|

   l     b(c) r 第二步
   |--------|-|

3:
   l  a   b   r
   |--|---|---|
   |----|(长为x)

  c是游标(初始为a)

   l      c b r    初始
   |------|-|-|

   l        b r(c) 第一步
   |--------|-|

  l(c)      b r    第二步
   |--------|-|

   l     b(c) r    第三步
   |--------|-|

G

代码:(思路我也说不清,代码有两行核心思路注释)

#include<bits/stdc++.h>
using namespace std;
#define many
int rnd(long long x){
    int cnt=0;
    while(x)cnt++,x/=10;
    return --cnt;
}
int Rnd(long long x){
    int cnt=0;
    while(x%10==0)cnt++,x/=10;
    return cnt;
}
long long n,m;
long long pw(long long x){
    long long res=1;
    while(x){
        res*=10;
        x--;
    }
    return res;
}
void solve(){
    cin>>n>>m;
    while(!(n/pw(Rnd(n))&1)&&m>=5)n*=5,m/=5;//先利用好因数
    while(!(n/pw(Rnd(n))%5)&&m>=2)n*=2,m/=2;
    long long k=pw(rnd(m));//贪心
    n*=k,m/=k;
    cout<<n*m<<"\n";
}
int main(){
    int t;
#ifdef many
    cin>>t;
#else
    t=1;
#endif
    for(;t--;solve());
    return 0;
}

H

将最小权值的点与其他点连边,优惠的边连上,再跑MST。

I

枚举“最大权值的边”,跑DSU找出最小的边。

J

将能替换的最大的边替换为当前边。实现使用MST上LCA。

#include<bits/stdc++.h>
using namespace std;
#define int long long
vector<pair<int,int>>G[200005];
int dep[200005],anc[200005][20],dis[200005][20],tree;
vector<tuple<int,int,int,int>>vec;
int n,m;
bool dbg;
// #define many
void dfs(int c,int fa,int d){
    dep[c]=dep[fa]+1;
    if(c==1)dep[c]=0;
    anc[c][0]=fa;
    dis[c][0]=d;
    for(int i=1;i<20;i++){
        anc[c][i]=anc[anc[c][i-1]][i-1];
        dis[c][i]=max(dis[c][i-1],dis[anc[c][i-1]][i-1]);
    }
    for(auto i:G[c]){
        if(i.first!=fa){
            dfs(i.first,c,i.second);
        }
    }
}
int lca(int u,int v){
    if(dep[u]<dep[v])swap(u,v);
    int ans=0;
    for(int i=19;i>=0;i--){
        if(dep[anc[u][i]]>=dep[v]){
            if(dbg)cout<<u<<" "<<i<<" "<<anc[u][i]<<" "<<dis[u][i]<<"\n";
            ans=max(ans,dis[u][i]);
            u=anc[u][i];
        }
    }
    if(u==v)return ans;
    for(int i=19;i>=0;i--){
        if(anc[u][i]!=anc[v][i]){
            if(dbg)cout<<u<<" "<<i<<" "<<anc[u][i]<<" "<<dis[u][i]<<"\n",cout<<v<<" "<<i<<" "<<anc[v][i]<<" "<<dis[v][i]<<"\n";
            ans=max({ans,dis[u][i],dis[v][i]});
            u=anc[u][i];
            v=anc[v][i];
        }
    }
    return max({ans,dis[u][0],dis[v][0]});
}
struct bcj{
    vector<int>f;
    bcj(){}
    bcj(int n){
        f=vector<int>(n+2);
        iota(f.begin(),f.end(),0);
    }
    int find(int x){
        return f[x]==x?x:f[x]=find(f[x]);
    }
    bool conn(int x,int y){
        x=find(x),y=find(y);
        if(x==y)return false;
        f[x]=y;
        return true;
    }
};
int kruskal(){
    bcj x(n);
    int res=0,cnt=0;
    sort(vec.begin(),vec.end());
    for(auto i:vec){
        int w,u,v,k;
        tie(w,u,v,k)=i;
        if(x.conn(u,v)){
            res+=w;
            ++cnt;
            G[u].push_back({v,w});
            G[v].push_back({u,w});
        }
        if(cnt==n-1)break;
    }
    return res;
}
bool cmp(tuple<int,int,int,int>x,tuple<int,int,int,int>y){
    int a,b,c,d,e;
    tie(a,b,c,d)=x;
    tie(a,b,c,e)=y;
    return d<e;
}
void solve(){
    cin>>n>>m;
    for(int i=1,u,v,w;i<=m;i++){
        cin>>u>>v>>w;
        vec.push_back(make_tuple(w,u,v,i));
    }
    tree=kruskal();
    dfs(1,0,0);
    // cout<<dis[3][2]<<"\n";
    sort(vec.begin(),vec.end(),cmp);
    for(int i=0;i<m;i++){
        int u,v,w,id;
        tie(w,u,v,id)=vec[i];
        cout<<tree+w-lca(u,v)<<"\n";
    }
}
signed main(){
    int t;
#ifdef many
    cin>>t;
#else
    t=1;
#endif
    for(;t--;solve());
    return 0;
}