2019 南昌邀请赛 Winner (tarjan缩点)

Ichuan really likes to play games, so he organized a game competition with participating players. Follows are the rule of the game competition. 1. There are three modes in the game, in each mode, players have different ability values, in addition, each player may have different ability value in different mode. 2. There are a total of matches. In each match, two players who have not yet been eliminated will play against each other in one of the modes. The player who has high ability in this mode will win, and the other one will be eliminated. 3. The only player who remains in the game after all matches will be the winner. As the organizer of the game, ichuan can manipulate the result of the game. Namely, for each match, he can choose both players and match mode. Of course, he can only choose players who have not yet been eliminated. Ichuan has some friends, some of them will ask him: "Does player will be the winner?" Answering this question will give you a lot of reward, so you need to write a program which can answer these questions. Input The first line contains integers and , the number of players and the number of requests. The next three lines, each line contains integers, the integer represents the ability value of player in this mode., The next lines, each line only has one integer , which means ichuan's friend wants to know if player can be the winner. Output For each query, if player has a chance to be the winner, output "YES", otherwise output "NO" 输出时每行末尾的多余空格,不影响答案正确性 N N − 1 N − 1 X N Q(1 ≤ N, Q ≤ 10 ) 5 N Xth Y (1 ≤ Y ≤ 10 ) 6 X q X(1 ≤ X ≤ n) X X  2019/7/23 Winner - 计蒜客 https://www.jisuanke.com/contest/3098/239926 2/2 复制 复制 样例输入 4 4 1 2 3 4 1 2 4 3 2 1 3 4 1 2 3 4 样例输出 NO NO YES YES

首先把实际问题建立DAG模型

由于每个值的能力值都不相同

我们将每个人的能力值排好序

然后按照顺序建立n-1条边

那么我们就可以

进行tarjan缩点了

没有出度的的点就是YES

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<stack>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

pair<int,int> p1[200005];
pair<int,int> p2[200005];
pair<int,int> p3[200005];

int IN[200005];
int OUT[200005];
int first[200005];
int nxt[600005];
int to[600005];
int instack[200005];
int dfn[200005];
int low[200005];
int num=0;
int belong[200005];
int id=0;
stack<int> st;
void tarjan(int x)
{
    low[x]=dfn[x]=++id;
    st.push(x);
    instack[x]=1;
    for(int i=first[x]; i!=-1; i=nxt[i])
    {
        int tmp=to[i];
        if(!dfn[tmp])
        {
            tarjan(tmp);
            low[x]=min(low[x],low[tmp]);
        }
        else if(instack[tmp])
        {
            low[x]=min(low[x],low[tmp]);
        }
    }
    if(dfn[x]==low[x])
    {
        num++;
        while(st.top()!=x)
        {
            instack[st.top()]=0;
            belong[st.top()]=num;
            st.pop();
        }
        instack[st.top()]=0;
        belong[st.top()]=num;
        st.pop();
    }
}
int main()
{
    id=0;
    memset(first,-1,sizeof(first));
    memset(nxt,-1,sizeof(nxt));
    int n;
    int q;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&p1[i].first);
        p1[i].second=i;
    }
     for(int i=1;i<=n;i++)
    {
        scanf("%d",&p2[i].first);
        p2[i].second=i;
    }
     for(int i=1;i<=n;i++)
    {
        scanf("%d",&p3[i].first);
        p3[i].second=i;
    }
    sort(p1+1,p1+1+n);
    sort(p2+1,p2+1+n);
    sort(p3+1,p3+1+n);
    int cnt=1;
    int nx;
    for(int i=1;i<n;i++)
    {
        nx=i+1;
        //while(p1[i].first==p1[nx].first&&i<=n) nx++;
        cnt++;
        nxt[cnt]=first[p1[i].second];
        first[p1[i].second]=cnt;
        to[cnt]=p1[nx].second;
    }
    for(int i=1;i<n;i++)
    {
        nx=i+1;
        //while(p2[i].first==p2[nx].first&&i<=n) while(nx)nx++;
        cnt++;
        nxt[cnt]=first[p2[i].second];
        first[p2[i].second]=cnt;
        to[cnt]=p2[nx].second;
    }
    for(int i=1;i<n;i++)
    {
        nx=i+1;
        //while(p3[i].first==p3[nx].first&&i<=n) nx++;
        cnt++;
        nxt[cnt]=first[p3[i].second];
        first[p3[i].second]=cnt;
        to[cnt]=p3[nx].second;
    }
    for(int i=1; i<=n; i++)
    {
        if(!dfn[i])
        {
            tarjan(i);
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=first[i]; j!=-1; j=nxt[j])
        {
            int tmp=to[j];
            if(belong[i]!=belong[tmp])
            {
                IN[belong[tmp]]++;
                OUT[belong[i]]++;
            }
        }
    }
    for(int i=1;i<=q;i++)
    {
        int tmp;
        scanf("%d",&tmp);
        if(OUT[belong[tmp]]==0)
        {
            printf("YES
");
        }
        else
        {
            printf("NO
");
        }
    }
}
原文地址:https://www.cnblogs.com/caowenbo/p/11852241.html