Description
Input
Output
Sample Input
6 10
4 6
4 5
3 6
5 2
3 2
1 2
3 4
6 1
2 4
1 3
Sample Output
2132
Data Constraint
分析:
显然至少每条边走一次,在这基础上,所有的奇点都要走一条路径到另外一个奇点。相当于把奇点两两配对,配对代价是他们的最短路,然后我就不会了。
最后发现两点的最短路一定在最小生成树上。因为第
i
i
i条边代价为
2
i
2^i
2i,所以两个点走前面的路径能连通,那么最短路就确定了。
于是我们相当于在最小生成树上,有一些奇点,两两配对的最小代价,直接贪心即可。
代码:
#include <iostream>
#include <cmath>
#include <cstdio>
const int maxn=5e5+7;
const int mod=998244353;
using namespace std;
int n,m,power,ans,cnt,x,y;
int ls[maxn],p[maxn],r[maxn],sum[maxn];
struct edge{
int y,w,next;
}g[maxn*2];
int find(int x)
{
if (!p[x]) return x;
return p[x]=find(p[x]);
}
void uni(int x,int y)
{
int u=find(x),v=find(y);
if (u==v) return;
p[u]=v;
}
int dfs(int x,int fa)
{
sum[x]=r[x];
for (int i=ls[x];i>0;i=g[i].next)
{
int y=g[i].y;
if (y==fa) continue;
dfs(y,x);
sum[x]+=sum[y];
if (sum[y]%2) ans=(ans+g[i].w)%mod;
}
}
void add(int x,int y,int w)
{
g[++cnt]=(edge){y,w,ls[x]};
ls[x]=cnt;
}
int main()
{
freopen("travel.in","r",stdin);
freopen("travel.out","w",stdout);
scanf("%d%d",&n,&m);
power=1;
for (int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
power=(power*2)%mod;
if (find(x)!=find(y))
{
add(x,y,power);
add(y,x,power);
uni(x,y);
}
r[x]++;
r[y]++;
ans=(ans+power)%mod;
}
dfs(1,0);
printf("%d",ans);
}