[CCF CSP]201903-4 消息传递接口

#include<bits/stdc++.h>
using namespace std;
#define mp(a,b) make_pair(a,b)
const int N=1e4+10;
int T,n;
int vis[8*N];
map<pair<int,int>,int> myMap;
struct Node{
    int type;
    int x;
    int from;
    int next;
    int num;
}node[8*N];
queue<Node> que;
queue<int> q1[N<<1];
char str[30];
int cnt,tot;
void init()
{
    cnt=0;
    myMap.clear();
    memset(vis,0,sizeof(vis));
    while(!que.empty()) que.pop();
    for(int i=1;i<=2*n;i++) while(!q1[i].empty()) q1[i].pop();
    tot=0;
}
void push_next(int u)
{
    int v=node[u].next;
    if(v==-1) return;
    que.push(node[v]);
    Node t=node[v];
    if(!myMap[mp(t.type,t.x)]) myMap[mp(t.type,t.x)]=++tot;
    int k=myMap[mp(t.type,t.x)];
    q1[k].push(t.num);
}
void print(Node t)
{
    if(t.type==0) cout<<'R';
    else cout<<'S';
    cout<<t.x<<' '<<t.from<<' '<<t.num<<' '<<t.next<<endl;
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>T>>n;
    while(T--)
    {
        init();
        for(int i=1;i<=n;i++)
        {
            gets(str);
            int len=strlen(str);
            int first=1;
            for(int j=0;j<len;j++)
            {
                if(!isalpha(str[j])) continue;
                if(str[j]=='R')
                {
                    node[++cnt].type=0;
                    node[cnt].x=str[j+1]-'0';
                    node[cnt].from=i-1;
                    node[cnt].next=cnt+1;
                    node[cnt].num=cnt;
                }
                if(str[j]=='S')
                {
                    node[++cnt].type=1;
                    node[cnt].x=str[j+1]-'0';
                    node[cnt].from=i-1;
                    node[cnt].next=cnt+1;
                    node[cnt].num=cnt;
                }
                Node t=node[cnt];
                if(first)
                {
                    if(!myMap[mp(t.type,t.x)]) myMap[mp(t.type,t.x)]=++tot;
                    int k=myMap[mp(t.type,t.x)];
                    que.push(t);
                    q1[k].push(t.num);
                    //cout<<t.num<<' '<<' '<<k<<' '<<q1[k].front()<<endl;
                    first=0;
                }
              //  print(t);
            }
            node[cnt].next=-1;
        }
        int w=0;
        while(!que.empty())
        {
            bool flag=false;
            Node cur=que.front();
            //cout<<cur.num<<endl;
            if(!myMap[mp(cur.type,cur.x)]) myMap[mp(cur.type,cur.x)]=++tot;
            int k1=myMap[mp(cur.type,cur.x)];
            if(vis[cur.num]) {
                que.pop();
                continue;
            }
            int cor_type=cur.type^1;
            int cor_x=cur.from;
            int k2=myMap[mp(cor_type,cor_x)];
            if(q1[k2].empty())
            {
                que.pop();
                que.push(cur);
                w++;
                //cout<<w<<endl;
                if(w==n) break;
                else continue;
            }
            else w=0;
            int p=q1[k2].front();
            vis[p]=vis[cur.num]=1;
            q1[k2].pop();
            q1[k1].pop();
            que.pop();
            //cout<<cur.num<<' '<<p<<endl;
            push_next(cur.num);
            push_next(p);
        }
        if(que.empty()) cout<<0<<endl;
        else cout<<1<<endl;
    }
    return 0;
}
/*
3 2
R1 S1
S0 R0
R1 S1
R0 S0
R1 R1 R1 R1 S1 S1 S1 S1
S0 S0 S0 S0 R0 R0 R0 R0

2 3
R1 S1
R2 S0 R0 S2
S1 R1
R1
R2 S0 R0
S1 R1
*/
#include<bits/stdc++.h>
using namespace std;

#define mp(a,b) make_pair(a,b)
const int max_node_size=8e4+10;
const int max_string_length=30;
const int N=1e4+10;
int T,n,n_cnt,q1_cnt;
int vis[max_node_size];
char str[max_string_length];
struct Node{
    int type,x,from,next,num;
}node[max_node_size];

map<pair<int,int>,int> myMap;
queue<int> q1[2*N];
queue<Node> que;

void init()
{
    n_cnt=q1_cnt=0;
    myMap.clear();
    memset(vis,0,sizeof(vis));
    while(!que.empty()) que.pop();
    for(int i=1;i<=2*n;i++) while(!q1[i].empty()) q1[i].pop();
}

int get_q1ID(Node cur)
{
     if(!myMap[mp(cur.type,cur.x)]) myMap[mp(cur.type,cur.x)]=++q1_cnt;
     int k1=myMap[mp(cur.type,cur.x)];
     return k1;
}

int get_q1ID(int type,int x)
{
    if(!myMap[mp(type,x)]) myMap[mp(type,x)]=++q1_cnt;
    int k2=myMap[mp(type,x)];
    return k2;
}

void push_next(int u)
{
    int v=node[u].next;
    if(v==-1) return;
    que.push(node[v]);
    Node t=node[v];
    int k=get_q1ID(t);
    q1[k].push(t.num);
}

void creat_node(int k,int i,int j)
{
    node[++n_cnt].type=k;
    node[n_cnt].x=str[j+1]-'0';
    node[n_cnt].from=i-1;
    node[n_cnt].next=n_cnt+1;
    node[n_cnt].num=n_cnt;
}

int work()
{
    int w=0;
    while(!que.empty())
    {
        int flag=1;
        Node cur=que.front();
        if(vis[cur.num]) {que.pop(); continue;}
        int k1=get_q1ID(cur);
        int k2=get_q1ID(cur.type^1,cur.from);
        if(q1[k2].empty()) flag=0;
        else{
            int ok=0;
            for(int i=1;i<=q1[k2].size();i++)
            {
                int tmp=q1[k2].front();
                if(node[tmp].from==cur.x) {
                    ok=1;
                    break;
                }
                else {
                    q1[k2].pop();
                    q1[k2].push(tmp);
                }
            }
            if(ok==0) flag=0;
        }
        if(flag==0)
        {
            que.pop();que.push(cur);w++;
            if(w==n) break;
            continue;
        }
        else w=0;
        int p=q1[k2].front();
        vis[p]=vis[cur.num]=1;
        q1[k1].pop();
        q1[k2].pop();
        que.pop();
        push_next(cur.num);
        push_next(p);
        //cout<<cur.num<<' '<<p<<endl;
    }
    if(que.empty()) return 0;
    return 1;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>T>>n;
    while(T--)
    {
        init();
        for(int i=1;i<=n;i++)
        {
            gets(str);
            int len=strlen(str);
            for(int j=0;j<len;j++)
            {
                if(!isalpha(str[j])) continue;
                if(str[j]=='R') creat_node(0,i,j);
                else if(str[j]=='S') creat_node(1,i,j);
                Node t=node[n_cnt];
                if(j==0){
                    que.push(t);
                    int k=get_q1ID(t);
                    q1[k].push(t.num);
                }
            }
            node[n_cnt].next=-1;
        }
        int ans=work();
        cout<<ans<<endl;
    }
    return 0;
}

/*
1 4
R2 R1 R1 S2 R3
S2 S0 S0
S0 R1 R0
S0

3 2
R1 S1
S0 R0
R1 S1
R0 S0
R1 R1 R1 R1 S1 S1 S1 S1
S0 S0 S0 S0 R0 R0 R0 R0

2 3
R1 S1
R2 S0 R0 S2
S1 R1
R1
R2 S0 R0
S1 R1
*/
原文地址:https://www.cnblogs.com/Andrew-aq/p/12527369.html