L - Connections in Galaxy War

24 篇文章 0 订阅
11 篇文章 0 订阅

本题题意为给定逐个点的力量值,再给出通道数,和询问数,要求给出询问点的来救援的点,本题我是百度出来的,下面写法也一致,本题是反过来输出结果,利用栈来实现

#include<iostream>
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn=20000+1000;
const int Big=10000;
int h[maxn],power[maxn],par[maxn],use[maxn];
int n;
struct node{
	int u,v,op;
}data[3*maxn];
int find(int x)
{
	int k=par[x];
	if(k!=x)
	{
		par[x]=find(k);	
	}
	return par[x];
}
void Union(int x,int y)
{
	int p1=find(x),p2=find(y);
	if(p1!=p2)
	{
		if(power[p1]>power[p2])	par[p2]=p1;
		else if(power[p1]<power[p2])	par[p1]=p2;
		else if(p1<p2)	par[p2]=p1;
		else par[p1]=p2;
	}
	
}
int main()
{
	int t,m,a,b,q;
	char s[maxn];
	//freopen("L.txt","r",stdin); 
	while(scanf("%d",&n)!=EOF)
	{
		for(int i=0;i<n;i++)
		{
			scanf("%d",&power[i]);
			par[i]=i;
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d %d",&a,&b);
			if(a>b)	swap(a,b);
			h[i]=a+b*Big;
			use[i]=0;
		}
		sort(h,h+m);
		scanf("%d",&q);
		for(int i=0;i<q;i++)
		{
			scanf("%s",s);
			if(s[0]=='d'){
				scanf("%d %d",&a,&b);
				if(a>b)	swap(a,b);
				data[i].u=a,data[i].v=b,data[i].op=0;
			int k=lower_bound(h,h+m,a+b*Big)-h;
			use[k]=1;
			}
			else
			{
				scanf("%d",&a);
				data[i].u=a;data[i].op=1;
			}
		}
		for(int i=0;i<m;i++)
		{
			int u=h[i]%Big,v=h[i]/Big;		
			if(use[i]==0)
				Union(u,v);
		}
		stack<int> st;
		for(int i=q-1;i>=0;i--)
		{
			if(!data[i].op)
				Union(data[i].u,data[i].v);
			else
			{
				int u=find(data[i].u);
				if(power[u]<=power[data[i].u])
					st.push(-1);
				else
					st.push(u);
			}
		}	
		if(t++)	printf("\n");
		while(st.size())
		{
			printf("%d\n",st.top());
			st.pop();
		}
	}
	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值