Chinese college students and computer systems Programming Contest CCF-CCSP-2017 serial schedule (serial)

Serial schedule (Serial)

 

 

In addition equivalent conditions, sets the condition according to the meaning of the questions, and then find the lexicographically smallest topological order.

 

Lite version

#include<bits/stdc++.h>
using namespace std;
const int N=2e4+5;
const int M=2e4+5;
const int E=8e5+5;
template <typename T>
inline void read(T &x){
    T f=1;char ch=getchar();x=0;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    x*=f;
}
vector<int>lastR[N];int lastW[N]; 
bitset<M>d[M];
int n,m,pcnt,qcnt,tot,ind[M],to[E],nxt[E],head[M]; 
inline void add(int x,int y){
    if(!x||x==y) return ;
    ind[y]++;
    to[++tot]=y;nxt[tot]=head[x];head[x]=tot;
}
inline void init(){
    read(n);read(m);read(pcnt);read(qcnt);
    for(int i=1,op,xk,tk;i<=pcnt;i++){
        read(op);read(xk);read(tk);
        if(op){
            if(!lastR[xk].empty()){
                for(auto &i:lastR[xk]) add(i,tk);
                lastR[xk].clear();
            }
            else{
                add(lastW[xk],tk);
            }
            lastW[xk]=tk;
        }
        else{
            add(lastW[xk],tk);
            lastR[xk].push_back(tk);
        }
    }
}
priority_queue<int,vector<int>,greater<int> >q;
inline void topo(){
    d[0]=1;
    for(int i=1;i<=m;i++) d[i]=d[i-1]<<1;
    for(int i=1;i<=m;i++) if(!ind[i]) q.push(i);
    for(int i=1;i<=m;i++){
        int x=q.top();q.pop();
        printf("%d ",x);
        for(int j=head[x];j;j=nxt[j]){
            if(!--ind[to[j]]){
                q.push(to[j]);
            }
            d[to[j]]|=d[x];
        }
    }
    puts("");
    for(int i=qcnt,x,y;i;i--){
        read(x);read(y);
        puts(!d[x].test(y)?"YES":"NO");
    }
}
int main(){
    init();
    topo();
    return 0;
}

 

Pure handwriting bitset

#include<queue> 
#include<vector> 
#include<cstdio> 
using std::vector;
using std::priority_queue;
const int N=2e4+5;
const int M=2e4+5;
const int E=8e5+5;
template <typename T>
inline void read(T &x){
    T f=1;char ch=getchar();x=0;
    while(ch<'0'||ch>'9' ) { IF (CH == ' - ' ) F = - . 1 ; CH = getchar ();}
     the while (CH> = ' 0 ' && CH <= ' . 9 ' ) {X = X * 10 + CH-, ' 0 ' ; CH = getchar ();} 
    X * = F; 
} 
Vector < int > lastR [N]; int lastW [N]; 
 struct BitSet to { // simulate the bitset 
     // unsigned int 32-bit compressed 
     // unsigned Long Long 64 can be compressed 
    const static int N = M / 32 + 5;
    unsigned data[N];
    unsigned& operator[](int a){
        return data[a];
    }
    const unsigned& operator[](int a)const{
        return data[a];
    }
    bool get(int a){
        return (data[a >> 5] >> (a & 31)) & 1;
    }
    void set(int a){
        data[a >> 5] |= 1 << (a & 31);
    }
    void unset(int a){
        data[a >> 5] &= ~(1 << (a & 31));
    }
    BitSet& operator|=(const BitSet& a){
        for(int i = 0; i < N; i++)
            data[i] |= a[i];
        return *this;
    }
}used[M];
int n,m,pcnt,qcnt,tot,ind[M],to[E],nxt[E],head[M]; 
inline void add(int x,int y){
    if(!x||x==y) return ;
    ind[y]++;
    to[++tot]=y;nxt[tot]=head[x];head[x]=tot;
}
inline void init(){
    read(n);read(m);read(pcnt);read(qcnt);
    for(int i=1,op,xk,tk;i<=pcnt;i++){
        read(op);read(xk);read(tk);
        if(op){
            if(!lastR[xk].empty()){
                for(auto &i:lastR[xk]) add(i,tk);
                lastR[xk].clear();
            }
            else{
                add(lastW[xk],tk);
            }
            lastW[xk]=tk;
        }
        else{
            add(lastW[xk],tk);
            lastR[xk].push_back(tk);
        }
    }
}
priority_queue<int,vector<int>,std::greater<int> >q;
inline void topo(){
    for(int i=1;i<=m;i++) used[i].set(i);
    for(int i=1;i<=m;i++) if(!ind[i]) q.push(i);
    for(int i=1;i<=m;i++){
        int x=q.top();q.pop();
        printf("%d ",x);
        for(int j=head[x];j;j=nxt[j]){
            if(!--ind[to[j]]){
                q.push(to[j]);
            }
            used[to[j]]|=used[x];
        }
    }
    puts("");
    for(int i=qcnt,x,y;i;i--){
        read(x);read(y);
        puts(!used[x].get(y)?"YES":"NO");
    }
}
int main(){
    init();
    topo();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/shenben/p/11622823.html