zoukankan      html  css  js  c++  java
  • Codefoces909E Coprocessor(拓扑排序)

    http://codeforces.com/problemset/problem/909/E

    由于分了两个queue,所以push的时候可以统一操作,不会影响彼此。两个queue相当于是平等的,只是q[1]加入计数。

    虽然一开始自己也是拓扑序做的,但是一些细节操作浪费了时间TLE了。

    比如我用了vis数组来判断终止态,其实只要判断pop点的个数就好

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cstdlib>
     6 #include<cmath>
     7 #include<vector>
     8 #include<queue>
     9 #define IO ios::sync_with_stdio(false);cin.tie(0);
    10 const int MOD=1e9+7;
    11 typedef int ll;
    12 using namespace std;
    13 vector<int> vec[100010];
    14 int n, m, indegree[100010], cnt=0, num=0, vis[100010];
    15 int a[100010], x, y, tail=-1;
    16 queue<int> q[2];
    17 void topo()
    18 {
    19     for(int i = 0; i < n; i++){
    20         if(!indegree[i]){
    21             q[a[i]].push(i);
    22         }
    23     }
    24     while(num<n){
    25         if(!q[0].empty())
    26         while(!q[0].empty()){
    27             int t = q[0].front();
    28             q[0].pop();
    29             num++;
    30             tail=t;
    31             for(int i = 0; i < vec[t].size(); i++){
    32                 indegree[vec[t][i]]--;
    33                 if(!indegree[vec[t][i]]){
    34                     q[a[vec[t][i]]].push(vec[t][i]);
    35                 }
    36             }
    37         }
    38         if(!q[1].empty()){
    39             cnt++;
    40             while(!q[1].empty()){
    41                 int t = q[1].front();
    42                 q[1].pop();
    43                 num++;
    44                 tail=t;
    45                 for(int i = 0; i < vec[t].size(); i++){
    46                     indegree[vec[t][i]]--;
    47                     if(!indegree[vec[t][i]]){
    48                         q[a[vec[t][i]]].push(vec[t][i]);
    49                     }
    50                 }
    51             }    
    52         }
    53     }
    54 }
    55 int main()
    56 {
    57     IO;
    58     memset(vis, 0, sizeof(vis));
    59     cin >> n >> m;
    60     for(int i = 0; i < n; i++){
    61         cin >> a[i];
    62     }
    63     for(int i = 0; i < m; i++){
    64         cin >> x >> y; //x<-y
    65         vec[y].push_back(x);
    66         indegree[x]++;
    67     }
    68     topo();
    69     cout << cnt << endl;
    70     return 0;
    71 } 
  • 相关阅读:
    Java批量文件打包下载
    Java Swing
    空白文章
    linux 用户创建、管理、权限分配
    在虚拟机下安装hadoop集成环境(centos7+hadoop-2.6.4+jdk-7u79)
    《转载》POI导出excel日期格式
    java导出生成word(类似简历导出)
    《sqoop实现hdfs中的数据导出至mysql数据库》
    c# winform 自动关闭messagebox 模拟回车
    Ubuntu下启动/重启/停止apache服务器
  • 原文地址:https://www.cnblogs.com/Surprisezang/p/8763275.html
Copyright © 2011-2022 走看看