2019ICPC南昌邀请赛正赛 G (Tarjan)

题目地址

题意:有n个人和3种状态,每个人在不同的状态下有个能力值,两人在一种模式下比赛,能力值高的人获胜。
询问某个人能否获胜。

题解:Tarjan对每个状态进行缩点。

先对每个状态的人按照能力值从大到小排序,能力值高的向能力值低的人连一条边。。然后跑一遍Tarjan,最后入度为0的联通分量里的所有点都能获胜

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
#include 
using namespace std;
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mem(a,b) memset(a,b,sizeof(a));
#define lowbit(x)  x&-x;  
#define debugint(name,x) printf("%s: %d\n",name,x);
#define debugstring(name,x) printf("%s: %s\n",name,x);
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-6;
const int maxn = 1e5+5;
const int mod = 1e9+7;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
vector<int>v[maxn];
stack<int>st;
int n,m,low[maxn],dfn[maxn],vis[maxn],in[maxn],belong[maxn],flag,tot,cnt;
struct node{
	int id;
	int val;
}cal[4][maxn];

bool cmp(node a,node b){
	return a.val > b.val;
}
void Tarjan(int x){
    low[x] = dfn[x] = ++tot;
    st.push(x);
    vis[x] = 1;
    for(int i = 0; i < v[x].size(); i++){
        int u = v[x][i];
        if(!dfn[u]){
            Tarjan(u);
            low[x] = min(low[x],low[u]);
        }
        else if(vis[u]){
            low[x] = min(low[x],dfn[u]);
        }
    }

    if(low[x] == dfn[x]){
        cnt++;
        while(1){
            int now = st.top();
            st.pop();
			belong[now] = cnt;
            vis[now] = 0;
            if(now == x) break;
        }
    }
}
void init(){
    cnt = tot = 0;
    mem(vis,0);
    mem(low,0);
    mem(dfn,0);
    mem(in,0);
    while(!st.empty()) st.pop();
    for(int i = 1; i <= n; i++)
        v[i].clear();
}

int main() {
    while(~scanf("%d%d",&n,&m)){
        init();
        for(int i = 1; i <= 3; i++){
			for(int j = 1; j <= n; j++){
				scanf("%d",&cal[i][j].val);
				cal[i][j].id = j;
			}
			sort(cal[i]+1,cal[i]+n+1,cmp);
			for(int j = 1; j < n; j++){
				v[cal[i][j].id].push_back(cal[i][j+1].id);
			}
		}
		
        for(int i = 1; i <= n;i++)
            if(!dfn[i])
                Tarjan(i);

		for(int i = 1; i <= n; i++){
			for(int j = 0; j < v[i].size(); j++){
				int to = v[i][j];
				if(belong[i] != belong[to]){
					in[belong[to]] = 1;
				}
			}
		}
		while(m--){
			int x;
			scanf("%d",&x);
		//	printf("belong:%d\n",belong[x]);
			if(in[belong[x]] == 0) puts("YES");
			else puts("NO");
		}
    }
}

你可能感兴趣的:(Tarjan,图论)