最近比赛状态有点迷,这场题目其实都挺简单的.压根没涉及到高级的算法和思维.
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