• ZR 染色(连通块)


    满分做法:

    由题,树是不需要染色的,所以我们要让所有的连通块变成树。所有联通块的总点数 是 n,所以如果设联通块数为 C,则最后剩下的边个数就是 n − C,因此答案就是 m − n + C。

    #include<cstring>
    #include<queue>
    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    using namespace std;
    typedef long long ll;
    const int maxm=4e5+7;
    int pre[maxm],last[maxm],other[maxm],l;
    int n,m,ans;
    bool vis[maxm];
    void add(int x,int y)
    {
     l++;
     pre[l]=last[x];
     last[x]=l;
     other[l]=y;
    }
    void dfs(int x)
    {
     vis[x]=1;
     for(int p=last[x];p;p=pre[p])
     {
       int v=other[p];
       if(vis[v]) continue;
       dfs(v);
     }    
    }
    int main()
    {
     scanf("%d%d",&n,&m);
     for(int i=1;i<=m;i++)
     {
      int x,y;
      scanf("%d%d",&x,&y);
      add(x,y);
      add(y,x);
     }
     for(int i=1;i<=n;i++)
     {
       if(vis[i]==0)
       {
            dfs(i);
            ans++;
       }
     }
     printf("%d
    ",m-n+ans);
     return 0;    
    }
  • 相关阅读:
    静态与非静态(转改)
    关于odp.net的FetchSize属性
    SQL_SERVER 导oracle(转)
    win7电脑上wifi
    Oracle对象统计信息
    SQL_SERVER 连接oracle(转)
    linq in 语法
    关于引擎的设计
    温习设计模式
    技巧类
  • 原文地址:https://www.cnblogs.com/lihan123/p/11722717.html
Copyright © 2020-2023  润新知