Bus Route
Mr. Shim received a request to improve bus routes of a complex city and he researched the bus operation policy of this city.
In the city, it operates a bus for all roads from a departure point to a designated arrival point.
For instance, If the roads consist of No. 3 point from No.1 point, No. 4 point from No.1 point, No. 3 point from No.2 point, and No. 4 point from No.3 point separately and their approvals are No. 4 point,
the bus routes are operated in three routes of 1->3->4, 2->3->4, & 1->4.
By the way, this bus operation policy causes traffic congestion locally.
Because of that, the first thing to do is finds out the number of buses of the section where the largest bus routes pass through.
In the city, there are the points of number N and one-way roads of number M connecting the number N’s points.
In addition, the arrival point is the number N’s point and the starting point is all points which have no roads to enter the corresponding points.
Let’s find out how Mr. Shim figures out the number of buses of the section where the largest bus routes pass by.
Time limit : 1 sec (Java : 2 sec)
[Input]
There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the totally T test cases are provided in the following lines (T ≤ 10 )
In each test case,
In the first line, The number of the points (N) and the number of one-way roads (M) are given in order. (1 ≤ N ≤ 5000, 1 ≤ M ≤ 50000)
Information of the roads (s) & (e) is given from the next line to the line of the number M.
This means the one-way road passing through No. s point to No. e point is existed.
[Output]
In the each line, generate the number of buses of the section where the largest bus routes pass through.
[Input Example]
2
7 7
1 3
3 4
3 5
4 6
2 3
5 6
6 7
[Output Example]
4
[Explanation of an example]
In the input example, there are four paths in total.
1->3->4->6->7
1->3->5->6->7
2->3->4->6->7
2->3->5->6->7
Four bus routes are overlapped on 6->7 from the above route. Therefore the answer is four.
#include <stdio.h>
typedef struct _ONE_WAY_{
int s;
int e;
} OneWay;
int N,M;
OneWay data[50005];
long long int way[5005][2];
long long int Answer;
int main(void)
{
int tc, T, i, j;
//freopen("input.txt", "r", stdin);
setbuf(stdout, NULL);
scanf("%d", &T);
for(tc = 0; tc < T; tc++)
{
scanf("%d %d", &N, &M);
for(i=1;i<=M;i++) {
scanf("%d %d", &data[i].s,&data[i].e);
}
for(i=1; i<=N; i++)
{
way[i][0] = 0;
way[i][1] = 0;
}
Answer = 0;
for(i=1; i<N; i++)
{
if(way[i][0] == 0)
way[i][0] = 1;
for(j=1; j<=M; j++)
{
if(data[j].s == i)
{
way[data[j].e][0] += way[i][0];
}
}
}
for(i=N; i>1; i--)
{
if(way[i][1] == 0)
way[i][1] = 1;
for(j=1; j<=M; j++)
{
if(data[j].e == i)
{
way[data[j].s][1] += way[i][1];
}
}
}
for(i=1; i<=M; i++)
{
if(Answer < way[data[i].s][0]*way[data[i].e][1])
Answer = way[data[i].s][0]*way[data[i].e][1];
}
printf("%lld\n", Answer);
}
return 0;
}