zoukankan      html  css  js  c++  java
  • Codeforces Round #435 (Div. 2) B (二分图) C(构造)

    B. Mahmoud and Ehab and the bipartiteness
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees.

    A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in such a way, that for each edge (u, v) that belongs to the graph, u and v belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below.

    Dr. Evil gave Mahmoud and Ehab a tree consisting of n nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add?

    A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same .

    Input

    The first line of input contains an integer n — the number of nodes in the tree (1 ≤ n ≤ 105).

    The next n - 1 lines contain integers u and v (1 ≤ u, v ≤ nu ≠ v) — the description of the edges of the tree.

    It's guaranteed that the given graph is a tree.

    Output

    Output one integer — the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions.

    Examples
    input
    3
    1 2
    1 3
    output
    0
    input
    5
    1 2
    2 3
    3 4
    4 5
    output
    2
    In the first test case the only edge that can be added in such a way, that graph won't contain loops or multiple edges is (2, 3), but adding this edge will make the graph non-bipartite so the answer is 0.

    In the second test case Mahmoud and Ehab can add edges (1, 4) and (2, 5).

     

    题意:给一棵n个结点的树,问最多能加多少边使得其是二分图并且不能有重边和自环。

    思路:直接统计两部分的结点数,求出两部分结点的乘积减去n - 1条边即可

    代码:

     1 #include<bits/stdc++.h>
     2 #define db double
     3 #include<vector>
     4 #define ll long long
     5 #define vec vector<ll>
     6 #define Mt  vector<vec>
     7 #define ci(x) scanf("%d",&x)
     8 #define cd(x) scanf("%lf",&x)
     9 #define cl(x) scanf("%lld",&x)
    10 #define pi(x) printf("%d
    ",x)
    11 #define pd(x) printf("%f
    ",x)
    12 #define pl(x) printf("%lld
    ",x)
    13 const int N = 2e5 + 5;
    14 const int mod = 1e9 + 7;
    15 const int MOD = 998244353;
    16 const db  eps = 1e-18;
    17 const db  PI = acos(-1.0);
    18 using namespace std;
    19 vector<int> g[N];
    20 bool v[N];
    21 int cnt[2];
    22 void dfs(int u,int id)
    23 {
    24     v[u]=1;
    25     cnt[id]++;
    26     for(int i=0;i<g[u].size();i++){
    27         int vv=g[u][i];
    28         if(v[vv]) continue;
    29         dfs(vv,id^1);
    30     }
    31 }
    32 int main()
    33 {
    34     int n;
    35     ci(n);
    36     int x,y;
    37     for(int i=1;i<n;i++) ci(x),ci(y),g[x].push_back(y),g[y].push_back(x);
    38     dfs(1,0);
    39     pl(1ll*cnt[0]*cnt[1]-n+1);
    40     return 0;
    41 }
     
     
    C. Mahmoud and Ehab and the xor
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.

    Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set of n distinct non-negative integers such the bitwise-xor sum of the integers in it is exactly x. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than 106.

    Input

    The only line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the set and the desired bitwise-xor, respectively.

    Output

    If there is no such set, print "NO" (without quotes).

    Otherwise, on the first line print "YES" (without quotes) and on the second line print n distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.

    Examples
    input
    5 5
    output
    YES
    1 2 4 5 7
    input
    3 6
    output
    YES
    1 2 5
    Note

    You can read more about the bitwise-xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR

    For the first sample .

    For the second sample .

    题意

    寻找 n 个不同的数,且这些数的异或值等于 x 。

    思路

    开个脑洞就可以想到

    除了 n=2,x=0 时找不到结果,其他情况下都可以找到一组解。

    当 n=1 时显然直接输出 x 即可, n=2 时解为 0,x 。

    对于其他情况下,保留三个数,其中两个可以中和掉相应位,而另一个数对最终结果做出贡献。

    我们令 pr=1<<17 ,代表一个大于 n 的数,最终结果中我们假设包含 1,2,3...n3 ,且这些数的异或值为 y 。

    如果 x=y ,则说明这 n3 个数已经保证了答案,那剩下的三个数只要异或值等于 0 即可,于是很方便找到 pr(pr×2)(pr(pr×2))=0 。

    对于 x!=y 时,剩下的三个数 0pr(prxy) 可以保证它与之前的 y 异或等于 x 。

    代码:

     1 #include<bits/stdc++.h>
     2 #define db double
     3 #include<vector>
     4 #define ll long long
     5 #define vec vector<ll>
     6 #define Mt  vector<vec>
     7 #define ci(x) scanf("%d",&x)
     8 #define cd(x) scanf("%lf",&x)
     9 #define cl(x) scanf("%lld",&x)
    10 #define pi(x) printf("%d
    ",x)
    11 #define pd(x) printf("%f
    ",x)
    12 #define pl(x) printf("%lld
    ",x)
    13 const int N = 2e5 + 5;
    14 const int mod = 1e9 + 7;
    15 const int MOD = 998244353;
    16 const db  eps = 1e-18;
    17 const db  PI = acos(-1.0);
    18 using namespace std;
    19 int a[N];
    20 int main()
    21 {
    22 
    23     int n,x;
    24     ci(n),ci(x);
    25     if(n==1) puts("YES"),pi(x);
    26     else if(n==2){
    27         if(!x) puts("NO");
    28         else puts("YES"),printf("0 %d
    ",x);
    29     }
    30     else{
    31         int ans=0;
    32         int xx=(1<<17);
    33         puts("YES");
    34         for(int i=1;i<=n-3;i++){
    35             printf("%d ",i);
    36             ans^=i;
    37         }
    38         if(ans==x) printf("%d %d %d
    ",xx,xx*2,xx*3);
    39         else printf("0 %d %d
    ",xx^ans,xx^x);
    40     }
    41 
    42     return 0;
    43 }
  • 相关阅读:
    NVIDIA Jetson TX2刷机
    安装python2和3在centos7里面的问题
    js和DOM结合实现评论功能 (可以添加,删除)
    js实现计时
    js获取星期日期
    js登录界面演示
    下拉列表演示
    html表单练习
    一个底层w32汇编的小例子,演示 原创
    invoke和call的区别
  • 原文地址:https://www.cnblogs.com/mj-liylho/p/7764519.html
Copyright © 2011-2022 走看看