一本通1515:网络协议

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <unordered_set>
#include <unordered_map>
using namespace std;

int read() {
    int x = 0;
    char c = getchar();

    while (c < '0' || c > '9')
        c = getchar();

    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }

    return x;
}

void dfs(const vector<vector<int>> &Edge, vector<int> &DFN, vector<int> &Low, stack<int> &St,
         unordered_set<int> &map, int x, int &Time, vector<int> &color, int &col, vector<int> &colorVal) {
    St.push(x);
    map.insert(x);
    DFN[x] = Time;
    Low[x] = Time;
    Time++;

    for (int i = 0; i < Edge[x].size(); i++)
        if (DFN[Edge[x][i]] == 0) {
            dfs(Edge, DFN, Low, St, map, Edge[x][i], Time, color, col, colorVal);
            Low[x] = min(Low[x], Low[Edge[x][i]]);
        } else if (map.count(Edge[x][i])) {
            Low[x] = min(Low[x], Low[Edge[x][i]]);
        }

    if (DFN[x] == Low[x]) {
        int sum = 0;

        while (St.top() != x) {
            map.erase(St.top());
            color[St.top()] = col;
            sum++;
            St.pop();
        }

        map.erase(St.top());
        colorVal.push_back(sum + 1);
        color[St.top()] = col++;
        St.pop();
    }
}

vector<vector<int>> Tarjan(const vector<vector<int>> &Edge, int n, int &col, vector<int> &colorVal) {
    vector<int>DFN(n + 1), Low(n + 1), Color(n + 1);
    unordered_set<int>map;
    stack<int>St;
    int Time = 1;

    for (int i = 1; i <= n; i++)
        if (!DFN[i])
            dfs(Edge, DFN, Low, St, map, i, Time, Color, col, colorVal);

    vector<vector<int>>newEdge(n + 1);

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < Edge[i].size(); j++) {
            if (Color[i] != Color[Edge[i][j]])
                newEdge[Color[i]].push_back(Color[Edge[i][j]]);
        }
    }

    return newEdge;
}

void topologicalSort(const vector<vector<int>> &Edge, int n, vector<int>colorVal, int &planA, int &planB) {
    unordered_map<int, int>indegree, outdegree;

    for (int i = 1; i <= n; i++) {
        indegree[i] = 0;
        outdegree[i] = 0;
    }

    queue<int>Qu;

    for (int i = 1; i <= n; i++) {
        outdegree[i] = Edge[i].size();

        for (int j = 0; j < Edge[i].size(); j++)
            indegree[Edge[i][j]]++;
    }

    for (int i = 1; i <= n; i++)
        if (indegree[i] == 0) {
            Qu.push(i);
            planA++;
        }

    int inB = 0, outB = 0;

    while (!Qu.empty()) {
        int Len = Qu.size();
        int selected = Qu.front();
        Qu.pop();

        if (outdegree[selected] == 0)
            outB++;

        for (int j = 0; j < Edge[selected].size(); j++) {
            indegree[Edge[selected][j]]--;

            if (!indegree[Edge[selected][j]])
                Qu.push(Edge[selected][j]);
        }
    }

    planB = max(planA, outB);
}

int main() {
    int n = read();
    vector<vector<int>>testEdge(n + 1);

    for (int i = 1; i <= n; i++) {
        int v = read();

        while (v != 0) {
            testEdge[i].push_back(v);
            v = read();
        }
    }

    int col = 1;
    vector<int>colorVal(1);
    vector<vector<int>>newEdge = Tarjan(testEdge, n, col, colorVal);

    for (int i = 1; i < col; i++) {
        unordered_set<int>tmp;

        for (int j = 0; j < newEdge[i].size(); j++) {
            if (tmp.count(newEdge[i][j])) {
                newEdge[i].erase(newEdge[i].begin() + j);
                j--;
            } else
                tmp.insert(newEdge[i][j]);
        }
    }

    int planA = 0, planB = 0;

    if (col == 2) {
        printf("1\n0");
        return 0;
    }

    topologicalSort(newEdge, col - 1, colorVal, planA, planB);
    printf("%d\n%d", planA, planB);
    return 0;
}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值