UVALive 6432 Influence // 暴力dfs

Problem Description

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

解题思路

给一个有向图,无重边自环,在给定的顶点集X中,判断从哪个顶点出发,能到达的顶点数最多。

直接对每个顶点dfs计数即可。计数用到C++中的bitset来表示,感觉挺好用的。。

参考代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <bitset>
#include <sstream>
using namespace std;
const int MAX_N = 5005;
vector<int> G[MAX_N];
bitset<MAX_N> bi[MAX_N];
bool vis[MAX_N], isX[MAX_N];

void init() {
    for (int i = 0; i < MAX_N; ++i)  {
        G[i].clear();
        bi[i].reset();
        bi[i].set(i);
        vis[i] = false;
    }
}

void dfs(int cur) {
    vis[cur] = true;
    int size = G[cur].size();
    for (int i = 0; i < size; i++) {
        int now = G[cur][i];
        if (!vis[now]) dfs(now);
        bi[cur] |= bi[now];
    }
}

int main() {
    int n, k, num, x;
    while (~scanf("%d %d", &n, &k)) {
        init();
        for (int i = 0; i < k; ++i) {
            scanf("%d", &num);
            isX[num] = true;
        }
        string s;
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &num);
            getline(cin, s);
            stringstream ss(s);
            while (ss >> x) G[num].push_back(x);
        }
        int ans = 0, id;
        for (int i = 1; i <= n; ++i) {
            if (!vis[i]) dfs(i);
            int tmp = bi[i].count();
            if (isX[i] && (tmp > ans || (tmp == ans && i < id))) {
                ans = tmp;
                id = i;
            }
        }
        printf("%d\n", id);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值