其他分享
首页 > 其他分享> > C. Game Master(强连通分量,缩点,建图

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

作者:互联网

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