CCF CSP 点亮数字人生

1 篇文章 0 订阅

原题地址

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
struct Tool{
    string name;//器件名
    int onum=0;//入度
    vector<int> in;//来自标准输入
    vector<int> oin;//来自其他器件输出的输入
    vector<int> outdegree;//输出到的器件
};
int Q,M,N,flag;//flag为0时表示成环
void process(vector<int>&Out,vector<int>&In,Tool op,int index);
void topo(vector<Tool>& a,vector<int>& ord);
main()
{
    ios::sync_with_stdio(0);//读入优化
    cin >> Q;
    while(Q--)
    {
        flag = 1;
        cin >> M >> N;
        vector<Tool> a;
        vector<int> ord;//存拓扑排序结果
        vector<int> In;//外部输入
        vector<int> Out;//输出
        vector<vector<int>> ans;//存输出答案
        a.resize(N);
        Out.resize(N);
        In.resize(M);
        for (int i = 0; i < N;i++)
        {
            string name;
            int k;
            string in;
            cin >> name >> k;
            a[i].name = name;
            for (int j = 0; j < k;j++)
            {
                cin >> in;
                if(in[0]=='I')
                {
                    in = in.substr(1);
                    int t = stoi(in)-1;
                    a[i].in.push_back(t);
                }
                else
                {
                    in = in.substr(1);
                    int t = stoi(in)-1;
                    a[t].outdegree.push_back(i);
                    a[i].oin.push_back(t);
                    a[i].onum++;
                }
            }   
        }
        topo(a,ord);
        int S;
        cin >> S;
        for (int i = 0; i < S; i++)
        {
            if (flag&&ord.size()<size_t(N))
            {
                cout << "LOOP" << endl;
                flag=0;
            }
            for (int j = 0; j < M; j++)
            {
                cin >> In[j];
            }
            for (auto j : ord)
            {
                process(Out,In,a[j], j);
            }
            ans.push_back(Out);
        }
        for (int i = 0; i < S;i++)
        {
            int n;
            cin >> n;
            for (int j = 0; j < n;j++)
            {
                int num;
                cin >> num;
                if(flag)
                cout << ans[i][num-1] << " ";
            }
            if(flag)
            cout << endl;
        }
    }
    
}
void process(vector<int>&Out,vector<int>&In,Tool op,int index)
{
    if(op.name=="NOT")
    {
        if(!op.in.empty())
        {
            Out[index] = 1-In[op.in[0]];
        }
        else
        {
            Out[index] = 1-Out[op.oin[0]];
        }
    }
    else if(op.name=="AND")
    {
        Out[index] = 1;
        for(auto i:op.in)
        {
            Out[index] &= In[i];
        }
        for(auto i:op.oin)
        {
            Out[index] &= Out[i];
        }
    }
    else if(op.name=="OR")
    {
        Out[index] = 0;
        for(auto i:op.in)
        {
            Out[index] |= In[i];
        }
        for(auto i:op.oin)
        {
            Out[index] |= Out[i];
        }
    }
    else if(op.name=="XOR")
    {
        Out[index] = 0;
        for(auto i:op.in)
        {
            Out[index] ^= In[i];
        }
        for(auto i:op.oin)
        {
            Out[index] ^= Out[i];
        }
    }
    else if(op.name=="NAND")
    {
        Out[index] = 1;
        for(auto i:op.in)
        {
            Out[index] &= In[i];
        }
        for(auto i:op.oin)
        {
            Out[index] &= Out[i];
        }
        Out[index] = 1 - Out[index];
    }
    else if(op.name=="NOR")
    {
        Out[index] = 0;
        for(auto i:op.in)
        {
            Out[index] |= In[i];
        }
        for(auto i:op.oin)
        {
            Out[index] |= Out[i];
        }
        Out[index] = 1 - Out[index];
    }
}
void topo(vector<Tool>& a,vector<int>& ord)
{
    queue<int> q;
    for (int i = 0; i < N;i++)
    {
        if(!a[i].onum)
        {
            q.push(i);
            ord.push_back(i);
        }     
    }
    while(!q.empty())
    {
        int top = q.front();
        q.pop();
        ord.push_back(top);
        for(auto i:a[top].outdegree)
        {
            a[i].onum--;
            if(!a[i].onum)
            {
                q.push(i);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值