ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C. Game Master(强连通分量,缩点,建图

2022-02-27 17:33:51  阅读:180  来源: 互联网

标签:缩点 int Game 建图 maxn low now id define


C. Game Master
题意:
n n n 个选手在进行比赛,比赛有两个场地,每个选手都有两个值表示在这两个场地上的能力值
n − 1 n-1 n−1 场比赛,每次从剩余的选手中选出两个,并在两个场地中任选一个,强者获胜,最后剩下一个选手即为冠军
问 n n n 个人是否能成为冠军,如果能输出 1 1 1,否则输出 0 0 0
最后输出一个 01 01 01 串
思路:
题解
题解讲的非常棒

看了题解感觉这个题目有点熟悉,然后发现和这个题差不多 P2341 [USACO03FALL / HAOI2006] 受欢迎的牛 G
原来是板子题
code:

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define ld long double
#define all(x) x.begin(), x.end()
#define mem(x, d) memset(x, d, sizeof(x))
#define eps 1e-6
using namespace std;
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
ll n, m;
struct node{
	int next, to;
}e[maxn];
int head[maxn], cnt;
int dfn[maxn], low[maxn], dcnt;
int stk[maxn], top;
bool vis[maxn];
int scc_id[maxn];
int id;

struct Node{
	int x, id;
	bool operator<(const Node &B)const{
		return x < B.x;
	}
}a[maxn];

void add(int x, int y){
	e[++cnt].to = y;
	e[cnt].next = head[x];
	head[x] = cnt;
}
void tarjan(int now){
	dfn[now] = low[now] = ++dcnt;
	stk[++top] = now;
	vis[now] = 1;
	for(int i = head[now]; i; i = e[i].next){
		int to = e[i].to;
		if(!dfn[to]){
			tarjan(to);
			low[now] = min(low[now], low[to]);
		}
		else if(vis[to])
			low[now] = min(low[now], dfn[to]);
	}
	if(dfn[now] == low[now]){
		++id;
		int t;
		do{
			t = stk[top--];vis[t] = 0;scc_id[t] = id;
		}while(t != now);
	}
}
int in[maxn], ans;
void init(){
	mem(dfn, 0);mem(low, 0);mem(head, 0);mem(in, 0);
	cnt = dcnt = top = id = 0;
}
void work()
{
	cin >> n;
	init();
	for(int j = 1; j <= 2; ++j)
	{
		for(int i = 1; i <= n; ++i){
			cin >> a[i].x;a[i].id = i;
		}
		sort(a + 1, a + 1 + n);
		for(int i = 2; i <= n; ++i) add(a[i].id, a[i-1].id);
	}
	for(int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i);
	for(int i = 1; i <= n; ++i){
		for(int j = head[i]; j; j = e[j].next){
			int to = e[j].to;
			if(scc_id[i] != scc_id[to]){
				in[scc_id[to]]++;
			}
		}
	}
	for(int i = 1; i <= n; ++i) cout << (in[scc_id[i]] ? 0 : 1);cout << endl;
}

int main()
{
	ios::sync_with_stdio(0);
	int TT;cin>>TT;while(TT--)
	work();
	return 0;
}

标签:缩点,int,Game,建图,maxn,low,now,id,define
来源: https://blog.csdn.net/cosx_/article/details/123163257

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有