HDU2767-连通图

来源:互联网 发布:又见白娘娘淘宝可信吗 编辑:程序博客网 时间:2024/06/05 09:37


Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6478    Accepted Submission(s): 2237


Problem Description
Consider the following exercise, found in a generic linear algebra textbook.

Let A be an n × n matrix. Prove that the following statements are equivalent:

1. A is invertible.
2. Ax = b has exactly one solution for every n × 1 matrix b.
3. Ax = b is consistent for every n × 1 matrix b.
4. Ax = 0 has only the trivial solution x = 0. 

The typical way to solve such an exercise is to show a series of implications. For instance, one can proceed by showing that (a) implies (b), that (b) implies (c), that (c) implies (d), and finally that (d) implies (a). These four implications show that the four statements are equivalent.

Another way would be to show that (a) is equivalent to (b) (by proving that (a) implies (b) and that (b) implies (a)), that (b) is equivalent to (c), and that (c) is equivalent to (d). However, this way requires proving six implications, which is clearly a lot more work than just proving four implications!

I have been given some similar tasks, and have already started proving some implications. Now I wonder, how many more implications do I have to prove? Can you help me determine this?
 

Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:

* One line containing two integers n (1 ≤ n ≤ 20000) and m (0 ≤ m ≤ 50000): the number of statements and the number of implications that have already been proved.
* m lines with two integers s1 and s2 (1 ≤ s1, s2 ≤ n and s1 ≠ s2) each, indicating that it has been proved that statement s1 implies statement s2.
 

Output
Per testcase:

* One line with the minimum number of additional implications that need to be proved in order to prove that all statements are equivalent.
 

Sample Input
24 03 21 21 3
 

Sample Output
42
 

题目大意:

                  题目意思就是给你一个无向图,要你求使这图成为连通图所需的最小加的边,连通图就是所有点都能相互到达

题目思路

                  首先我们很好想到的是如果图中某些点已经联通了就可以压缩成一个点,所以我们可以先用targan缩点,然后我们考虑连通图的一些性质,所有点的入度和出度没有没0的,这个性质对于这题有很大的帮助,然后我们试着去统计所有点的出入度为0的个数,应为有为0的情况我们就要去连边,而一个入度或出度为0的连接一个出度过入度为0的点时可以抵消两个0,所以我们可以让出入度中为0的少的去连接为0多的一方,这样就能保证边最少!


AC代码:

#include<cstring>#include<cstdio>#define min(x,y) (x<y?x:y)const int maxn = 2e4+10;const int maxm = 5e4+10;struct st{  int v,nex;}edge[maxm];int hed[maxn],vis[maxn],low[maxn],dfn[maxn],belon[maxn],stack[maxn];int in[maxn],out[maxn];int n,m,e,top,cnt,num,sum1,sum2;void init(){    memset(hed,-1,sizeof(hed));    memset(vis,0,sizeof(vis));    memset(dfn,0,sizeof(dfn));    memset(in,0,sizeof(in));    memset(out,0,sizeof(out));    e=1;    top=num=cnt=0;}void add(int u,int v){    edge[e].v=v,edge[e].nex=hed[u],hed[u]=e++;}void targan(int u){     //缩点    dfn[u]=low[u]=++num;    stack[top++]=u;    vis[u]=1;    for(int i=hed[u];~i;i=edge[i].nex){        int v = edge[i].v;        if(!dfn[v]){            targan(v);            low[u]=min(low[u],low[v]);        }else if(vis[v]){            low[u]=min(low[u],dfn[v]);        }    }    if(low[u]==dfn[u]){        cnt++;        int x;        do{            x=stack[--top];            vis[x]=0;            belon[x]=cnt;        }while(x!=u);    }}int main(){    int t;scanf("%d",&t);    while(t--){        init();        scanf("%d%d",&n,&m);        int mm = m;        while(m--){            int u,v;scanf("%d%d",&u,&v);            add(u,v);        }        for(int i=1;i<=n;i++){            if(!dfn[i])targan(i);        }        if(cnt==1){            printf("0\n");            continue;        }        for(int i=1;i<=n;i++){            for(int j = hed[i];~j;j=edge[j].nex){                int v = edge[j].v;                if(belon[i]!=belon[v]){                    out[belon[i]]=1;       //记录出入度的情况                    in[belon[v]]=1;                }            }        }        sum1=sum2=0;        for(int i=1;i<=cnt;i++){            if(in[i]==0)sum1++;    //分别统计出入度为0的个数            if(out[i]==0)sum2++;        }        if(sum1<sum2)sum1=sum2;     //答案就是为0的大的一方        printf("%d\n",sum1);    }    return 0;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝买的流量卡然后网卡怎么办? 忘记了qq号绑定了手机怎么办 微信绑定qq号忘记了怎么办 扣扣密码忘了手机号换了怎么办 如何更改微信登录密码忘记了怎么办 阿里卖家不肯同意退货退款怎么办 新买的号码被绑定微信怎么办 如果换手机原来旧手机支付宝怎么办 支付宝注册证件已过有效期怎么办 支付宝账号是手机号换号怎么办 淘宝网卖假药伤了别人身体怎么办 微信手机登录收不到验证码怎么办 淘宝店的宝贝没有尺码选项怎么办 淘宝账号被保护要自助开通怎么办 小米平板经常出现系统无响应怎么办 淘宝买家被取消运费险了怎么办 新开的淘宝直播店没有流量怎么办 淘宝买的东西一直不发货怎么办 在淘宝上买了东西不发货怎么办 宝贝好几天拉一次吃的也少怎么办 淘宝快递正在运输途中想退货怎么办 淘宝买的东西退货商家拒收怎么办 手机屛上出现了微信图标怎么办 手机用了两年了反应太慢怎么办 魅族手机显示手机已锁定怎么办 为什么微信注册要安全验证码怎么办 微信帐号异常无法领取红包怎么办 帮朋友代付在支付宝被骗怎么办 商家说未收到货拒绝退款怎么办 淘宝商家拒绝退款怎么办还没收货的 拼多多点错确认收货了怎么办 被别人用菜刀砍伤没钱看病怎么办 东京下了订单但不发货怎么办 绑定卷皮钱包的手机号码丢了怎么办 小孩回奶在垫的被子上发霉了怎么办 2个月宝宝不喝母乳只喝奶瓶怎么办 我的扣扣被盗了朋友别被骗了怎么办 我买的股票退市了我的钱怎么办啊 在美食林被门口买宝石的骗了怎么办 在商场买的彪马鞋子皮子裂了怎么办 手机换号了京东钱包里的余额怎么办