zoukankan      html  css  js  c++  java
  • 图论——链式前向星的加边和删边操作

    点击展开代码块
    #include <bits/stdc++.h>
    
    #define mkp make_pair
    #define pb push_back
    #define all(x) x.bg,x.ed
    #define newline puts("")
    #define rep(i,n) for(int i=1;i<=n;++i)
    #define rrep(i,n) for(int i=0;i<n;++i)
    
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> pii;
    const int maxn = 1e5+10;
    const int inf = 0x7f7f7f7f;
    const int Mod = 1e9+7;
    const double eps = 1e-7;
    int head[maxn],cnt=0;
    struct edge{
        int u,v,pre,next;
    }e[maxn<<1];
    //加边
    void add(int u,int v){
        e[++cnt].v=v;e[cnt].u=u;e[head[u]].pre=cnt;
        e[cnt].next=head[u];head[u]=cnt;
    }
    //删边
    void del(int id){//边的编号
        if(e[id].pre){
            e[e[id].pre].next=e[id].next;//类似于链表的删点
            e[e[id].next].pre=e[id].pre;
        }
        else{
            head[e[id].u]=e[id].next;
            e[e[id].next].pre=0;
        }
    }
    int n,m;
    struct node{
        int x,y;
    }p[10010];
    
    int main(){
        cnt=0;
        cin>>n>>m;
        
        for (int i=0;i<m;i++){
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        cout<<"cnt = "<<cnt<<endl;
        for (int u=1;u<=n;u++){
            printf("%d->",u);
            for (int i=head[u];i;i=e[i].next){
                printf("%d ",e[i].v);
            }
            newline;
        }
        del(3);//cnt边的数量不会变化,链式前向星变化,删除第三条边
        // del(1);
        // del(2);
        newline;
        cout<<"cnt = "<<cnt<<endl;
        for (int u=1;u<=n;u++){
            printf("%d->",u);
            for (int i=head[u];i;i=e[i].next){
                printf("%d ",e[i].v);
            }
            newline;
        }
        return 0;
    }
    
    你将不再是道具,而是成为人如其名的人
  • 相关阅读:
    vue自定义指令directive
    vue组件:input数字输入框
    vue中用数组语法绑定class
    vue中检测数组改变
    node绝对和相对模块
    判断拖放
    媒体查询 和rem布局
    JSON字符串对象相互转换
    深度封装typeof判断
    类数组
  • 原文地址:https://www.cnblogs.com/wsl-lld/p/13651165.html
Copyright © 2011-2022 走看看