美文网首页
LADA C++记录

LADA C++记录

作者: lucia320 | 来源:发表于2017-12-06 14:34 被阅读2次

有关struct的简单用法

  • typedef 对struct结构体重命名,简化代码
  • 加入构造函数,进行初始化(注:这种情况下智能使用构造函数初始化,而不能再使用花括号)
typedef struct vertex{
    int index, lowlink; 
    bool onStack;
    vertex(){
        index = 0;
        lowlink = 0;
        onStack = false;
    }
    vertex(int i, int l, bool oS) {
        index = i;
        lowlink = l;
        onStack = oS;
    }
}vertex;

自制答案

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <sstream>

using namespace std;

int index = 1;
int LeaderNum = 0;
stack<int> S;

typedef struct vertex{
    int index, lowlink;
    bool onStack;
    vertex(){
        index = 0;
        lowlink = 0;
        onStack = false;
    }
    vertex(int i, int l, bool oS) {
        index = i;
        lowlink = l;
        onStack = oS;
    }
}vertex;

void scc(vector< vector<int> > &Adj, vector<vertex> &Vertex, int v){
    Vertex[v].index = index; 
    Vertex[v].lowlink = index;
    index++;
    S.push(v);
    Vertex[v].onStack = true;
    for (int i = 0; i < Adj[v].size(); i++){
        int u = Adj[v][i];
        if (Vertex[u].index == 0){
            scc(Adj, Vertex, u);
            Vertex[v].lowlink = min(Vertex[v].lowlink, Vertex[u].lowlink);
        }
        else if (Vertex[u].onStack == true){
            Vertex[v].lowlink = min(Vertex[v].lowlink, Vertex[u].lowlink);
        }
    }
    
    if (Vertex[v].index == Vertex[v].lowlink){
        while(1){
            int w = S.top();
            S.pop();
            Vertex[w].onStack = false;
            if ( w == v) break;
        }
        LeaderNum++;
    }
}

int main(){
    int n;
    cin >> n;
    vector< vector<int> > Adj(n);
    vector<vertex> Vertex(n);
    int v, temp;
    while( cin >> v ){
        cin >> temp;
        while(temp!=-1){
            Adj[v].push_back(temp);
            cin >> temp;
        }
    }
    for (int i = 0; i < n; i++){
        if ( Vertex[i].index == 0 ){
            scc(Adj, Vertex, i);    
        }
    }
    cout << LeaderNum;
    return 0;
}

相关文章

  • LADA C++记录

    有关struct的简单用法 typedef 对struct结构体重命名,简化代码 加入构造函数,进行初始化(注:这...

  • simpleperf源码阅读-1.C++

    SimplePerf C++ 承接上文,本文主要记录simpleperf C++部分的代码的阅读笔记。 Main ...

  • FFmpeg视频播放

    首先记录一下C++中的NULL、0、nullptr的区别 NULL在C++中就是0,这是因为在C++中void* ...

  • C++ 总结 (一、基础篇)

    C++ 总结 (一、基础篇) 官网 C++ 完全兼容C语言,但是有自己的语法特点,本文总结了C++的基础知识。记录...

  • C++记录

    vector 参考[https://www.runoob.com/w3cnote/cpp-vector-conta...

  • 诗游斯里兰卡之康提佛牙寺游记

    诗游斯里兰卡之康提佛牙寺游记 佛牙寺是斯里兰卡著名的佛寺,位于康提湖畔,又称“达拉达·马利戛瓦” (Dālada ...

  • 博覽網:第一週筆記

    仅个人学习记录,毫无参考性 望知悉!!! 零:c++書籍目錄 《the C++ programming langu...

  • C++ Cheat Sheet (keep updating .

    有一阵子没有关注C++了,随着年纪增长记忆力下降,感觉需要一个小抄记录些C++的知识可以随时查阅,碰到什么值得记录...

  • Mac/linux环境下 如何使用 CMakeLists.txt

    本文主要目的是记录 利用CMakeLists.txt编译C++。 1 创建C++工程 1.1 创建工程目录及CMa...

  • C++ Core Guidelines 读书笔记

    前言 C++ 是永远也学不完的语言,最近发现了一个不错的教程 C++ Core Guidelines,希望记录下自...

网友评论

      本文标题:LADA C++记录

      本文链接:https://www.haomeiwen.com/subject/sdakixtx.html