• Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】


    传送门:http://codeforces.com/contest/1093/problem/D

    D. Beautiful Graph

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given an undirected unweighted graph consisting of nn vertices and mm edges.

    You have to write a number on each vertex of the graph. Each number should be 11, 22 or 33. The graph becomes beautiful if for each edge the sum of numbers on vertices connected by this edge is odd.

    Calculate the number of possible ways to write numbers 11, 22 and 33 on vertices so the graph becomes beautiful. Since this number may be large, print it modulo 998244353998244353.

    Note that you have to write exactly one number on each vertex.

    The graph does not have any self-loops or multiple edges.

    Input

    The first line contains one integer tt (1t31051≤t≤3⋅105) — the number of tests in the input.

    The first line of each test contains two integers nn and mm (1n3105,0m31051≤n≤3⋅105,0≤m≤3⋅105) — the number of vertices and the number of edges, respectively. Next mm lines describe edges: ii-th line contains two integers uiui, vivi (1ui,vin;uivi1≤ui,vi≤n;ui≠vi) — indices of vertices connected by ii-th edge.

    It is guaranteed that i=1tn3105∑i=1tn≤3⋅105 and i=1tm3105∑i=1tm≤3⋅105.

    Output

    For each test print one line, containing one integer — the number of possible ways to write numbers 11, 22, 33 on the vertices of given graph so it becomes beautiful. Since answers may be large, print them modulo 998244353998244353.

    Example
    input
    Copy
    2
    2 1
    1 2
    4 6
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4
    
    output
    Copy
    4
    0
    
    Note

    Possible ways to distribute numbers in the first test:

    1. the vertex 11 should contain 11, and 22 should contain 22;
    2. the vertex 11 should contain 33, and 22 should contain 22;
    3. the vertex 11 should contain 22, and 22 should contain 11;
    4. the vertex 11 should contain 22, and 22 should contain 33.

    In the second test there is no way to distribute numbers.

    题意概括:

    给一个无向图,要求在每个结点填入 1,2,3 三个数的其中一个,如果每条边相连的两个结点的权值之和为奇数,则当前的填数方案是满足条件的;

    询问有多少种填数方案,如果不存在则输出0;

    解题思路:

    因为只有 1, 2, 3 三个数,其中有两个奇数, 一个偶数。为了满足题目的条件,我们的填入规则肯定是在一条路径上奇偶奇偶...这样填入数字,保证相连两点的和为奇数。

    那么就有该路径是先填奇数还是先填偶数之分了:

    如果是在该路径上的第一点填入奇数,那么由这个点出发的路径的偶数点肯定是要填偶数,那么我们统计填入奇数的点(即奇数点)的个数 p ,每个点可填两种数,方案有 2 的 p 次方种。

    如果在该路径的第一点填入偶数,那么偶数点要填奇数(1或者3),统计填入奇数的点(即偶数点)的个数 x,每个点有两种选择,方案数为 2 的 x 次方。

    那么总的方案数就是上面两种情况的方案数之和。

    由此,我们发现统计路径的奇数点和偶数点,分别以他们求2次幂之和,就是以当前点出发所能经过路径的方案数了。枚举一遍起点,求出总的方案数就是答案。

    那么无解的情况呢,就是存在起点和终点奇偶性相同的环(DFS过程中判断一下即可)。

    注意点:

    一、2次幂可先预处理

    二、一开始敲得静态邻接表版本要注意初始化得方式 for循环可过,效率客观(比vector版本快些)。memset初始化超时。

    三、vector版本的for循环初始化即可。

    AC code:

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <vector>
     6 #include <map>
     7 #define LL long long
     8 #define INF 0x3f3f3f3f
     9 using namespace std;
    10 const LL MOD = 998244353;
    11 int N, M, p, s;
    12 LL ans;
    13 const int MAXN = 3e5+10;
    14 int book[MAXN];
    15 LL pw[MAXN+1];
    16 bool con;
    17 //bool vis[MAXN];
    18 struct EDGE
    19 {
    20     int v, nxt;
    21 }edge[MAXN<<1];
    22 int head[MAXN], cnt;
    23 
    24 void add(int u, int v)
    25 {
    26     edge[cnt].v = v;
    27     edge[cnt].nxt = head[u];
    28     head[u] = cnt++;
    29 }
    30 
    31 void init()
    32 {
    33 //    memset(book, -1, sizeof(book));
    34 //    memset(head, -1, sizeof(head));
    35     cnt = 0;
    36     con = true;
    37     ans = 1LL;
    38 }
    39 
    40 void dfs(int now, int flg)
    41 {
    42     if(flg == 1) p++;
    43     else s++;
    44 
    45     book[now] = flg;
    46     int v;
    47     for(int i = head[now]; i != -1; i = edge[i].nxt){
    48         v = edge[i].v;
    49         if(book[v] == -1){
    50             dfs(v, 1-flg);
    51         }
    52         else if(book[v] == flg) {con = 0; return;}
    53     }
    54 }
    55 
    56 int main()
    57 {
    58     int T_case, u, v;
    59     scanf("%d", &T_case);
    60     pw[0] = 1;
    61     for(int i = 1; i < MAXN; i++){
    62         pw[i] = (pw[i-1]*2)%MOD;
    63     }
    64     memset(head, -1, sizeof(head));
    65     memset(book, -1, sizeof(book));
    66     while(T_case--)
    67     {
    68         init();
    69         scanf("%d%d", &N, &M);
    70         for(int i = 1; i <= M; i++){
    71             scanf("%d %d", &u, &v);
    72             add(u, v);
    73             add(v, u);
    74         }
    75         for(int st = 1; st <= N; st++){
    76             if(book[st] != -1) continue;
    77             else if(book[st] == -1){
    78                 p = 0, s = 0;
    79                 dfs(st, 1);
    80                 ans = ans*(pw[p]+pw[s])%MOD;
    81             }
    82         }
    83 
    84         if(con) printf("%I64d
    ", ans);
    85         else printf("0
    ");
    86 
    87         for(int i = 0; i <= N; i++){
    88             head[i] = -1;
    89             book[i] = -1;
    90         }
    91     }
    92     return 0;
    93 }
  • 相关阅读:
    servlet 和filter 抛出404等异常
    cassandra 存储list数组
    cassandra 存储二进制data
    ubuntu下安装cpython 0.2x
    mongodb存储二进制数据的二种方式——binary bson或gridfs
    Python中使用Flask、MongoDB搭建简易图片服务器
    kubernetes要实现的目标——随机关掉一台机器,看你的服务能否正常;减少的应用实例能否自动迁移并恢复到其他节点;服务能否随着流量进行自动伸缩
    陈斌:探索技术领导力的最佳实践
    python berkeley DB操作——打开btree索引文件中的database
    什么是AWS Lambda?——事件驱动的函数执行环境
  • 原文地址:https://www.cnblogs.com/ymzjj/p/10127952.html
Copyright © 2020-2023  润新知