HDU2767-连通图


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
2 4 0 3 2 1 2 1 3
 

Sample Output
4 2
 

题目大意:

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

题目思路

                  首先我们很好想到的是如果图中某些点已经联通了就可以压缩成一个点,所以我们可以先用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;
}



全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 12:31
以前小时候我最痛恨出轨、偷情的人,无论男女,为什么会出轨?现在我成了自己最讨厌的人,没想到分享的东西在牛客会被这么多人看,大家的评价都很中肯,我也认同,想过一一回复,但我还是收声了,我想我应该说说这件事,这件事一直压在我心里,是个很大的心结,上面说了人为什么出轨,我大概能明白了。我们大一下半年开始恋爱,开始恋爱,我给出了我铭记3年的承诺,我对她好一辈子,我永远不会背叛,我责任心太重,我觉得跟了我,我就要照顾她一辈子,我们在一起3年我都没有碰过她,她说往东我就往东,她说什么我做什么,她要我干什么,我就干什么!在学校很美好,中途也出过一些小插曲,比如男闺蜜、男闺蜜2号等等等。但我都强迫她改掉了,我...
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
社会教会你的第一课
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务