Title Description
Suppose the graph is stored by adjacency matrix. Input the vertex information and edge information of the graph, complete the setting of adjacency matrix, calculate the in degree, out degree and degree of each vertex, and output the isolated points in the graph (the vertices with degree 0)
--Procedural requirements--
If C + + is used, only one header file iostream can be included; if C language is used, only one header file stdio can be included
If there is more than one header file include d in the program, do not look at the code, and do 0 point processing
It is not allowed to use third-party objects or functions to fulfill the requirements of this question
input
Test times T, each group of test data format is as follows:
Graph type top points (D-directed graph, U-undirected graph)
Vertex information
Edge number
One edge (vertex 1, vertex 2) or arc (arc tail, arc head) information per line
output
Each group of test data outputs the following information (see the example for the specific output format):
adjacency matrix
Output the degree of each vertex (undirected graph) or the degree of each vertex (directed graph) according to the vertex information. The degree information of isolated points is not output.
Isolated point of a graph. If there is no isolated point, no information will be output.
sample input
2 D 5 V1 V2 V3 V4 V5 7 V1 V2 V1 V4 V2 V3 V3 V1 V3 V5 V4 V3 V4 V5 U 5 A B C D E 5 A B A C B D D C A D
sample output
0 1 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 V1: 2 1 3 V2: 1 1 2 V3: 2 2 4 V4: 2 1 3 V5: 0 2 2 0 1 1 1 0 1 0 0 1 0 1 0 0 1 0 1 1 1 0 0 0 0 0 0 0 A: 3 B: 2 C: 2 D: 3 E
#include<iostream> #include<cstring> using namespace std; int find(string str[], string st, int n){ for(int i= 0; i< n; i++) if(str[i]== st) return i; } int main(){ int t; cin>>t; while(t--){ string ch; int n; cin>>ch>>n; string str[200]; for(int i= 0; i< n; i++) cin>>str[i]; int array[n+ 5][n+5]; for(int i= 0; i< n+ 5; i++) for(int j= 0 ; j< n+ 5; j++) array[i][j]= 0; int in; cin>>in; for(int i= 0 ; i< in; i++){ string s1; string s2; cin>>s1>>s2; int a= find(str, s1, n); int b= find(str, s2, n); //cout<<a<<" "<<b<<"_____"<<endl; array[a][b]= 1; if(ch== "U") array[b][a]= 1; } for(int i= 0; i< n; i++){ for(int j= 0; j< n; j++){ cout<<array[i][j]; if(j!= n- 1) cout<<" "; } cout<<endl; } for(int i= 0; i< n; i++){ cout<<str[i]; int inn= 0; int outt= 0; for(int j= 0; j< n; j++){ if(array[i][j]) outt++; } for(int j= 0; j< n; j++){ if(array[j][i]) inn++; } if(ch== "U"){ if(inn+ outt){ cout<<": "<<(inn+outt)/ 2<<endl; } } else if(ch== "D"){ if(inn+ outt) cout<<": "<<outt<<" "<<inn<<" "<<inn+ outt<<endl; } } } return 0; }