2019ICPC南昌邀请赛 G. Winner

  • 题目链接:https://nanti.jisuanke.com/t/40259
  • 题意:有n个人参加三场比赛,每场比赛有一个排名,m次询问,每次询问第x人 能不能获胜,获胜条件有两种,一是本场比赛的第一名,或者在其他比赛中战胜过获胜者。
  • 思路:由题意可知,获胜的条件可以转化为若几个人的输赢关系可以连成一个环且环中有一个人是赢家,则整个环中的人都是赢家。这个环放在图中则变为了求强连通分量。。。so。。
  • 核心技能:tarjan求强连通分量
void trajan(int root){
	if(dfn[root]) return ;
	dfn[root]=low[root]=++indexx;
	s.push(root);
	for(int i=0;i<v[root].size();i++){
		int e=v[root][i];
		if(!dfn[e]){
			trajan(e);
			low[root]=min(low[root],low[e]);
		}
		else if(!scc[e]){
			low[root]=min(low[root],dfn[e]);
		}
	}
	if(low[root]==dfn[root]){
		sccnum++;
		for(;;){
			int x=s.top();
			s.pop();
			scc[x]=sccnum;
			if(ans[x]) is[sccnum]=1;
			if(x==root) break; 
		}
	}
}
  • 小Tips:1. 用vector建邻接表存有向图
    2.用ans[]存储每个人是否可以赢,输入时顺便用sort排序找出本场比赛最大的,也就是肯定能赢的人
    3用is[]来记录每个强连通子图中是否有必赢的人

  • 代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+100;
vector<int> v[maxn];
int dfn[maxn],low[maxn],scc[maxn],ans[maxn],indexx=0,sccnum=0;
int is[maxn];
stack<int> s;
struct st{
	int vl,no;
}a[maxn];
bool cmp(const st &x,const st &y){
	if(x.vl>y.vl) return 1;
	else return 0;
}

void trajan(int root){
	if(dfn[root]) return ;
	dfn[root]=low[root]=++indexx;
	s.push(root);
	for(int i=0;i<v[root].size();i++){
		int e=v[root][i];
		if(!dfn[e]){
			trajan(e);
			low[root]=min(low[root],low[e]);
		}
		else if(!scc[e]){
			low[root]=min(low[root],dfn[e]);
		}
	}
	if(low[root]==dfn[root]){
		sccnum++;
		for(;;){
			int x=s.top();
			s.pop();
			scc[x]=sccnum;
			if(ans[x]) is[sccnum]=1;
			if(x==root) break; 
		}
	}
}


int main(){
	int n,m;
	cin>>n>>m;
	int i,j;
	for(i=0;i<3;i++){
		for(j=0;j<n;j++){
			scanf("%d",&a[j].vl);
			a[j].no=j;
		}
		sort(a,a+n,cmp);
		ans[a[0].no]=1;
		for(j=0;j<n-1;j++){
			v[a[j].no].push_back(a[j+1].no);
		}
	}
	for(i=0;i<n;i++){
		if(!dfn[i]) trajan(i);
	}
	for(i=0;i<n;i++){
		if(is[scc[i]]) ans[i]=1;
	}
	for(i=0;i<m;i++){
		int x;
		cin>>x;
		x--;
		if(ans[x]) printf("YES\n");
		else printf("NO\n");
 	} 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值