forked from ISCASTEAM/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.h
More file actions
40 lines (36 loc) · 955 Bytes
/
Graph.h
File metadata and controls
40 lines (36 loc) · 955 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED
#include "func.h"
class Graph{
private:
int V;
int E;
vector<vector<int>> adj; //邻接链表
void addEdge(int v,int w){
adj[v].push_back(w);
adj[w].push_back(v);
}
public:
Graph(int v):V(v),E(0){adj.resize(v,vector<int>());};
Graph(string file){
std::ifstream infile(file);
string tmp;
int lineNum = 0;
while(std::getline(infile,tmp)){
std::istringstream iss(tmp);
int v1,v2;
if(lineNum==0) {iss>>V; adj.resize(V,vector<int>());}
else if(lineNum==1) iss>>E;
else {
iss>>v1>>v2;
addEdge(v1,v2);
}
lineNum++;
}
};
int getV(){return V;}
int getE(){return E;}
int degree(int v){return adj[v].size();}
vector<int> getadj(int v){return adj[v];}
};
#endif // GRAPH_H_INCLUDED