zoukankan      html  css  js  c++  java
  • CODE FESTIVAL 2017 qual B C

    Score : 500 points

    Problem Statement

    Rng has a connected undirected graph with N vertices. Currently, there are M edges in the graph, and the i-th edge connects Vertices Ai and Bi.

    Rng will add new edges to the graph by repeating the following operation:

    • Operation: Choose u and v (uv) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices uand v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.

    Find the maximum possible number of edges that can be added.

    Constraints

    • 2≤N≤105
    • 1≤M≤105
    • 1≤Ai,BiN
    • The graph has no self-loops or multiple edges.
    • The graph is connected.

    Input

    Input is given from Standard Input in the following format:

    N M
    A1 B1
    A2 B2
    :
    AM BM
    

    Output

    Find the maximum possible number of edges that can be added.


    Sample Input 1

    Copy
    6 5
    1 2
    2 3
    3 4
    4 5
    5 6
    

    Sample Output 1

    Copy
    4
    

    If we add edges as shown below, four edges can be added, and no more.


    Sample Input 2

    Copy
    5 5
    1 2
    2 3
    3 1
    5 4
    5 1
    

    Sample Output 2

    Copy
    5
    

    Five edges can be added, for example, as follows:

    • Add an edge connecting Vertex 5 and Vertex 3.
    • Add an edge connecting Vertex 5 and Vertex 2.
    • Add an edge connecting Vertex 4 and Vertex 1.
    • Add an edge connecting Vertex 4 and Vertex 2.
    • Add an edge connecting Vertex 4 and Vertex 3.

    这道题我们的做法是二分图染色 如果原图是个二分图 那么同色距离为偶 所以答案就是 黑*白-m

    如果不是 那么我们发现 图上存在环且为奇环 那么环上任意一对点都可以连边

    这样可以不断拓出来使得整个图每对点都可以连边 这样答案就是n*(n-1)/2-m辣

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    const int M=2e5+7;
    int n,m,s[2],color[M],vis[M];
    int first[M],cnt;
    struct node{int to,next;}e[2*M];
    void ins(int a,int b){e[++cnt]=(node){b,first[a]}; first[a]=cnt;}
    void insert(int a,int b){ins(a,b); ins(b,a);}
    bool ly=false;
    void dfs(int x){
        vis[x]=1; s[color[x]]++;
        for(int i=first[x];i;i=e[i].next){
            int now=e[i].to;
            if(!vis[now]) color[now]=1-color[x],dfs(now);
            if(vis[now]&&color[now]==color[x]){ly=true; return ;}
        }
    }
    int main(){
        int x,y;
        n=read(); m=read();
        for(int i=1;i<=m;i++) x=read(),y=read(),insert(x,y);
        dfs(1);
        if(ly) printf("%lld
    ",1LL*n*(n-1)/2-m);
        else printf("%lld
    ",1LL*s[0]*s[1]-m);
        return 0;
    }
    View Code
  • 相关阅读:
    [HDFS Manual] CH6 HDFS Federation
    [HDFS Manual] CH4 HDFS High Availability Using the Quorum Journal Manager
    [HDFS Manual] CH3 HDFS Commands Guide
    [HDFS Manual] CH2 HDFS Users Guide
    [HDFS Manual] CH1 HDFS体系结构
    [20180312]进程管理其中的SQL Server进程占用内存远远大于SQL server内部统计出来的内存
    [MySQL Status] Queries,Questions,read/s区别,Com_Commit和handle_commit
    [MySQL TroubleShooting] 服务启动报错
    [MySQL Code]Innodb 锁分配和锁冲突判断
    [MySQL Reference Manual]17 Group Replication
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7639527.html
Copyright © 2011-2022 走看看