【Java全模板】必知的经典算法模板

二分

import java.util.*;
public class Main{
    public static void main(String []args){
        Scanner in  = new Scanner(System.in);
        int n = in.nextInt();
        int q=in.nextInt();
        int a[]=new int[100010];
        for(int i=0;i<n;i++)a[i]=in.nextInt();
        while(q-->0){
            int x=in.nextInt();
            int l=0,r=n-1;
            while(l<r){
                int mid=l+r>>1;
                if(a[mid]>=x){
                    r=mid;
                }
                else{
                    l=mid+1;
                }
            }
                if(a[l]!=x){
                    System.out.println("-1 -1");
            }
            else{
                System.out.print(l+" ");
                l=0;r=n-1;
                while(l<r){
                int mid=l+r+1>>1;
                if(a[mid]<=x)l=mid;
                else r=mid-1;
                }
                System.out.println(l);
            }

        }


    }

}

双指针

//对一段序列用两个指针实现同步的移动

import java.util.*;
public class Main{
    static int N=100010;;
    static int a[]=new int [N];
    static int s[]=new int [N];
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int res=0;
        for(int i=0;i<n;i++)a[i]=sc.nextInt();
        for(int i=0,j=0;i<n;i++){
            s[a[i]]++;
            while(j<=i&&s[a[i]]>1){
                s[a[j]]--;
                j++;
            }
            res=Math.max(res,i-j+1);

        }
        System.out.println(res);


    }

}

前缀和:

模板1

import java.util.*;
public class Main{
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int s[]=new int[n+10];
        int a[]=new int[n+10];
        for(int i=1;i<=n;i++){
            a[i]=sc.nextInt();
        }
        for(int i=1;i<=n;i++){
            s[i]+=s[i-1]+a[i];
        }
        while(m-->0){
            int l=sc.nextInt();
            int r=sc.nextInt();
            System.out.println(s[r]-s[l-1]);
        }
    }
}

//

模板2

import java.util.*;
public class Main{
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        //int a[]=new int[n+10];
        int s[]=new int[n+10];
        for(int i=1;i<=n;i++){
            s[i]=sc.nextInt();
        }
        for(int i=1;i<=n;i++){
            s[i]+=s[i-1];
        }
        while(m-->0){
            int l=sc.nextInt();
            int r=sc.nextInt();
            System.out.println(s[r]-s[l-1]);
        }
    }
}

子矩阵的和:

对于每个询问输出子矩阵中所有数(值)的和

import java.util.*;
public class Main{
        static int N=1010;
        static int a[][]=new int[N][N];
        static int s[][]=new int[N][N];
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int q=sc.nextInt();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                a[i][j]=sc.nextInt();
            }
        }
        //预处理前缀和
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i][j];
            }
        }
        while(q-->0){
            int x1=sc.nextInt();
            int y1=sc.nextInt();
            int x2=sc.nextInt();
            int y2=sc.nextInt();
            System.out.println(s[x2][y2]-s[x2][y1-1]-s[x1-1][y2]+s[x1-1][y1-1]);
        }
    }
}

差分模板

import java.util.*;
public class Main{
    static int N=100010;
    static int b[]=new int[N];
    static int a[]=new int[N];
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int m=in.nextInt();
        for(int i=1;i<=n;i++)
        {
         a[i]=in.nextInt();
         insert(i,i,a[i]);
        }
        while(m-->0){
            int l= in.nextInt();
            int r=in.nextInt();
            int c=in.nextInt();
            insert(l,r,c);
        }
        for(int i=1;i<=n;i++){
            a[i]=a[i-1]+b[i];
            System.out.print(a[i]+" ");
        }
    }
    public static void insert(int l,int r,int c){
        b[l]+=c;
        b[r+1]-=c;
    }
}

差分矩阵 矩阵内数字加上C

import java.util.*;
public class Main{
    static int N=1010;
    static int b[][]=new int[N][N];
    static int a[][]=new int[N][N];
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        int q=sc.nextInt();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                a[i][j]=sc.nextInt();
                insert(i,j,i,j,a[i][j]);
            }
        }
        while(q-->0){
            int x1=sc.nextInt();
            int y1=sc.nextInt();
            int x2=sc.nextInt();
            int y2=sc.nextInt();
            int c=sc.nextInt();
            insert(x1,y1,x2,y2,c);
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+b[i][j];
                System.out.print(a[i][j]+" ");
            }
            System.out.println();
        }


    }
    public static void insert(int x1,int y1,int x2,int y2,int c){
        b[x1][y1]+=c;
        b[x1][y2+1]-=c;
        b[x2+1][y1]-=c;
        b[x2+1][y2+1]+=c;
    }
}

归并排序:

import java.util.*;
public class Main{
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int q[] = new int [n];
        for(int i=0;i<n;i++){
            q[i]=in.nextInt();
        }
        mergeSort(q,0,n-1);
        for(int i=0;i<n;i++){
            System.out.print(q[i]+" ");
        }
    }
    public static void mergeSort(int q[],int l,int r ){
        if(l>=r)return;
        int mid = (l+r)/2;
        mergeSort(q,l,mid);
        mergeSort(q,mid+1,r);
        int temp[] = new int[r-l+1];
        int k=0,i=l,j=mid+1;
        while(i<=mid&&j<=r)
        if(q[i]<=q[j]&&j<=r)
        temp[k++]=q[i++];
        else
        temp[k++]=q[j++];
        while(i<=mid)
        temp[k++]=q[i++];
        while(j<=r)
        temp[k++]=q[j++];
        for(int a=l,b=0;a<=r;a++,b++){
            q[a]=temp[b];
        }
    }
}

二进制中1的个数

import java.util.*;
public class Main{
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0){
            int x=sc.nextInt();
            int sum=0;
            while(x!=0){
                x-=lowbit(x);
                sum++;
            }
            System.out.print(sum+" ");
        }
    }
    public static int lowbit(int x){
        return x&-x;
    }
}

并查集

import java.util.*;
public class Main{
    static int N=100010;
    static int p[]=new int[N];
    public static int find(int x){
        if(x!=p[x])p[x]=find(p[x]);
        return p[x];

    }
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        for(int i=1;i<=n;i++)p[i]=i;
        while(m-->0){
            String s=sc.next();
            if(s.equals("M")){
                int a=sc.nextInt();
                int b=sc.nextInt();
                if(find(a)!=find(b)){
                    p[find(a)]=find(b);
                }
            }
            else{
                int a=sc.nextInt();
                int b=sc.nextInt();
                if(find(a)==find(b))System.out.println("Yes");
                else System.out.println("No");
            }
        }
    }
}

0-1背包

import java.util.*;
public class Main{
    static int N=1010;
    static int w[]=new int[N];
    static int V[]=new int[N];
    static int f[][]=new int[N][N];
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int m=in.nextInt();
        for(int i=1;i<=n;i++){
            V[i]=in.nextInt();
            w[i]=in.nextInt();
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<=m;j++){
                f[i][j]=f[i-1][j];
                if(j>=V[i])f[i][j]=Math.max(f[i][j],f[i-1][j-V[i]]+w[i]);
            }
        }
        System.out.println(f[n][m]);

    }
}

完全背包问题

import java.util.*;
public class Main{
    static int N=1010;
    static int f[][]=new int [N][N];
    static int v[]=new int[N];
    static int w[]=new int[N];
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        for(int i=1;i<=n;i++){
            v[i]=sc.nextInt();
            w[i]=sc.nextInt();
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                f[i][j]=f[i-1][j];
                if(j>=v[i])f[i][j]=Math.max(f[i][j],f[i][j-v[i]]+w[i]);
            }
        }
        System.out.println(f[n][m]);
    }
}

多重背包

//一件物品选k次
//最多选s[i]次,体积不超过j

import java.util.*;
public class Main{
    static int N=110;
    static int f[][]=new int[N][N];
    static int v[]=new int[N];
    static int w[]=new int[N];
    static int s[]=new int[N];
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        for(int i=1;i<=n;i++){
            v[i]=sc.nextInt();
            w[i]=sc.nextInt();
            s[i]=sc.nextInt();
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<=m;j++){
                for(int k=0;k<=s[i]&&k*v[i]<=j;k++){
                    f[i][j]=Math.max(f[i][j],f[i-1][j-k*v[i]]+k*w[i]);
                }
            }
        }
    System.out.println(f[n][m]);    
    }
}

分组背包

import java.util.*;
public class Main{
    static int N=1010;
    static int s[]=new int[N];
    static int f[][]=new int[N][N];
    static int w[][]=new int[N][N];
    static int v[][]=new int[N][N];
    public static  void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        for(int i=1;i<=n;i++){
            s[i]=sc.nextInt();
            for(int j=1;j<=s[i];j++){
                v[i][j]=sc.nextInt();
                w[i][j]=sc.nextInt();
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=0;j<=m;j++){
                f[i][j]=f[i-1][j];
                for(int k=1;k<=s[i];k++){
                    if(j>=v[i][k])f[i][j]=Math.max(f[i][j],f[i-1][j-v[i][k]]+w[i][k]);
                }
            }
        }
        System.out.println(f[n][m]);

    }
}

线性DP: 数字三角形

import java.util.*;
public class Main{
    static int N=510;
    static int a[][]=new int[N][N];
    static int f[][]=new int[N][N];
    static int INF=0x3f3f3f3f;
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=i;j++){
                a[i][j]=sc.nextInt();
            }
        }
        //0和i+1都会被走到需要初始化
        for(int i=0;i<=n;i++){
            for(int j=0;j<=i+1;j++){
                f[i][j]=-INF;

            }
        }
        f[1][1]=a[1][1];
        //第1层已初始化
        //遍历第2层到第n层的三角形的所有点
        //直至最后一层遍历完毕
        for(int i=2;i<=n;i++){
            for(int j=1;j<=i;j++){
                f[i][j]=Math.max(f[i-1][j-1]+a[i][j],f[i-1][j]+a[i][j]);
            }
        }
        long res=f[n][1];
        //先定义为最大,比他大的可以被筛出
        //没有比他大的,则它最大
        for(int i=1;i<=n;i++){
            res=Math.max(f[n][i],res);
        }

        System.out.println(res);
    }
}

线性DP: 最长上升子序列

import java.util.*;
public class Main{
    static int N=1010;
    static int a[]=new int[N];
    static int q[]=new int[N];
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        for(int i=0;i<n;i++)a[i]=sc.nextInt();
        int len=0;
        for(int i=0;i<n;i++){
        int l=0,r=len;
        while(l<r){
            int mid=l+r+1>>1;
            if(q[mid]<a[i])l=mid;
            else r=mid-1;
        }
        len=Math.max(len,r+1);
        q[r+1]=a[i];
        }
        System.out.println(len);
    }
}

区间DP

import java.util.*;
public class Main{
    static int N=310;
    static int f[][]=new int[N][N];
    static int s[]=new int[N];
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        for(int i=1;i<=n;i++)s[i]=in.nextInt();
        for(int i=1;i<=n;i++)s[i]+=s[i-1];
        for(int len=2;len<=n;len++){
            for(int i=1;i+len-1<=n;i++){
            int l=i,r=i+len-1;
            f[l][r]=0x3f3f3f3f;
            for(int k=l;k<r;k++){
                f[l][r]=Math.min(f[l][r],f[l][k]+f[k+1][r]+s[r]-s[l-1]);
            }
        }
        }
        System.out.println(f[1][n]);
    }
}

排列数字DFS

import java.util.*;
public class Main{
    static int n;
    static boolean st[]=new boolean[10];
    static int a[]=new int[10];
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        dfs(0);
    }
    public static void dfs(int u){
        if(u==n){
            for(int i=0;i<n;i++){
                System.out.print(a[i]+" ");
            }
            System.out.println();
        }
        for(int i=1;i<=n;i++){
            if(!st[i]){
                a[u]=i;
                st[i]=true;
                dfs(u+1);
                st[i]=false;
            }
        }
    }
}

BFS 走迷宫

import java.util.*;
public class Main{
    static int N=110;
    static int n,m;
    static PII q[]=new PII[N*N];
    static int d[][]=new int[N][N];
    static int dx[]={0,1,-1,0};
    static int dy[]={1,0,0,-1};
    static int g[][]=new int[N][N];
    static int  hh,tt;
    public static int bfs(){
        hh=0;
        tt=-1;
        q[++tt]=new PII(0,0);
        d[0][0]=0;
        while(hh<=tt){
            PII t=q[hh++];
            for(int i=0;i<4;i++){
            int a=t.x+dx[i];
            int b=t.y+dy[i];
            if(a>=0&&a<=n&&b>=0&&b<=m&&d[a][b]==-1&&g[a][b]==0){
                d[a][b]=d[t.x][t.y]+1;
                q[++tt]=new PII(a,b);

            }
            }


        }
        return d[n-1][m-1];


    }
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        m=sc.nextInt();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                g[i][j]=sc.nextInt();
                d[i][j]=-1;
            }
        }
        System.out.println(bfs());
    }


}
class PII{
    int x;
    int y;
    public PII(int x,int y){
        this.x=x;
        this.y=y;
    }
}

全球变暖 BFS

import java.util.*;
public class Main{
    static int N=1010;
    static boolean st[][]=new boolean[N][N];
    static char g[][]=new char[N][N];
    static int n;
    static int dx[]={0,1,0,-1};
    static int dy[]={1,0,-1,0};
    public static boolean bfs(int x,int y){
        Queue<PII>q=new LinkedList<PII>();
        q.add(new PII(x,y));
        st[x][y]=true;
        int cnt=0;
        int bound=0;
        while(!q.isEmpty()){
            PII t=q.poll();
            cnt++;
            boolean isbound=false;
            for(int i=0;i<4;i++){
                int a=t.x+dx[i];
                int b=t.y+dy[i];
                if(a<0||a>=n||b<0||b>=n)continue;
                if(st[a][b])continue;
                if(!st[a][b]&&g[a][b]=='.'){
                    isbound=true;
                    continue;
                }
                q.add(new PII(a,b));
                st[a][b]=true;

            }
            if(isbound)bound++;
        }
        return bound==cnt;
    }
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        for(int i=0;i<n;i++){
            char a[]=sc.next().toCharArray();
            for(int j=0;j<a.length;j++){
                g[i][j]=a[j];
            }
        }
        int res=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(!st[i][j]&&g[i][j]=='#'){
                    if(bfs(i,j))res++;
                }
            }
        }
    System.out.println(res);
    }
}
class PII{
    int x;
    int y;
    public PII(int x,int y)
    {
        this.x=x;
        this.y=y;

    }
}

spfa求最短路

import java.util.*;
public class Main{
    static int N=100010,INF=0x3f3f3f3f;
    static int e[]=new int[N];
    static int ne[]=new int[N];
    static int w[]=new int[N];
    static int h[]=new int[N];
    static int dist[]=new int[N];
    static boolean st[]=new boolean[N];
    static int n,m,idx;
    public static void add(int a,int b,int c){
        e[idx]=b;
        w[idx]=c;
        ne[idx]=h[a];
        h[a]=idx;
        idx++;
    }
   public static void spfa(){
       Queue<Integer>q=new LinkedList<Integer>();
       Arrays.fill(dist,INF);
       q.offer(1);
       st[1]=true;
       dist[1]=0;
       while(!q.isEmpty()){
           int t=q.poll();
           st[t]=false;
           //表示不在队列中,避免重复元素的查找
       for(int i=h[t];i!=-1;i=ne[i]){
           int j=e[i];
           if(dist[j]>dist[t]+w[i]){
               dist[j]=dist[t]+w[i];
               if(!st[j]){
                   q.offer(j);
                   st[j]=true;
                   //表示在队列中
               }
           }
       }
   }
   if(dist[n]==INF)System.out.println("impossible");
   else System.out.println(dist[n]);
   }

    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        Arrays.fill(h,-1);
        n=sc.nextInt();
        m=sc.nextInt();
        while(m-->0){
            int a=sc.nextInt();
            int b=sc.nextInt();
            int c=sc.nextInt();
            add(a,b,c);

        }
        spfa();
    }
}

dijkstra I

import java.util.*;
public class Main{
    static int N=510,max=0x3f3f3f3f,n,m;
    static int g[][]=new int[N][N];
    static int d[]=new int[N];
    static boolean st[]=new boolean[N];
    public static int dijkstra(){
        Arrays.fill(d,max);
        d[1]=0;
        for(int i=0;i<n;i++){
            int t=-1;
            for(int j=1;j<=n;j++){
                if(!st[j]&&(t==-1||d[j]<d[t])){
                    t=j;
                }
            }
                st[t]=true;
               for(int j=1;j<=n;j++){
                   d[j]=Math.min(d[j],d[t]+g[t][j]);
               }

            }
        if(d[n]==max)return -1;
        else return d[n];

    }
    public static void main(String []args){
        Scanner scan=new Scanner(System.in);
        n=scan.nextInt();
        m=scan.nextInt();
        for(int i=1;i<=n;i++){
        Arrays.fill(g[i],max);
        }
        while(m-->0){
            int a=scan.nextInt();
            int b=scan.nextInt();
            int c=scan.nextInt();
            g[a][b]=Math.min(g[a][b],c);
        }
       System.out.print(dijkstra());
    }
}

dijkstra II(堆优化版排序)

import java.io.*;
import java.util.*;
public class Main{
    static int N=150010,idx,n,m;
    static int dist[]=new int[N];
    static int h[]=new int[N],e[]=new int[N],ne[]=new int[N],w[]=new int[N];
    static boolean st[]=new boolean[N];
    static int INF=0x3f3f3f3f;
    public static void add(int a,int b, int  c) {
        e[idx]=b;
        w[idx]=c;
        ne[idx]=h[a];
        h[a]=idx++;
    }
    public static int dijkstra() {
        PriorityQueue<PIIs>queue=new PriorityQueue<>();
        Arrays.fill(dist, INF);
        dist[1]=0;
        queue.add(new PIIs(0,1));
        //第一个是距离,后一个是点。
        while(!queue.isEmpty()) {
            PIIs p= queue.poll();
            int t=p.getSecond();
            int distance=p.getFirst();
            if(st[t])continue;
            st[t]=true;
            for(int i=h[t];i!=-1;i=ne[i]) {
                int j=e[i];
                if(dist[j]>distance+w[i]) {
                    dist[j]=distance+w[i];
                    queue.add(new PIIs(dist[j],j));
                }
            }
        }
        if(dist[n]==INF)return -1;
        else return dist[n];
    }
    public static void main(String []args) throws IOException {
        BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
        String []str=bf.readLine().split(" ");
        n=Integer.parseInt(str[0]);
        m=Integer.parseInt(str[1]);
        Arrays.fill(h, -1);
        while(m-->0) {
            String s[]=bf.readLine().split(" ");
            int a=Integer.parseInt(s[0]);
            int b=Integer.parseInt(s[1]);
            int c=Integer.parseInt(s[2]);
            add(a,b,c);
        }
        System.out.println(dijkstra());

    }
}
    class PIIs implements Comparable<PIIs>{
        private int first;
        private int second;
        public int getFirst() {

            return this.first;
        }
        public int getSecond() {
            return this.second;

        }
        public PIIs(int first,int second) {
            this.first=first;
            this.second=second;

        }
        //按数字首位字典序排序
        public int compareTo(PIIs o) {
            return Integer.compare(first, o.first);

        }
    }

最大公约数gcd

import java.util.*;
public class Main{
    public static int gcd(int a,int b){
        while(b!=0){
            int c=a%b;
            a=b;
            b=c;
        }
        return a;
    }
    public static void main(String []args){
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        while(n-->0){
        int a=sc.nextInt();
        int b=sc.nextInt();
        System.out.println(gcd(a,b));
    }
}
}

筛质数

import java.util.*;
public class Main{
    static int N=1000010;
    static int primes[]=new int[N];
    static boolean st[]=new boolean[N];
    static int cnt;
    public static int get_primes(int x){
        for(int i=2;i<=x;i++){
            if(!st[i]){
                primes[cnt++]=i;
            for(int j=i;j<=x;j+=i){
                st[j]=true;
            }
        }
    }
    return cnt;
    }

    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.println(get_primes(n));
    }
}
/* * (有向)图的深度优先遍历算法模板 */ package dsa; public abstract class DFS extends GraphTraverse { //变量 protected static int clock = 0;//遍历过程中使用的计时钟 //构造方法 public DFS(Graph g) { super(g); } //深度优先遍历算法 protected Object traverse(Vertex v, Object info) {//从顶点v出发,做深度优先查找 if (UNDISCOVERED != v.getStatus()) return null;//跳过已访问过的顶点(针对非连通图) v.setDStamp(clock++); v.setStatus(DISCOVERED); visit(v, info);//访问当前顶点 for (Iterator it = v.outEdges(); it.hasNext();) {//检查与顶点v Edge e = (Edge)it.getNext();//通过边e = (v, u) Vertex u = (Vertex)e.getVPosInV(1).getElem();//相联的每一顶点u switch (u.getStatus()) {//根据u当前的不同状态,分别做相应处理 case UNDISCOVERED ://若u尚未被发现,则 e.setType(TREE);//e被归类为“树边” traverse(u, info);//从u出发,继续做深度优先查找 break; case DISCOVERED ://若u已经被发现,但对其访问尚未结束,则 e.setType(BACKWARD);//将e归类为“后向跨边” break; default ://VISITED,即对u的访问已经结束 if (u.getDStamp() < v.getDStamp())//若相对于v,u被发现得更早,则 e.setType(CROSS);//将e归类为“横跨边” else//否则 e.setType(FORWARD);//将e归类为“前向跨边” break; } }//至此,v的所有邻居都已访问结束,故 v.setFStamp(clock++); v.setStatus(VISITED);//将v标记为VISITED return null;//然后回溯 } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值