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

本文解析了 Codeforces 上的问题 D. Beautiful Graph,这是一个关于无向图中填数的算法问题。任务是在图的每个顶点上写入1、2或3,使得所有边的相邻顶点数值之和为奇数,最终计算满足条件的填数方案数量,并给出了解题思路和 AC 代码。

传送门: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\n", ans);
85         else printf("0\n");
86 
87         for(int i = 0; i <= N; i++){
88             head[i] = -1;
89             book[i] = -1;
90         }
91     }
92     return 0;
93 }

 

内容概要:本文介绍了ENVI Deep Learning V1.0的操作教程,重点讲解了如何利用ENVI软件进行深度学习模型的训练与应用,以实现遥感图像中特定目标(如集装箱)的自动提取。教程涵盖了从数据准备、标签图像创建、模型初始化与训练,到执行分类及结果优化的完整流程,并介绍了精度评价与通过ENVI Modeler实现一键化建模的方法。系统基于TensorFlow框架,采用ENVINet5(U-Net变体)架构,支持通过点、线、面ROI或分类图生成标签数据,适用于多/高光谱影像的单一类别特征提取。; 适合人群:具备遥感图像处理基础,熟悉ENVI软件操作,从事地理信息、测绘、环境监测等相关领域的技术人员或研究人员,尤其是希望将深度学习技术应用于遥感目标识别的初学者与实践者。; 使用场景及目标:①在遥感影像中自动识别和提取特定地物目标(如车辆、建筑、道路、集装箱等);②掌握ENVI环境下深度学习模型的训练流程与关键参数设置(如Patch Size、Epochs、Class Weight等);③通过模型调优与结果反馈提升分类精度,实现高效自动化信息提取。; 阅读建议:建议结合实际遥感项目边学边练,重点关注标签数据制作、模型参数配置与结果后处理环节,充分利用ENVI Modeler进行自动化建模与参数优化,同时注意软硬件环境(特别是NVIDIA GPU)的配置要求以保障训练效率。
内容概要:本文系统阐述了企业新闻发稿在生成式引擎优化(GEO)时代下的全渠道策略与效果评估体系,涵盖当前企业传播面临的预算、资源、内容与效果评估四大挑战,并深入分析2025年新闻发稿行业五大趋势,包括AI驱动的智能化转型、精准化传播、首发内容价值提升、内容资产化及数据可视化。文章重点解析央媒、地方官媒、综合门户和自媒体四类媒体资源的特性、传播优势与发稿策略,提出基于内容适配性、时间节奏、话题设计的策略制定方法,并构建涵盖品牌价值、销售转化与GEO优化的多维评估框架。此外,结合“传声港”工具实操指南,提供AI智能投放、效果监测、自媒体管理与舆情应对的全流程解决方案,并针对科技、消费、B2B、区域品牌四大行业推出定制化发稿方案。; 适合人群:企业市场/公关负责人、品牌传播管理者、数字营销从业者及中小企业决策者,具备一定媒体传播经验并希望提升发稿效率与ROI的专业人士。; 使用场景及目标:①制定科学的新闻发稿策略,实现从“流量思维”向“价值思维”转型;②构建央媒定调、门户扩散、自媒体互动的立体化传播矩阵;③利用AI工具实现精准投放与GEO优化,提升品牌在AI搜索中的权威性与可见性;④通过数据驱动评估体系量化品牌影响力与销售转化效果。; 阅读建议:建议结合文中提供的实操清单、案例分析与工具指南进行系统学习,重点关注媒体适配性策略与GEO评估指标,在实际发稿中分阶段试点“AI+全渠道”组合策略,并定期复盘优化,以实现品牌传播的长期复利效应。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值