牛客小白月赛30 全题解

最近比赛状态有点迷,这场题目其实都挺简单的.压根没涉及到高级的算法和思维.
A-黑白边
最小生成树裸题.判断一下无解就好了.
代码:

#define LL long long
#define pq priority_queue
#define ULL unsigned long long
#define pb push_back
#define mem(a,x) memset(a,x,sizeof a)
#define pii pair<int,int>
#define fir(i,a,b) for(int i=a;i<=(int)b;++i)
#define afir(i,a,b) for(int i=(int)a;i>=b;--i)
#define ft first
#define vi vector<int>
#define sd second
#define ALL(a) a.begin(),a.end()
#define bug puts("-------")
#define mpr(a,b) make_pair(a,b)
#include <bits/stdc++.h>

using namespace std;
const int N = 2e5+10;

inline int read(){
   
	int x = 0,f=1;char ch = getchar();
	while(ch<'0'||ch>'9'){
   if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){
   x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

int nxt[N],head[N],to[N],edge[N],ct;
void addedge(int x,int y,int z){
   
	nxt[++ct] = head[x];head[x] = ct;to[ct] = y;edge[ct] = z;
}
struct E{
   
	int x,y,z;
};
bool cmp(E a,E b){
   
	return a.z < b.z;
}
vector<E> e;
int fa[N];
int find(int x){
   
	int r,j,k;r=j=k=x;
	while(r!=fa[r]) r=fa[r];
	while(j!=r) j=fa[k],fa[k]=r,k=j;
	return r;
}
int main(){
   
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,m;
	cin >> n >> m;
    fir(i,1,n) fa[i] = i;
	fir(i,1,m){
   
		int x,y,z;
		cin >> x >> y >> z;
		addedge(x,y,z);
		addedge(y,x,z);
		E ee = {
   x,y,z};
		e.pb(ee);
	}
	sort(ALL(e),cmp);
	int num = 0;
	for(auto x:e){
   
		int fx = find(x.x),fy = find(x.y);
		if(fx!=fy){
   
			num+=x.z;
			fa[fx] = fy;
		}
	}
	int f = 0;
	fir(i,1,n){
   
		int fx = find(i);
		if(!f) f = fx;
		else if(f!=fx){
   
			f = -1;
			break;
		}
	}
	if(f == -1) cout << -1 << "\n";
	else cout << num << '\n';
	
	return 0;
}	

B-最好的宝石
线段树裸题,比赛的时候因为一个小问题线段树的代码出错了(标注在代码里了),搞的去思路混乱,敲了个带修主席树还炸了空间…迷迷迷.
代码:

#define LL long long
#define pq priority_queue
#define ULL unsigned long long
#define pb push_back
#define mem(a,x) memset(a,x,sizeof a)
#define pii pair<int,int>
#define fir(i,a,b) for(int i=a;i<=(int)b;++i)
#define afir(i,a,b) for(int i=(int)a;i>=b;--i)
#define ft first
#define vi vector<int>
#define sd second
#define ALL(a) a.begin(),a.end()
#define bug puts("-------")
#define mpr(a,b) make_pair(a,b)
#include <bits/stdc++.h>

using namespace std;
const int N = 3e6+10;

inline int read(){
   
	int x = 0,f=1;char ch = getchar();
	while(ch<'0'||ch>'9'){
   if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){
   x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
namespace seg{
   
	int mx[N],num[N];
	#define ls 2*i
	#define rs 2*i+1
	void add(int i,int l,int r,int p,int v){
   
		if(l == r){
   
			mx[i] = v,num[i] = 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值