Influence
In any society there are social influence relations between individuals, where a person x can influence another person y. The same is true for Softopia, a special society where social influence relations are also transitive, meaning that if x can influence y and y can influence z, then x also influences z.
Moreover, the rules of social influence guarantee that if x influences any other person y, then x cannot be influenced by y as well. Using these simple rules, if a person x from Softopia wants something, then all the nodes influenced by x also want the same thing.
Although, Softopia is almost a perfect society, there is a set of certain individuals, X, that would spread false demands through the social influence mechanism used by the society. And there is also an evil entity that would want to find out which of these people should be selected to spread a demand of their own. Because the society can only select one person from X, it would like to select one that is able to influence as many people as possible from Softopia. If there is more than a person that satisfies this request, you should pick the one with the lowest identifier.
Input
The input file contains several test cases, each of them as described below.
The input starts with a line containing two integers separated by one space: n (n ≤ 5000) the
number of individuals in the society, and k, the number of elements in set X. The next line contains
the elements from X, thus k different integers from 1 … n separated by one space. Then follow n lines
and each line i, 1 ≤ i ≤ n, contains first the identifier of the current person followed by the identifiers of
the persons that can be directly influenced by person i, all of them separated by a space. The persons
are labeled from 1 to n. The total number of influences in a society is less than 250000. Additional
whitespaces in the input file should be skipped.
Output
For each test case, the result, representing the identifier of the person that satisfies the conditions
mentioned above, will be written on a single line.
Hint: The table below describes two samples of input and output.
Sample Input
5 2
1 2
1 3 4
2 3 4
3 5
4 5
5
6 3
1 2 3
1 2
2 5
3 4 2
4 6
5
6
Sample Output
1
3
题意:在一个社会中,人会单向的影响另外的一些人,现在有n个人,每个人标号为1~n,每个人有其影响的人,而自己影响的人中他们影响的人自己也能影响,也就是说影响能够传递。现在有一个集合X,这个集合有k个人,现在要求这k个人中,那个人影响的人数最多,如果人数有相同的输出数值小的。
分析:通过每个人影响的人,建立一个有向图,用dfs,然后枚举X集合里的人,找出最大节点的人的标号。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
vector <int> v[5005];
int n,q;
int vis[5005];
int a[5005];
int sum[5005];
int cnt;
void dfs(int x,int &cnt){
for (int i=0;i<v[x].size() ;i++){
if (vis[v[x][i]]==0){
vis[v[x][i]]=1;
cnt++;
dfs(v[x][i],cnt);
}
}
}
int main ()
{
while (scanf ("%d%d",&n,&q)!=EOF){
for (int i=0;i<=n;i++) v[i].clear();//每次开始都得将表清零,一开始忘记了,wa了一次
for (int i=0;i<q;i++) scanf ("%d",&a[i]);//X集合
for (int i=0;i<n;i++){
int num;
char c;
int t=0;
int f=0;
while (1){
t++;
scanf ("%d",&num);
if (t==1) f=num;
else {
v[f].push_back(num); //建图
}
cin.get (c);
if (c=='\n') break;
}
}
memset (sum,0,sizeof (sum));
for (int i=0;i<q;i++){//遍历每个人
memset (vis,0,sizeof (vis));
cnt=0;
dfs(a[i],cnt);
sum[a[i]]=cnt;
}
int ans=0;
int s=0;
for (int i=0;i<q;i++){
if (sum[a[i]]>s) {
s=sum[a[i]];
ans=a[i];
}
}
printf ("%d\n",ans);
}
return 0;
}