AtCoder Beginner Contest 244

题目链接

AtCoder Beginner Contest 244

E - King Bombee

Problem Statement

You are given a simple undirected graph with N vertices and M edges. The vertices are numbered from 1 through N, and the edges are numbered from 1 through M. Edge i connects Vertex Ui and Vertex Vi.
You are given integers K,S,T, and X. How many sequences A=(A0,A1,,AK) are there satisfying the following conditions?

  • Ai is an integer between 1 and N (inclusive).
  • A0=S
  • AK=T
  • There is an edge that directly connects Vertex Ai and Vertex Ai+1.
  • Integer X(XS,XT) appears even number of times (possibly zero) in sequence A.
    Since the answer can be very large, find the answer modulo 998244353 .

Constraints

  • All values in input are integers.
  • 2N2000
  • 1M2000
  • 1K2000
  • 1S,T,XN
  • XS
  • XT
  • 1Ui<ViN
  • If ij, then (Ui,Vi)(Uj,Vj).

Input

Input is given from Standard Input in the following format:

N M K S T X U1 V1 U2 V2 . . . UM VM

Output

Print the answer modulo 998244353.

Sample Input 1

4 4 4 1 3 2 1 2 2 3 3 4 1 4

Sample Output 1

4

The following 4 sequences satisfy the conditions:

  • (1,2,1,2,3)
  • (1,2,3,2,3)
  • (1,4,1,4,3)
  • (1,4,3,4,3)
    On the other hand, (1,2,3,4,3) and (1,4,1,2,3) do not, since there are odd number of occurrences of 2 .

Sample Input 2

6 5 10 1 2 3 2 3 2 4 4 6 3 6 1 5

Sample Output 2

0

The graph is not necessarily connected.

Sample Input 3

10 15 20 4 4 6 2 6 2 7 5 7 4 5 2 4 3 7 1 7 1 4 2 9 5 10 1 3 7 8 7 9 1 6 1 2

Sample Output 3

952504739

Find the answer modulo 998244353.

解题思路

dp

  • 状态表示:f[i][j][k] 表示从 S 出发走了 i 步,到达 j,路径中经过奇(k=1)/偶数次(k=0)的 X 的方案数

  • 状态计算:

    • f[i+1][u][k(u==x)]+=f[i][v][k]
    • f[i+1][v][k(v==x)]+=f[i][u][k]
      分析:从 vu 经过了 k (不包括 u)次的 x,如果 u=x,则经过 x 的奇偶性改变,从 uv 同理
  • 时间复杂度:O((n+m)k)

代码

// %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=2005,mod=998244353; int n,m,k,s,t,x; int f[N][N][2]; vector<PII> edge; int main() { cin>>n>>m>>k>>s>>t>>x; while(m--) { int a,b; cin>>a>>b; edge.pb({a,b}); } f[0][s][0]=1; for(int i=0;i<k;i++) { for(auto t:edge) { int u=t.fi,v=t.se; for(int j=0;j<2;j++) { f[i+1][u][j^(u==x)]=(f[i+1][u][j^(u==x)]+f[i][v][j])%mod; f[i+1][v][j^(v==x)]=(f[i+1][v][j^(v==x)]+f[i][u][j])%mod; } } } cout<<f[k][t][0]; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16033968.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(80)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示