zoukankan      html  css  js  c++  java
  • 玲珑学院1072 【DFS】

    蛤蛤,略蠢。

    priority_queue 自定义优先级 和排序是反的

    struct node
    {
        int x,y;
        friend bool operator< (node a,node b)
        {
            if(a.y<b.y)
                return 1;
            if(a.y==b.y&&a.x>b.x)
                return 1;
            return 0;
        }
    };
    priority_queue<node>qu;


    思路:

    +v就加一下,标记一下
    对了-v 遍历一下,标记一下
    加个优先队列维护一下结点的大小,深度。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    
    const int N=1e5+10;
    
    struct node
    {
        int x,y;
        friend bool operator< (node a,node b)
        {
            if(a.y<b.y)
                return 1;
            if(a.y==b.y&&a.x>b.x)
                return 1;
            return 0;
        }
    };
    priority_queue<node>qu;
    
    map<int,int>kt;
    
    struct asd
    {
        int to;
        int next;
    };
    int n;
    asd q[N*4];
    int head[N*4],tol;
    
    bool vis1[100010];
    
    void add(int u,int v)
    {
        q[tol].to=v;
        q[tol].next=head[u];
        head[u]=tol++;
    
        node now;
        now.x=v;
        kt[v]=kt[u]+1;
        now.y=kt[v];
        qu.push(now);
    }
    
    void ceshi()
    {
        node now;
        int uu;
        scanf("%d",&uu);
        for(int i=1;i<=uu;i++)
        {
            scanf("%d%d",&now.x,&now.y);
            qu.push(now);
        }
        while(!qu.empty())
        {
            now=qu.top();
            printf("%d %d 
    ",now.x,now.y);
            qu.pop();
        }
        puts("");
    }
    
    
    void Delet(int u)
    {
        vis1[u]=0;
        for(int i=head[u]; i!=-1; i=q[i].next)
        {
            int v=q[i].to;
            if(!vis1[v])
                continue;
            Delet(v);
        }
    }
    
    int main()
    {
        int T;
        int x;
        scanf("%d",&T);
        while(T--)
        {
            while(!qu.empty())
                qu.pop();
            //ceshi();
    
            memset(vis1,0,sizeof(vis1));
    
            memset(head,-1,sizeof(head));
            tol=0;
    
            kt.clear();//深度啊
            kt[1]=1;
            node now;
            now.x=1;
            now.y=kt[1];
            qu.push(now);
            vis1[1]=1;
    
            int num=2;
    
            scanf("%d",&n);
            for(int i=1; i<=n; i++)
            {
                scanf("%d",&x);
                if(x<0)
                {
                    x=-x;
                    Delet(x);
                }
                else
                {
                    add(x,num);
                    vis1[num]=1;
                    num++;
                }
                while(!qu.empty())
                {
                    node now=qu.top();
                    if(!vis1[now.x])
                        qu.pop();
                    else
                    {
                        printf("%d
    ",now.x);
                        break;
                    }
                }
            }
        }
        return 0;
    }
    
    



  • 相关阅读:
    简明Python3教程 12.问题解决
    简明Python3教程 11.数据结构
    【SPOJ 694】Distinct Substrings
    【codeforces Manthan, Codefest 17 C】Helga Hufflepuff's Cup
    【CF Manthan, Codefest 17 B】Marvolo Gaunt's Ring
    【CF Manthan, Codefest 17 A】Tom Riddle's Diary
    【SPOJ 220】 PHRASES
    【POJ 3261】Milk Patterns
    【POJ 3294】Life Forms
    【POJ 1226】Substrings
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777478.html
Copyright © 2011-2022 走看看