跳到正文

个人记录

96

zhaohongbo1234约 8 分钟
原文

这是 2025年12月13日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月13日
更新
2026年8月10日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

T1

模板

C++
#include<bits/stdc++.h>
#define fr(i,l,r) for(int i=l;i<=r;++i)
using namespace std;
int f[1000010];;
int find(int x){
	if(f[x]==x)return x;
	return f[x]=find(f[x]);
}
signed main(){
	int n,m,q;
	cin>>n>>m>>q; 
    fr(i,1,n){
        f[i]=i;
    }
	fr(i,1,m){
        int x,y;
        cin>>x>>y;
        int fx=find(x),fy=find(y);
        f[fx]=fy;//合并
    }
    while(q--){
        int x,y;
        cin>>x>>y;
        cout<<(find(x)==find(y)?"Yes":"No")<<"\n";//是不是亲戚
    }
	return 0;
}

T2

有几个连通块

C++
#include <bits/stdc++.h>
#define int long long
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
int f[10010],q,a;
vector<int>s;
int n,m;
int find(int x){
	if(f[x]==x)return x;
	else return f[x]=find(f[x]);
} 
void sv(){
	cin>>n;
    fr(i,1,n)f[i]=i;
    if(!n){
        q=1;
        return;
    }
    cin>>m;
	fr(i,1,m){
		int u,v;
		cin>>u>>v;
		int fu=find(u),fv=find(v);
		f[fu]=fv;///合并
	}
    fr(i,1,n){
		if(f[i]==i)a++;
	}
    cout<<a-1;//每个连通块中间一条路(链图)
    a=0;
	cout<<'\n';
}
signed main()
{
	while(1){
        sv();
        if(q)return 0;
    }
	return 0;
}

T3

二分答案

C++
#include <bits/stdc++.h>
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
struct node{
	int u,w;
};
bool cmp(node x,node y){
	return x.w<y.w;
}
int u[10010],v[10010],f[10010],c[10010],d[10010],maxn=-1e9;
vector<node>s[10010];
int n,m;
int find(int x){
	if(f[x]==x)return x;
	else return f[x]=find(f[x]);
} 
bool check(int x){
	fr(i,1,n){
		f[i]=i;
	}
	fr(i,1,n){
		int tmp=s[i].size();
		for(int j=0;j<tmp;j++){
			if(s[i][j].w>x)break;//不在范围
			int v=s[i][j].u;
			int fi=find(i),fv=find(v);
			f[fi]=fv;
		}
	}
	int z=0;
	fr(i,1,n){
		if(f[i]==i)z++;//每个连通块需要一个电话
        //cout<<i<<" "<<f[i]<<" "<<x<<endl;
	}
	return z<=m;
}
signed main()
{
	cin>>m>>n;
	fr(i,1,n){
		cin>>u[i]>>v[i];
	}
	fr(i,1,n-1)
		fr(j,i,n){
			double c=sqrt((u[i]-u[j])*(u[i]-u[j])+(v[i]-v[j])*(v[i]-v[j]));
			c*=100;//123.45->12345
			c+=0.5; //四舍五入
			maxn=max(maxn,int(c));
            //cout<<i<<" "<<j<<" "<<int(c)<<endl;
			s[i].push_back({j,int(c)});
			s[j].push_back({i,int(c)});
		}
	fr(i,1,n)
		sort(s[i].begin(),s[i].end(),cmp);
	int l=0,r=maxn+1;
	while(l+1<r){
		int mid=(l+r)/2;
		if(check(mid))r=mid;
		else l=mid;
	}
	double c=r*1.0/100;if(n==m)cout<<"0.00";//实际不用
    else //if(check(r))
	printf("%.2f",c);
    
    //else printf("%.2f",maxn*1.0/100);
	return 0;
}

T4

模板paim

C++
#include <bits/stdc++.h>
#define int long long//不开longlong见祖宗
#define fr(i, l, r) for(int i = l; i <= r; ++ i)
using namespace std;
struct PairHash {//适配哈希
	template <typename T1, typename T2>
	size_t operator()(const pair<T1, T2>& p) const {
	return hash<T1>()(p.first) ^ hash<T2>()(p.second);
	}
};//unordered_mapO(1),map被卡
//unordered_map<pair<int,int> ,bool,PairHash> vis;
unordered_map<pair<int,int> ,double,PairHash> dis;
int u[10010],v[10010];
inline void read(int& a) {//不加被卡
    int s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        s = s * 10 + ch - '0';
        ch = getchar();
    }
    a = s * w;
}
vector<int>s[10010];
int n,m,w;
signed main()
{
	cin>>n;
	fr(i,1,n){
		read(u[i]);
        read(v[i]);
		dis[{u[i],v[i]}]=1e9;
	}
	queue<int>q;
	q.push(u[1]);
	q.push(v[1]);
	//vis[{u[1],v[1]}]=1;
    swap(u[n],u[1]);
    swap(v[n],v[1]);
	double ans=0;
	while(!q.empty()){//稠密paim
		int x=q.front();
		q.pop();
		int y=q.front();
		q.pop();double minn=1e14;int t,t2,li;
		fr(i,1,n){
            long long a=(x-u[i])*(x-u[i]),b=(y-v[i])*(y-v[i]);//小心爆
			double c=sqrt(a+b);
			if(dis[{u[i],v[i]}]>c){
				dis[{u[i],v[i]}]=c;
			}
            if(minn>dis[{u[i],v[i]}])
                minn=dis[{u[i],v[i]}],t=u[i],t2=v[i],li=i;
		}
        swap(u[n],u[li]);
        swap(v[n],v[li]);//放在后面,不加被卡
        n--;
        if(minn==1e14)continue;
		q.push(t);
		q.push(t2);
		ans+=minn;
	}
	printf("%.2f",ans);
	return 0;
}

卡(60)