HDU-3639-Hawk-and-Chicken(强连通,缩点,DFS)

链接:https://vjudge.net/problem/HDU-3639

题意:

有n个小朋友在一个班级中,现在要选择班长。收集了小朋友们的意见,一条意见表示为A认为B合适。这个是具备传递性的,A认为B合适,B认为C合适。那么A也会认为C合适。 
现在需要提供一份候选人名单,这里面的人,是被最多的人,认为合适的。

思路:

tarjan,缩点,再根据缩点之后的点建立反向的新图,用来求每个人的的票数。

不过在找得票数的时候用记忆化搜索wa了,不知道为啥

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include <map>
#include <queue>
#include <math.h>
#include <cstdio>
#include <set>
#include <iterator>
#include <cstring>
using namespace std;
 
typedef long long LL;
const int MAXN = 5e3+10;
const int INF = 0x3f3f3f3f;
 
vector<int> G[MAXN];
vector<int> Gr[MAXN];
stack<int> St;
int Dfn[MAXN], Low[MAXN];
int Vis[MAXN], Dis[MAXN];
int Fa[MAXN], Fans[MAXN];
int Num[MAXN];
int n, m;
int times, cnt;
 
void Init()
{
    for (int i = 1;i <= n;i++)
        G[i].clear(), Fa[i] = i, Gr[i].clear();
    memset(Dfn, 0, sizeof(Dfn));
    memset(Low, 0, sizeof(Low));
    memset(Vis, 0, sizeof(Vis));
    memset(Dis, 0, sizeof(Dis));
    memset(Num, 0, sizeof(Num));
    memset(Fans, -1, sizeof(Fans));
    times = cnt = 0;
}
 
void Tarjan(int x)
{
    Dfn[x] = Low[x] = ++times;
    Vis[x] = 1;
    St.push(x);
    for (int i = 0;i < G[x].size();i++)
    {
        int node = G[x][i];
        if (Dfn[node] == 0)
        {
            Tarjan(node);
            Low[x] = min(Low[x], Low[node]);
        }
        else if (Vis[node] == 1)
            Low[x] = min(Low[x], Dfn[node]);
    }
    if (Low[x] == Dfn[x])
    {
        cnt++;
        Num[cnt] = 0;
        while (St.top() != x)
        {
            Num[cnt]++;
            Fa[St.top()] = cnt;
            Vis[St.top()] = 0;
            St.pop();
        }
        Num[cnt]++;
        Fa[St.top()] = cnt;
        Vis[St.top()] = 0;
        St.pop();
    }
}
 
int GetFans(int x)
{
    Vis[x] = 1;
    int sum = 0;
    for (int i = 0;i < Gr[x].size();i++)
    {
        int node = Gr[x][i];
        if (Vis[node] == 1)
            continue;
        sum += Num[node] + GetFans(node);
    }
    return sum;
}
 
int main()
{
    int t, cn = 0;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        Init();
//            cin >> Cost[i];
        int l, r;
        for (int i = 1;i <= m;i++)
        {
            scanf("%d%d", &l, &r);
            l++, r++;
//            cin >> l >> r;
            G[l].push_back(r);
        }
        for (int i = 1;i <= n;++i)
            if (!Dfn[i])
                Tarjan(i);
        for (int i = 1;i <= n;i++)
        {
            for (int j = 0;j < G[i].size();j++)
            {
                int node = G[i][j];
                int l = Fa[i], r = Fa[node];
                if (l != r)
                {
                    ++Dis[l];
                    Gr[r].push_back(l);
                }
            }
        }
        int number = -1, is = 0;
        for (int i = 1;i <= cnt;i++)
        {
            if (Dis[i] == 0)
            {
                memset(Vis, 0, sizeof(Vis));
                Fans[i] = Num[i]-1 + GetFans(i);
                number = max(number, Fans[i]);
            }
        }
        cout << "Case " << ++cn << ": " << number << endl;
        for (int i = 1;i <= n;i++)
        {
            if (Fans[Fa[i]] == number)
            {
                if (is++ == 0)
                    cout << i-1;
                else
                    cout << ' ' << i-1 ;
            }
        }
        cout << endl;
    }
 
    return 0;
}

  

posted @   YDDDD  阅读(179)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示