-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathDigraph.h
More file actions
48 lines (44 loc) · 1.16 KB
/
Digraph.h
File metadata and controls
48 lines (44 loc) · 1.16 KB
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
41
42
43
44
45
46
47
48
#ifndef DIGRAPH_H_INCLUDED
#define DIGRAPH_H_INCLUDED
#include "func.h"
class Digraph{
private:
int V;
int E;
vector<vector<int>> adj; //邻接链表
void addEdge(int v,int w){
adj[v].push_back(w); //区别于无向图
}
public:
Digraph(int v):V(v),E(0){adj.resize(v,vector<int>());};
Digraph(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];}
//反转图
Digraph* reverseG(){
Digraph* revG = new Digraph(V);
for(int v=0;v<V;v++){
for(int w : getadj(v))
revG->addEdge(w,v);
}
return revG;
}
};
#endif // DIGRAPH_H_INCLUDED