zoukankan      html  css  js  c++  java
  • Codeforces 909E. Coprocessor (拓扑、模拟)

    题目链接: Coprocessor

    题意:

      给出n个待处理的事件(0 - n-1),再给出了n个标(0表示只能在主处理器中处理这个事件,1表示只能在副处理器中处理这个事件),处理器每次能处理多个任务。每个事件有关联,如果一个任务要在副处理器上执行,那它所依赖的任务要么已执行完了,要么和它一起在这个副处理器上同时执行。问副处理器最少调用多少次。

    题解:

      首先拓扑排序是肯定要的,先处理出入度为0的点,处理的时候先处理所有入度为0且标为0(只能在主处理器上处理)的点,这样保证每处理一次就把能在副处理器上处理的点全都处理了出来。在处理的过程中保存处理出来的入度为0的点,这样就不用暴力找点了。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int MAX_N = 1e5+9;
     4 int res[MAX_N];
     5 vector<int> vec[MAX_N];
     6 int in[MAX_N];
     7 queue<int> que1,que2;
     8 int main()
     9 {
    10     int N,M,T;
    11     while(cin>>N>>M)
    12     {
    13         memset(res,0,sizeof(res));
    14         memset(in,0,sizeof(in));
    15         while(!que1.empty()) que1.pop();
    16         while(!que2.empty()) que2.pop();
    17         for(int i=0;i<MAX_N;i++) vec[i].clear();
    18         for(int i=0;i<N;i++)
    19         {
    20             scanf("%d",&res[i]);
    21         }
    22         for(int i=0;i<M;i++)
    23         {
    24             int a,b;
    25             scanf("%d%d",&a,&b);
    26             vec[b].push_back(a);
    27             in[a] ++;
    28         }
    29         for(int i=0;i<N;i++)
    30         {
    31             if(in[i] == 0)
    32             {
    33                 if(res[i] == 0) que1.push(i);
    34                 else if(res[i] == 1) que2.push(i);
    35             }
    36 
    37         }
    38         int ans = 0;
    39         int cnt = N;
    40         while(cnt > 0)
    41         {
    42             while(!que1.empty())
    43             {
    44 
    45                 int t = que1.front();que1.pop();
    46                 cnt --;
    47                 for(int i=0;i<vec[t].size();i++)
    48                 {
    49                     in[vec[t][i]]--;
    50                     if(in[vec[t][i]] == 0)
    51                     {
    52                         if(res[vec[t][i]] == 1) que2.push(vec[t][i]);
    53                         else que1.push(vec[t][i]);
    54                     }
    55                 }
    56             }
    57             if(!que2.empty()) ans ++;
    58             while(!que2.empty())
    59             {
    60                 int t = que2.front();que2.pop();
    61                 cnt --;
    62                 for(int i=0;i<vec[t].size();i++)
    63                 {
    64                     in[vec[t][i]]--;
    65                     if(in[vec[t][i]] == 0)
    66                     {
    67                         if(res[vec[t][i]] == 1) que2.push(vec[t][i]);
    68                         else que1.push(vec[t][i]);
    69                     }
    70                 }
    71             }
    72         }
    73         cout<<ans<<endl;
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    mysql命令大全(转发)
    算法大神学习之路
    MYSQL之explain的作用与相关知识
    FastDFS(分布式存储系统)+nginx web 服务器
    获取用户浏览历史记录(django_redis)
    用户登陆装饰器和页面跳转(包含mixin的使用)
    .NET 方法回调
    asp.net viewstate 数据过大 导致错误
    asp.net viewstate 数据大导致错误
    软件测试 Record
  • 原文地址:https://www.cnblogs.com/doggod/p/8391587.html
Copyright © 2011-2022 走看看