社区讨论

萌新刚学OI,开O2RE0,不开AC,为什么qwq

P2387[NOI2014] 魔法森林参与者 10已保存回复 11

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
11 条
当前快照
1 份
快照标识符
@mi6wlwou
此快照首次捕获于
2025/11/20 12:00
4 个月前
此快照最后确认于
2025/11/20 15:06
4 个月前
查看原帖
代码如下:
CPP
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson ch[x][0]
#define rson ch[x][1]
using namespace std;

int n,m,f[200020],ch[200020][2],tag[200020],w[200020],sum[200020];
struct node
{
	int from,to,a,b;
} e[200020];

int cmp(node a,node b)
{
	return a.a<b.a;
}

int not_root(int now)
{
	int x=f[now];
	return lson==now||rson==now;
}

int push_up(int x)
{
	sum[x]=x;
	if(e[sum[x]].b<e[sum[lson]].b)
	{
		sum[x]=sum[lson];
	}
	if(e[sum[x]].b<e[sum[rson]].b)
	{
		sum[x]=sum[rson];
	}
}

int rev(int x)
{
	swap(lson,rson);
	tag[x]^=1;
}

int push_down(int x)
{
	if(tag[x])
	{
		rev(lson);
		rev(rson);
		tag[x]^=1;
	}
}

int rotate(int x)
{
	int y=f[x],z=f[y],kd=ch[y][1]==x,xs=ch[x][!kd];
	if(not_root(y))  ch[z][ch[z][1]==y]=x;
	ch[x][!kd]=y;
	ch[y][kd]=xs;
	if(xs)  f[xs]=y;
	f[y]=x;
	f[x]=z;
	push_up(y);
}

int push_all(int x)
{
	if(not_root(x))
	{
		push_all(f[x]);
	}
	push_down(x);
}

int splay(int x)
{
	int y,z;
	push_all(x);
	while(not_root(x))
	{
		y=f[x],z=f[y];
		if(not_root(y))
		{
			(ch[y][1]==x)^(ch[z][1]==y)?rotate(x):rotate(y);
		}
		rotate(x);
	}
	push_up(x);
}

int access(int x)
{
	int y;
	for(y=0; x; y=x,x=f[x])
	{
		splay(x);
		rson=y;
		push_up(x);
	}
}

int make_root(int x)
{
	access(x);
	splay(x);
	rev(x);
}

int split(int x,int y)
{
	make_root(x);
	access(y);
	splay(y);
}

int find_root(int x)
{
	access(x);
	splay(x);
	while(lson)
	{
		push_down(x);
		x=lson;
	}
	return x;
}

int link(int x,int y)
{
	make_root(x);
	if(find_root(y)==x) return 0;
	f[x]=y;
	return 1;
}

int cut(int x,int y)
{
	make_root(x);
	if(find_root(y)!=x||f[x]!=y||rson) return 0;
	f[x]=ch[y][0]=0;
	push_up(y);
	return 1;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1; i<=m; i++)
	{
		scanf("%d%d%d%d",&e[i].from,&e[i].to,&e[i].a,&e[i].b);
	}
	sort(e+1,e+m+1,cmp);
	int ans=0x3f3f3f3f;
	for(int i=1; i<=m; i++)
	{
		make_root(e[i].from+m);
		if(find_root(e[i].to+m)==e[i].from+m)
		{
			split(e[i].from+m,e[i].to+m);
			int tmp=sum[e[i].to+m];
			if(e[tmp].b>e[i].b)
			{
				cut(e[tmp].to+m,tmp);
				cut(e[tmp].from+m,tmp);
				link(e[i].from+m,i);
				link(e[i].to+m,i);
			}
		}
		else
		{
			link(e[i].from+m,i);
			link(e[i].to+m,i);
		}
		make_root(1+m);
		if(find_root(n+m)==1+m) ans=min(ans,e[i].a+e[sum[n+m]].b);
	}
	if(ans<0x3f3f3f3f) printf("%d\n",ans);
	else puts("-1");
}

回复

11 条回复,欢迎继续交流。

正在加载回复...