ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-03-19 23:02:29  阅读:228  来源: 互联网

标签:node cnt const int max vis 消息传递 CCF CSP


#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
*/

 

标签:node,cnt,const,int,max,vis,消息传递,CCF,CSP
来源: https://www.cnblogs.com/Andrew-aq/p/12527369.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有