zoukankan      html  css  js  c++  java
  • 洛谷P1726 上白泽慧音

    求完强联通分量以后本来准备写一个自定义的比较函数或者暴力转成string类啥的来比较字典序.

    后来仔细一想, 求强联通分量的时候不就是按照节点编号从小到大来的吗?

    直接顺着扫一遍不就美滋滋了?

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <stack>
    #include <vector>
    using namespace std;
    const int MAXN = 5e3 + 20;
    
    int N, M;
    vector<int> g[MAXN];
    
    int dfn[MAXN], low[MAXN], col[MAXN];
    vector<int> scc[MAXN]; int scccnt = 0;
    stack<int> sta;
    bool ins[MAXN];
    
    void tarjan(int u)
    {
        static int dfnclock = 0;
        dfn[u] = low[u] = ++dfnclock;
        sta.push(u); ins[u] = true;
        for(int i = 0; i < (int) g[u].size(); i++){
            int &v = g[u][i];
            if(!dfn[v]){
                tarjan(v);
                low[u] = min(low[u], low[v]);
            }
            else if(ins[v]) low[u] = min(low[u], dfn[v]);
        }
        if(dfn[u] == low[u]){
            ++scccnt; int v;
            do{
                v = sta.top(); sta.pop();
                ins[v] = false;
                col[MAXN] = scccnt, scc[scccnt].push_back(v);
            }while(u != v);
        }
    }
    
    int main()
    {
        cin>>N>>M;
        for(int i = 1, u, v, c; i <= M; i++){
            scanf("%d%d%d", &u, &v, &c);
            g[u].push_back(v);
            if(c == 2) g[v].push_back(u);
        }
        for(int i = 1; i <= N; i++) if(!dfn[i]) tarjan(i);
        int siz = 0, ans = 0;
        //for(int i = 1; i <= scccnt; i++) cout<<scc[scccnt].size()<<endl;
        for(int i = 1; i <= scccnt; i++) {
            if((int)scc[i].size() > siz) {
                siz = scc[i].size();
                ans = i;
            }
        }
        cout<<siz<<endl; sort(scc[ans].begin(), scc[ans].end());
        for(int i = 0; i < (int) scc[ans].size(); i++)
            cout<<scc[ans][i]<<" ";
        return 0;
    }
  • 相关阅读:
    Open Live Writer增加代码插件
    WinSCP列出’/’目录项出错
    Ueditor中增加迅雷下载支持
    Ueditor设置默认字体
    PDF编辑、删除、替换某页面或文字
    个人站长如何使用svn发布到服务器不遗漏文件
    PHP 测试程序运行时间 microtime函数用法
    LeetCode---Stack && Heap
    LeetCode---Binary Search
    LeetCode---Hash Table
  • 原文地址:https://www.cnblogs.com/wsmrxc/p/9329492.html
Copyright © 2011-2022 走看看