学习路上的杂事/坑

  • mac上的eclipse安装sqljdbc驱动,jdk15会出错,换成1.8就好了
  • 连接数据库的代码:

package test;

import java.sql.*;

class Test{
	public static void main(String[] args) {
	// TODO Auto-generated method stub
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.println("驱动加载成功");
			
			Connection conn=DriverManager.getConnection(
					"jdbc:sqlserver://10.120.123.1:1433;DatabaseName=YGGL","SA","yxr010527!"
					);
			System.out.println("连接数据库成功");
			
			Statement st=conn.createStatement();
			String sqldelete="delete from departments where departmentID='011';";
			st.executeUpdate(sqldelete);
			st.close();
			System.out.println(sqldelete);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	
	}
}
  • c++STL中set的用法
begin/rendrbeginend
34

2.set重载运算符

struct cmp{
    bool operator() (const int &a,const int &b) const{return dfn[a]<dfn[b];}
};
set<int,cmp> s;

注意有三个const,按照dfs序从小到大排序
acwing355

#include <bits/stdc++.h>

using namespace std;

#define int long long

vector<pair<int,int>> v[100005];
int n,m;

int d[100005];//层数
int h[100005];//深度
int fa[100005][24];//祖先
int dfn[100005];//dfs序
int tot=0;

void dfs(int x,int pre) {
    for(auto p:v[x]) {
        if(p.first==pre) continue;
        d[p.first]=d[x]+1;h[p.first]=h[x]+p.second;dfn[p.first]=++tot;
        fa[p.first][0]=x;
        for(int i=1;i<24;i++) fa[p.first][i]=fa[fa[p.first][i-1]][i-1];
        dfs(p.first,x);
    }
}
int lca(int x,int y){
    if(d[x]>d[y]) swap(x,y);
    for(int i=23;i>=0;i--) {
        if(d[fa[y][i]]>=d[x]) y=fa[y][i];
    }
    if(x==y) return x;
    for(int i=23;i>=0;i--) {
        if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
    }
    return fa[x][0];
}
int dis(int x,int y){return h[x]+h[y]-2*h[lca(x,y)];}
int ans=0;
struct cmp{
    bool operator() (const int &a,const int &b) const{return dfn[a]<dfn[b];}
};
set<int,cmp> s;
void solve() {
    cin>>n;
    for(int i=1;i<n;i++) {
        int x,y,z;
        cin>>x>>y>>z;
        v[x].emplace_back(y,z);
        v[y].emplace_back(x,z);
    }
    d[1]=1;h[1]=1;dfn[1]=++tot;
    dfs(1,0);
    cin>>m;
    int cnt=0;
    for(int i=1;i<=m;i++) {
        char c; cin>>c;
        //for(auto p:s) cout<<p<<" ";cout<<endl;
        if(c=='?') cout<<ans/2<<endl;
        else if(c=='+') {
            int x; cin>>x;
            if(cnt==1) {
                auto p=s.begin();
                //cout<<*p<<endl;
                ans+=dis(*p,x)*2;
                //cout<<*p<<" "<<x<<endl;
            }
            if(cnt>1) {
                auto p=s.lower_bound(x);
                if(p==s.begin()) p=s.end();p--;
                auto pr=s.upper_bound(x);
                if(pr==s.end()) pr=s.begin();
                ans-=dis(*p,*pr);
                ans+=dis(*p,x)+dis(*pr,x);
            }
            s.insert(x);
            cnt++;
        }
        else {
            int x; cin>>x;
            if(cnt==2) ans=0;
            if(cnt>2) {
                auto p=s.lower_bound(x);
                if(p==s.begin()) p=s.end();p--;
                auto pr=s.upper_bound(x);
                if(pr==s.end()) pr=s.begin();
                ans-=(dis(*p,x)+dis(*pr,x));
                ans+=dis(*p,*pr);
            }
            s.erase(x);
            cnt--;
        }
    }

}

signed main() {
    solve();
    return 0;
}

  • c++中auto 的用法:如果想改变原本的值,要加上引用
  • 例如keruskal算法
void kruskal() {
    for(int i=1;i<=n;i++) f[i]=i;
    sort(e.begin(),e.end(),cmp);
    for(auto &p:e) {
        int x=find(p.to),y=find(p.from);
        if(x==y) continue;
        v[p.to].emplace_back(p.from,p.val);
        v[p.from].emplace_back(p.to,p.val);
        f[x]=y;p.flag=1;
        ans+=p.val;
    }
}
  • 不要并查集查完之后,建树的时候要记得千万不要把祖先连一起!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值