hdu3639Hawk-and-Chicken

原创 2013年12月01日 22:59:05

Hawk-and-Chicken

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1562    Accepted Submission(s): 440

Problem Description
Kids in kindergarten enjoy playing a game called Hawk-and-Chicken. But there always exists a big problem: every kid in this game want to play the role of Hawk. 
So the teacher came up with an idea: Vote. Every child have some nice handkerchiefs, and if he/she think someone is suitable for the role of Hawk, he/she gives a handkerchief to this kid, which means this kid who is given the handkerchief win the support. Note the support can be transmitted. Kids who get the most supports win in the vote and able to play the role of Hawk.(A note:if A can win
support from B(A != B) A can win only one support from B in any case the number of the supports transmitted from B to A are many. And A can't win the support from himself in any case.
If two or more kids own the same number of support from others, we treat all of them as winner.
Here's a sample: 3 kids A, B and C, A gives a handkerchief to B, B gives a handkerchief to C, so C wins 2 supports and he is choosen to be the Hawk.
 
Input
There are several test cases. First is a integer T(T <= 50), means the number of test cases.
Each test case start with two integer n, m in a line (2 <= n <= 5000, 0 <m <= 30000). n means there are n children(numbered from 0 to n - 1). Each of the following m lines contains two integers A and B(A != B) denoting that the child numbered A give a handkerchief to B.
 
Output
For each test case, the output should first contain one line with "Case x:", here x means the case number start from 1. Followed by one number which is the totalsupports the winner(s) get. 
Then follow a line contain all the Hawks' number. The numbers must be listed in increasing order and separated by single spaces.
 
Sample Input
2 4 3 3 2 2 0 2 1 3 3 1 0 2 1 0 2
 
Sample Output
Case 1: 2 0 1 Case 2: 2 0 1 2
 
Author
Dragon
 
Source
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <stack>
#define DEBUG 10
using namespace std;
const int maxn = 5000+10;// vertex.
const int maxm = 30000+10; // edges.
int gn, gm;
//Accepted	3639	562MS	1508K	4225 B	G++	Achiberx
vector<int> G[maxn], G2[maxn];
vector<int> G3[maxn]; // reverse graph of G2.
vector<int> result[maxn];
bool vis[maxn]; // for dfs2.
int weight[maxn];// the number of vertex of every scc.
int pre[maxn], lowlink[maxn], sccno[maxn];
int dfs_clock, scc_cnt;
stack<int> S;

void dfs(int u) {
    pre[u] = lowlink[u] = ++dfs_clock;
    S.push(u);
    for(int i = 0; i < (int)G[u].size(); i++) {
        int v = G[u][i];
        if(!pre[v]) {
            dfs(v);
            lowlink[u] = min(lowlink[v], lowlink[u]);
        } else if(!sccno[v]) {
            lowlink[u] = min(lowlink[u], pre[v]);
        }
    }
    if(lowlink[u] == pre[u]) {
        scc_cnt++;
        for(;;) {
            int x = S.top(); S.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}


void find_scc(int n) {
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(pre, 0, sizeof(pre));
    for(int i = 0; i < n; i++) { // from 0 to n-1.
        if(!pre[i]) dfs(i);
    }
}

void build_map() {
    memset(weight, 0, sizeof(weight));
    for(int i = 0; i < gn; i++) {
        int u = sccno[i];
        weight[u] += 1;
    }
    for(int i = 0; i < maxn; i++) { G2[i].clear(); G3[i].clear(); }
    for(int i = 0; i < gn; i++) {
        for(int j = 0; j < (int)G[i].size(); j++) {
            int v = G[i][j];
            if(sccno[i] != sccno[v]) {
                G2[sccno[i]].push_back(sccno[v]);
                G3[sccno[v]].push_back(sccno[i]);// reverse graph of G2.
            }
        }
    }
}

int dfs2(int u) {
    if(vis[u]) return 0;
    int total = 0;
    vis[u] = true;
    for(int i = 0; i < (int)G3[u].size(); i++) {
        int v = G3[u][i];
        total += dfs2(v);
    }
    if(!G3[u].size()) return weight[u];// leaf node.
    else return total + weight[u];
}

int main()
{
    //freopen("in", "r", stdin);
    int T, cas = 0;
    int u, v; // for input.
    scanf("%d", &T);
    while(T--) {
        for(int i = 0; i < maxn; i++) G[i].clear();
        scanf("%d%d", &gn, &gm);
        for(int i = 0; i < gm; i++) {
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
        }
        find_scc(gn);
        build_map();
        for(int i = 0; i < maxn; i++) result[i].clear();
        for(int i = 1; i <= scc_cnt; i++) {
            if((int)G2[i].size() == 0) { // the outdegree equal 0 of G2.
                memset(vis, false, sizeof(vis));
                int amount = dfs2(i);
                result[amount-1].push_back(i);
            }
        }
        int maxv;
        for(int i = gn; i >= 0; i--) {
            if(result[i].size() != 0) {
                maxv = i;
                break;
            }
        }
        printf("Case %d: %d\n", ++cas, maxv);
        int Size = result[maxv].size();
        vector<int> V;
        V.clear();
        for(int i = 0; i < Size; i++) {
            for(int j = 0; j < gn; j++) {
                if(sccno[j] == result[maxv][i]) {
                    V.push_back(j);
                }
            }
        }
        sort(V.begin(), V.end());
        Size = V.size();
        if(Size==1) {
            printf("%d\n", V[0]);
        } else {
            for(int i = 0; i < Size-1; i++) {
                printf("%d ", V[i]);
            }
            printf("%d\n", V[Size-1]);
        }
    }
    return 0;
}
版权声明:欢迎转载!

相关文章推荐

HDU3639 Hawk-and-Chicken

Tarjan好可怕

HDU3639 Hawk-and-Chicken Tarjan缩点 +dfs+贪心

###HDU3639 Hawk-and-Chicken :Tarjan缩点 +dfs+贪心 ####Description 一个N(0~n-1)个点与M条边的有向图,他们是有传递关系的,问对于图中...
  • YJSchaf
  • YJSchaf
  • 2017年04月01日 08:58
  • 1505

hdu3639 Hawk-and-Chicken(Tarjan缩点+反图搜索)

http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意:给你n个小孩和m个关系,关系中x, y代表x认为y受欢迎,欢迎度可传递,求最后最受欢迎的人有多...

hdu3639 Hawk-and-Chicken

刚看到此题, 感觉很简单,就顺着感觉写了。 后来,写不下去啦, 整不清楚了, 网上看了一下, 要建反图, 思路: 先建图, 后缩点,后根据缩后的点, 二次建图,但要建反图, 记录个缩点出度, 找出度...

Hawk-and-Chicken (hdu 3639 强连通缩点+反向建图DFS)

题意:n个人投票,告诉投票关系问哪些人的票数最多,票可以传递,比如A投给B,B得一票,B又投给C,那么C得两票。 思路:先强连通缩点,在同一个连通分量里的传递出去的票数为连通分量内点的个数-1(自己不...

hdu 3639 Hawk-and-Chicken(缩点+树dp)

Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others...
  • WEYuLi
  • WEYuLi
  • 2013年08月12日 15:05
  • 822

hdu 3639 Hawk-and-Chicken【强联通Tarjan+Dfs】

Hawk-and-Chicken Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others...

HDU3639Hawk-and-Chicken (好题,强连通缩点,建图,DFS回溯)

题意:大家玩老鹰抓小鸡的游戏,但是大家都想做老鹰,所以老师安排一个投票,每个人不能投自己,但是可以投多个人,投票具有传递性,比如A–>B, B–>C,那么B获得A一票,C获得B一票,A一票,共计2票。...

hdu3639 Hawk-and-Chicken

题目大意n个人相互投票,这投票关系是可以传递的。A投B,B投C,那么C就获得了两票。 分析同一个强连通分量的人的票数等于点数减一。 现在就缩点,然后逆向建图,从入度为0的点开始搜索。 题目链接/***...

HDU 3639 Hawk-and-Chicken(强连通分量+缩点)

HDU 3639 Hawk-and-Chicken(强连通分量+缩点) http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意:给你一个有向图,如果从u点能...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdu3639Hawk-and-Chicken
举报原因:
原因补充:

(最多只允许输入30个字)