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 \(U_{i}\) and Vertex \(V_{i}\).
You are given integers \(K, S, T\), and \(X\). How many sequences \(A=\left(A_{0}, A_{1}, \ldots, A_{K}\right)\) are there satisfying the following conditions?

  • \(A_{i}\) is an integer between 1 and \(N\) (inclusive).
  • \(A_{0}=S\)
  • \(A_{K}=T\)
  • There is an edge that directly connects Vertex \(A_{i}\) and Vertex \(A_{i+1}\).
  • Integer \(X(X \neq S, X \neq T)\) 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.
  • \(2 \leq N \leq 2000\)
  • \(1 \leq M \leq 2000\)
  • \(1 \leq K \leq 2000\)
  • \(1 \leq S, T, X \leq N\)
  • \(X \neq S\)
  • \(X \neq T\)
  • \(1 \leq U_{i}<V_{i} \leq N\)
  • If \(i \neq j\), then \(\left(U_{i}, V_{i}\right) \neq\left(U_{j}, V_{j}\right)\).

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]\)
      分析:从 \(v\)\(u\) 经过了 \(k\) (不包括 \(u\))次的 \(x\),如果 \(u=x\),则经过 \(x\) 的奇偶性改变,从 \(u\)\(v\) 同理
  • 时间复杂度:\(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;
}
posted @ 2022-03-21 11:36  zyy2001  阅读(74)  评论(0编辑  收藏  举报