E. Qpwoeirut and Vertices

E. Qpwoeirut and Vertices

给定n个点m条的无向连通图和q个询问:
给定l,r,对于任意两点 l<=a,b<=r连通至少需要1~k条边的最小k值

/*input
3
2 1 2
1 2
1 1
1 2
5 5 5
1 2
1 3
2 4
3 4
3 5
1 4
3 4
2 2
2 5
3 5
3 2 1
1 3
2 3
1 3
 
*/
 
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int fa[N],dp[N][20],id[N][20],len[N];
int ans[N][20],lg[N];
typedef pair<int,int> pii;
vector<pii> v[N];
int get(int x){
    if(x!=fa[x]) return fa[x]=get(fa[x]);
    return x;
}
void link(int x,int y){
    fa[get(x)]=y;
}
void dfs(int root,int fa,int dep){
    len[root]=dep;
    for(auto node:v[root]){
        if(node.first==fa) continue;
        dp[node.first][0]=root;
        id[node.first][0]=node.second;
        dfs(node.first,root,dep+1); 
    }
}
int lca(int x,int y){
    if(len[x]>len[y]) swap(x,y);
    int ans=0;
    for(int i=19;i>=0;i--){
        if(len[dp[y][i]]>=len[x]){
            ans=max(ans,id[y][i]);
            y=dp[y][i];
        }
    }
    for(int i=19;i>=0;i--){
        if(dp[x][i]!=dp[y][i]){
            ans=max({ans,id[x][i],id[y][i]});
            x=dp[x][i];
            y=dp[y][i];
        }
    }
    if(x!=y) ans=max({ans,id[x][0],id[y][0]});
    return ans;
}
void solve(){
    int n,m,q;
    cin>>n>>m>>q;
    for(int i=1;i<=n;i++){
        fa[i]=i;
        v[i].clear();
    } 
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        if(get(x)!=get(y)){
            v[x].push_back({y,i});
            v[y].push_back({x,i});
            link(x,y);
        }
    }
    dfs(1,0,1);
    
 
    for(int i=1;i<20;i++){
        for(int j=1;j<=n;j++){
            dp[j][i]=dp[dp[j][i-1]][i-1];
            id[j][i]=max(id[j][i-1],id[dp[j][i-1]][i-1]);
        }
    }
 
    for(int i=1;i<n;i++)
        ans[i][0]=lca(i,i+1);
    for(int i=0;1<<i<=n;i++){
        for(int j=1<<i;j<(1<<(i+1))&&j<=n;j++){
            lg[j]=i;
        }
    }
    for(int i=1;i<20;i++){
        for(int j=1;j+(1<<i)-1<n;j++){
            ans[j][i]=max(ans[j][i-1],ans[j+(1<<(i-1))][i-1]);
        }
    }
    while(q--){
        int l,r;
        cin>>l>>r;
        if(l==r) cout<<0<<' ';
        else{
            int length=r-l;
            cout<<max(ans[l][lg[length]],ans[r-(1<<lg[length])][lg[length]])<<' ';
        }
    }
    puts("");
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--) solve();
}

解释这段代码import jittor as jt from jittor import nn jt.flags.use_cuda = 1 import os import tqdm import numpy as np import imageio import argparse import jrender as jr from jrender import neg_iou_loss, LaplacianLoss, FlattenLoss current_dir = os.path.dirname(os.path.realpath(file)) data_dir = os.path.join(current_dir, 'data') class Model(nn.Module): def init(self, template_path): super(Model, self).init() # set template mesh self.template_mesh = jr.Mesh.from_obj(template_path, dr_type='n3mr') self.vertices = (self.template_mesh.vertices * 0.5).stop_grad() self.faces = self.template_mesh.faces.stop_grad() self.textures = self.template_mesh.textures.stop_grad() # optimize for displacement map and center self.displace = jt.zeros(self.template_mesh.vertices.shape) self.center = jt.zeros((1, 1, 3)) # define Laplacian and flatten geometry constraints self.laplacian_loss = LaplacianLoss(self.vertices[0], self.faces[0]) self.flatten_loss = FlattenLoss(self.faces[0]) def execute(self, batch_size): base = jt.log(self.vertices.abs() / (1 - self.vertices.abs())) centroid = jt.tanh(self.center) vertices = (base + self.displace).sigmoid() * nn.sign(self.vertices) vertices = nn.relu(vertices) * (1 - centroid) - nn.relu(-vertices) * (centroid + 1) vertices = vertices + centroid # apply Laplacian and flatten geometry constraints laplacian_loss = self.laplacian_loss(vertices).mean() flatten_loss = self.flatten_loss(vertices).mean() return jr.Mesh(vertices.repeat(batch_size, 1, 1), self.faces.repeat(batch_size, 1, 1), dr_type='n3mr'), laplacian_loss, flatten_loss
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值