Welcome to the world of my C language
topic
Problem Description
There is a war between country A and country B. There are n cities in country A. These cities are connected by m directed roads. These roads will not form loops. Some of the cities are special. They have granaries. Cities with granaries cannot be reached by other cities. Grain can be sent from the granaries to any city through some roads. Now the king of country B wants to destroy a city in country A. Pass through the destroyed city. Asking which city to destroy can make the most cities out of food.
Input
Two numbers n and m in the first line (n<=1000, m<=10000)
In the next m lines, each line contains two numbers a and b, indicating that city a has a road connecting to city b
Output
Output a number to indicate the serial number of the destroyed city. If multiple cities meet the conditions, output the city with the smallest serial number
Sample Input
3 3
1 2
2 3
1 3
Sample Output
1
answer
Shown below 实现代码
.
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
int a[1007];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m) != EOF)
{
memset(a,0,sizeof(a));
int a1,b;
for(int i = 0; i < m; i++)
{
cin >> a1 >> b;
a[a1]++;//类似散列表的思想
}
int max = 0, max_flag = 0;
for( int i = 0; i < n; i++)
{
if(a[i] > max)
{
max = a[i];
max_flag = i;
}
}
cout << max_flag << endl;
}
return 0;
}
Thoughts on this question
The content of this block may come from textbooks or other websites. If infringement is involved, please contact me to delete it. Thank you~
Ideas:
Hash the store, and then find the largest.
the above.