牛客网多校4 Hash Function(拓扑排序)

感觉自己很恍惚,真的是越来越菜了。

题目:给一个哈希表,判断哈希表是不是合法,合法的话求一个字典序最小的插入序列,不合法输出-1

思路:建图好困难,比赛时自己建图乱七八糟,赛后看了大佬的代码才最终完成自己的。题解是线段树建图...emm没用过。

记录一个movl[i]表示这个点可以往前移动的合法步数,过程中找前面的可行点连边,movl路径压缩。总共连边数最多最多也是不会超过2n的,O(n)建图。

#include 
using namespace std;
const int maxn=2e5+10;
struct node1
{
    int v,next;
};
node1 edge[2*maxn];
struct node2
{
    bool friend operator < (node2 n1,node2 n2){
        return n1.val>n2.val;
    }
    int id,val;
};
priority_queue que;
int ary[maxn],first[maxn],degree[maxn];
int ans[maxn];
int tot;
void addedge(int u,int v)
{
    edge[tot].v=v;
    edge[tot].next=first[u];
    first[u]=tot++;
}
int n,nu;
void toposort()
{
    node2 cur,tmp;
    while(!que.empty()) que.pop();
    int num=0,i,u,v;
    for(i=0;i=movl[i])
                    j=(j-movl[j]+n)%n;//j往前移
                else break;
            }
            if(!flag) break;
        }
        if(flag)
            toposort();//拓扑排序
        else puts("-1");
    }
    return 0;
}
/*
100
9
16 -1 -1 -1 -1 -1 -1 7 8

4
8 5 2 3

10
8 10 -1 -1 34 75 86 55 88 18

2
1 2

5
100000 1000000000 111 1154 1123
*/

 

你可能感兴趣的:(多校,拓扑)