1.Maximum Indegree
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Given a directed graph with N vertices (1 ≤ N≤ 2,500) and C (1 ≤ C ≤ 10,000) edges, each vertex of the graph is assigned with a unique number from 1 to N. You are required to calculate the maximum indegree of the vertices in the graph. (The indegree of a vertex is defined as the number of edges pointing to it).
Input format : The first line of the input is an integer m to indicate m test cases. The following lines are the input of the m cases, each of which is corresponding to one graph. The first line of each test case contains two integers N and C indicating the vertices number and edges number in the graph. The edge information is defined in the following C lines, each one of which contains two integers vi and vj indicating an edge vi -> vj in the graph.
Output format:
The output contains m lines. The No. i (1≤ i≤ m) line is the maximum degree in the graph in the No. i test case.
Input Example:
2
3 2
2 1
3 1
7 11
2 4
1 4
7 2
3 4
5 7
7 3
6 1
6 3
2 5
5 6
7 1
Output Example:
2
3
题解:就是求图中各点的最大入度,关键的是建图这一步。数组graph就是用二维数组建的图。
#include<iostream> using namespace std; int main(){ int test_num,point_num,edge_num; cin>>test_num; while(test_num--){ int edge_start_index,edge_end_index; cin>>point_num>>edge_num; int **graph=(int**)new int*[point_num]; for(int i=0;i<point_num;i++){ graph[i]=new int[point_num]; } for(int i=0;i<point_num;i++){ for(int j=0;j<point_num;j++)graph[i][j]=0; } while(edge_num--){ cin>>edge_start_index>>edge_end_index; graph[edge_start_index-1][edge_end_index-1]=1; } int maxdegree=0; for(int i=0;i<point_num;i++){ int count=0; for(int j=0;j<point_num;j++){ if(graph[j][i]==1)count++; } if(count>maxdegree)maxdegree=count; } cout<<maxdegree<<endl; } }
2. Cycle
Time Limit: 1000MS |
|
Memory Limit: 20000K |
Given a directed graph as defined in the last question, please judge whether there is a cycle in the graph or not.
Input format : The same as the last question.
Output format:
The output contains m lines. The No. i (1≤ i≤ m) line is “YES”, if there is a cycle in the graph of the No. i test case. Otherwise, “NO” is output.
Input Example:
2
3 2
2 1
3 1
7 11
2 4
1 4
7 2
3 4
5 7
7 3
6 1
6 3
2 5
5 6
7 1
Output Example:
NO
YES
题解:问的是所给的图是否有环,对每个点进行广度优先搜索(利用模板里的队列),如果回到起点则说明图中含环
#include<iostream>
#include<queue>
using namespace std;
int main(){
int test_num,point_num,edge_num;
cin>>test_num;
while(test_num--){
int edge_start_index,edge_end_index;
cin>>point_num>>edge_num;
int*mark=new int[point_num];
int **graph=(int**)new int*[point_num];
for(int i=0;i<point_num;i++){
graph[i]=new int[point_num];
}
for(int i=0;i<point_num;i++){
for(int j=0;j<point_num;j++)graph[i][j]=0;
}
while(edge_num--){
cin>>edge_start_index>>edge_end_index;
graph[edge_start_index-1][edge_end_index-1]=1;
}
int count=0;
queue<int>q;
for(int i=0;i<point_num;i++){
for(int k=0;k<point_num;k++){
mark[k]=0;
}
q.push(i);
mark[i]=1;
while(!q.empty()){
int index;
index=q.front();mark[index]=1;
q.pop();
for(int j=0;j<point_num;j++){
if(graph[index][j]==1&&mark[j]==1){count=1;goto A;}
if(graph[index][j]==1)q.push(j);
}
}
}
A:
if(count==1)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
3. Shortest Path
Time Limit: 1000MS |
|
Memory Limit: 20000K |
Abrao is a beautiful country. It has N cities (1 ≤ N≤ 2,500) and C (1 ≤ C ≤ 10,000) highways to connect adjacent cities. Each city is assigned with a unique number from 1 to N. You are required to calculate the length of the shortest path from one city to another.
Input format: The first line contains four integers N, C, S and T. N and C has been defined above. S and T (1 ≤ S,T≤ 2,500) are the numbers of two cities. The information about the highways is defined in the following C lines, each one of which contains three integers vi,vj and w indicating a high way between vi and vj with the distance equal to w (there may be multiple highways between two cities). You are required to calculate the length of the shortest path from the city S to T.
Output format: The output is the length of the shortest path from the city S to T.
Input Example:
7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1
Output Example:
7