CCF——201903-4消息传递接口 90分

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10010;
struct Node{
    char op;
    int from, to;
};
bool dis[maxn];
int  S_v[maxn];
vector<queue<Node> >vec;
int T, n;
void make1(string s, int v_id)
{
    int len = s.size();
    int i = 0, j = 0;
    while(1){
        if(i >= len) break;
        string s1 = "";
        for(i, j = 0; i < len && s[i] != ' '; i++, j++){
            s1[j] = s[i];
        }
        i++;
        s1[j] = '\0';
        Node node;
        node.op = s1[0];
        int num = 0;
        for(int i = 1; s1[i] != '\0'; i++){
            num *= 10;
            num = num + (s1[i]-'0');
        }
        node.to = num+1;
        node.from = v_id+1;
        vec[v_id+1].push(node);
    }
}



bool solve()
{
    queue<Node> r_q;
//    while(!r_q.empty()){
//        r_q.pop();
//    }
    while(1){
        bool flag = true;
        bool flag1;
        while(1){
            flag1 = false;
            for(int i = 1; i <= n; i++){
                if(!dis[i]&& !vec[i].empty()){
                    flag1 = true;  //进程队列中有消息进入消息队列
                    Node node = vec[i].front();
                    if(node.op == 'R'){
                        vec[i].pop();
                        if(S_v[node.to] == node.from)   //能够立马消掉
                            vec[node.to].pop(), dis[node.to] = 0;
                        else{
                            r_q.push(node);             //不能够立马消掉
                            dis[i]++;
                        }
                    }
                    else {
                        S_v[node.from] = node.to;
                        dis[i] = 1;
                    }
                }
            }
            if(!flag1) break;
            }
            for(int i = 1; i <= n; i++){
                if(!vec[i].empty())   // 判断进程队列里面还有没有
                    flag = false;
            }
            if(flag && r_q.empty())   //如果进程队列和消息队列全都空了未死锁
                return true;
           //进程队列动不了了
            bool flag2 = false;
            if(r_q.empty())
                return false;     //如果进程队列没有东西进来就死锁了
            queue<Node> r_q1;
            while(!r_q.empty()){
                Node node1 = r_q.front();
                r_q.pop();
                if(S_v[node1.to] == node1.from){ //匹配成功
                    flag2 = true;
                    dis[node1.from] = 0;
                    dis[node1.to] = 0;
                    S_v[node1.to] = 0;
                    vec[node1.to].pop();
                }
                else{
                    r_q1.push(node1);
                }
            }
            if(flag && !r_q1.empty())  //进程队列为空但消息队列不为空
                return false;
            r_q = r_q1;
            if(!flag1 && !flag2)
                 return false;

    }
}


int main()
{
    scanf("%d %d", &T, &n);
    getchar();
    while(T--){
        memset(dis, 0, sizeof(dis));
        memset(S_v, 0, sizeof(S_v));
        vec.clear();
        queue<Node> q;
        for(int i = 0; i <= n; i++)
            vec.push_back(q);
        string s;
        for(int i = 0; i < n; i++){
            getline(cin, s);
            make1(s, i);
        }
        bool flag = solve();
        if(flag)
            printf("0\n");
        else
            printf("1\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/WSS_ang/article/details/103340717