zoukankan      html  css  js  c++  java
  • POJ3697【BFS】

    题意:

    n个点的完全图,删掉m条边以后,求与1联通的点的个数。

    思路:
    直接判断
    遍历图,n(n+1)/2=5e7
    复杂度n^2......,哦,这样也行。。。

    //#include<bits/stdc++.h>
    #include<cstdio>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    
    const int N=1e4+10;
    const int M=2e6+10;
    struct Edge{
        int to;
        int next;
    };
    Edge edge[M];
    int head[M],tol,pre[N];
    bool vis[N];
    int n,m;
    
    void init()
    {
        tol=0;
        memset(head,-1,sizeof(head));
    }
    
    void add(int u,int v)
    {
        edge[tol].to=v;
        edge[tol].next=head[u];
        head[u]=tol++;
    }
    
    int q[N],ss,tt;
    int BFS()
    {
        int ans=0;
        ss=tt=0;;
        memset(vis,false,sizeof(vis));
        vis[1]=true;
        q[tt++]=1;
        while(ss<tt)
        {
            int u=q[ss++];
            for(int i=head[u];~i;i=edge[i].next)
                pre[edge[i].to]=u;
            for(int i=1;i<=n;i++)
            if(pre[i]!=u&&!vis[i]){
                vis[i]=true;
                q[tt++]=i;
                ans++;
            }
        }
        return ans;
    }
    
    int main()
    {
        int cas=1;
        while(~scanf("%d%d",&n,&m))
        {
            int u,v;
            if(!n&&!m) break;
            init();
            while(m--)
            {
                scanf("%d%d",&u,&v);
                add(u,v);
                add(v,u);
            }
            printf("Case %d: %d
    ",cas++,BFS());
        }
        return 0;
    }
    


  • 相关阅读:
    正则表达式
    匿名函数作业
    内置函数&匿名函数
    模拟面试题一
    迭代器
    生成器
    装饰器
    函数
    疑问?
    3,app信息抽取
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/6777442.html
Copyright © 2011-2022 走看看