算法学习:图论

嗯..把之前的坑填了吧.(就算)

图论..就从最基础的存图开始吧

先上一波代码

//#define fre yes

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 100005;
bool Vis[maxn];
int head[maxn];
int ver[maxn];
int to[maxn];

int n,m;

template<typename T>inline void read(T&x)
{
    x = 0;char c;int lenp = 1;
    do { c = getchar();if(c == '-') lenp = -1; } while(!isdigit(c));
    do { x = x * 10 + c - '0';c = getchar(); } while(isdigit(c));
    x *= lenp;
}

void addedge(int x,int y)
{
    ver[tot] = y;
    to[tot] = head[x];
    head[x] = tot++;
}

void kre(int x)
{
    for (int i=head[x];~i;i=to[i])
    {
        int y = ver[i];//下一条边
        if(Vis[y]) kre(y);
    }
}

int main()
{
    memset(head,-1,sizeof(head));

    read(n);read(m);
    for (int i=1;i<=m;i++)
    {
        int x,y;
        addedge(x,y);
    }
    //遍历每一条边
    for (int i=1;i<=n;i++) kre(i);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Steinway/p/9209393.html