强连通 反向建图 hdu3639

Hawk-and-Chicken

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

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 total supports 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

反向建图的原因是方便统计,倘若是正向的话 ,那么如果有2->3,3->1,2->1  2这个节点在计算的时候很难搞

#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=5008;
const int M=30008;
int head[N],tot,scnt,cnt,cont,sz[N];
int dfn[N],low[N],bl[N],q[N],l;
bool instack[N],ru[N];
vector<int>G;
vector<int>C[N];
struct node{
   int to,next;
}e[M];
void add(int u,int v){
   e[tot].to=v;
   e[tot].next=head[u];
   head[u]=tot++;
}
void init(){
   tot=scnt=cnt=l=0;
   memset(head,-1,sizeof(head));
   memset(dfn,0,sizeof(dfn));
   memset(instack,0,sizeof(instack));
   memset(ru,0,sizeof(ru));
   G.clear();
   for(int i=0;i<N;++i) C[i].clear();
}
void Tajan(int u){
   dfn[u]=low[u]=++cnt;
   q[l++]=u;
   instack[u]=1;
   for(int i=head[u];i+1;i=e[i].next){
    int v=e[i].to;
    if(!dfn[v]) {
        Tajan(v);
        low[u]=min(low[u],low[v]);
    }
    else if(instack[v]&&dfn[v]<low[u])
        low[u]=dfn[v];
   }
   if(dfn[u]==low[u]) {
    ++scnt;
    int t;
    sz[scnt]=0;
    do{
        t=q[--l];
        bl[t]=scnt;
        ++sz[scnt];
        C[scnt].push_back(t);
        instack[t]=0;
    }while(t!=u);
   }
}
bool used[N];
void dfs(int u,int tz){
   for(int i=head[u];i+1;i=e[i].next)
   if(!used[e[i].to]){
    used[e[i].to]=1;
    sz[tz]+=sz[e[i].to];
    dfs(e[i].to,tz);
   }
}
struct point{
   int u,v;
}ee[M];
int main(){
    int n,m,u,v,T;
    scanf("%d",&T);
    for(int tas=1;tas<=T;++tas){
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;++i){
            scanf("%d%d",&u,&v);
            add(v,u);
        }
        for(int i=0;i<n;++i) if(!dfn[i]) Tajan(i);
        int pp=0;
        for(int i=0;i<n;++i)
        for(int j=head[i];j+1;j=e[j].next){
            int v=e[j].to;
            if(bl[i]==bl[v]) continue;
            else {ee[pp].u=bl[i];ee[pp++].v=bl[v];ru[bl[v]]=1;}
        }
        memset(head,-1,sizeof(head));
        tot=0;
        for(int i=0;i<pp;++i) add(ee[i].u,ee[i].v);
        int maxx=-1;
        for(int i=1;i<=scnt;++i)
        if(!ru[i]){
            memset(used,0,sizeof(used));
            dfs(i,i);
            if(maxx<sz[i]) {maxx=sz[i];G.clear(); for(int j=0;j<(int)C[i].size();++j) G.push_back(C[i][j]);}
        else if(maxx==sz[i]) for(int j=0;j<(int)C[i].size();++j) G.push_back(C[i][j]);
        }
        sort(G.begin(),G.end());
        printf("Case %d: %d\n",tas,maxx-1);
        for(int i=0;i<(int)G.size()-1;++i) printf("%d ",G[i]);
        printf("%d\n",G[(int)G.size()-1]);
    }
}
时间: 2024-10-23 10:34:21

强连通 反向建图 hdu3639的相关文章

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

http://acm.hdu.edu.cn/showproblem.php?pid=3639 题意: 有一群孩子正在玩老鹰抓小鸡,由于想当老鹰的人不少,孩子们通过投票的方式产生,但是投票有这么一条规则:投票具有传递性,A支持B,B支持C,那么C获得2票(A.B共两票),输出最多能获得的票数是多少张和获得最多票数的人是谁? 思路: 先强连通缩点反向建图,在计算强连通的时候,需要保存每个连通分支的结点个数. 为什么要反向建图呢?因为要寻找票数最多的,那么肯定是入度为0的点,然后dfs计算它的子节点的

poj1122 FDNY to the Rescue!(dij+反向建图+输出路径)

题目链接:poj1122 FDNY to the Rescue! 题意:给出矩阵,矩阵中每个元素tij表示从第i个交叉路口到第j个交叉路口所需时间,若tij为-1则表示两交叉路口之间没有直接路径,再给出火警位置所在的交叉路口 和 一个或多个消防站所处的交叉路口位置.输出要求按消防站到火警位置所需时间从小到大排列,输出信息包括消防站位置(初始位置),火警位置(目标位置),所需时间,最短路径上每个交叉路口. 题解:反向建图,从火警位置求一次最短路,求最短路时记录路径,按时间从小到大输出. 1 #in

HDU2647(拓扑排序+反向建图)

题意不说了,说下思路. 给出的关系是a要求的工资要比b的工资多,由于尽可能的让老板少付钱,那么a的工资就是b的工资+1,可以确定关系为a>b,根据拓扑排序建边的原则是把"小于"关系看成有向边,那么我们可以建边v->u. #include <stdio.h> #include <string.h> #include <string> #include <iostream> #include <algorithm> #

HDU1535Invitation Cards(有向图,正向建图和反向建图各spfa一次)

Invitation Cards Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2374    Accepted Submission(s): 1151 Problem Description In the age of television, not many people attend theater performances.

POJ 3687 反向建图+拓扑

Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11146   Accepted: 3192 Description Windy has N balls of distinct weights from 1 unit to N units. Now he tries to label them with 1 to N in such a way that: No two balls share

HDU4857——逃生(反向建图+拓扑排序)

逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会是不平等的,这些人有的穷有的富.1号最富,2号第二富,以此类推.有钱人就贿赂负责人,所以他们有一些好处.负责人现在可以安排大家排队的顺序,由于收了好处,所以他要让1号尽量靠前,如果此时还有多种情况,就再让2号尽量靠前,如果还有多种情况,就让3号尽量靠前,以此类推.那么你就要安排大家的顺序.我们保证一

[CF825E] Minimal Labels(反向建图,拓扑排序)

题目链接:http://codeforces.com/problemset/problem/825/E 题意:给一个有向图,求一个排列,这个排列是每一个点的序号,使得序号对应的点的排序符合拓扑序并且这个排列字典序最小. 直接跑字典序最小的拓扑排序是不行的,因为那样只是确保点的字典序而非这个排列的字典序,比如这个数据: 10 15 2 反过来考虑,点号大的入度为0的点一定排在后面,这个位置确定了.但是点好小的入度为0的未必一定排在前面,因为这个点之前可能有入度不为0,但是与此点无关的点在前面,按题

hdu 1535 Invitation Cards(有向图的来回最短路,要反向建图)

题目: 链接:点击打开链接 题意: 给一个图,求1到各点和各点到1最短路. 思路: 先spfa,然后反向建图,在spfa就行了. 代码: #include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; #define INF 100000000 const int N = 1000010; struct node{ int u,v,w

HDU 2680 Choose the best route &lt;SPFA算法+反向建图&gt;

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10690    Accepted Submission(s): 3454 Problem Description One day , Kiki wants to visit one of her friends. As she is liabl