• 【比赛】HNOI2018 毒瘤



    虚树+dp

    直接看zlttttt的强大题解 zlttttt的题解看这里

    #include<bits/stdc++.h>
    #define ui unsigned int
    #define ll long long
    #define db double
    #define ld long double
    #define ull unsigned long long
    const int MAXN=100000+10,MAXM=100010+10,Mod=998244353;
    int n,m,e,to[MAXM<<1],nex[MAXM<<1],beg[MAXN],w[MAXM<<1],Jie[MAXN][20],dfn,dfsrk[MAXN],st[MAXN],ed[MAXN],dep[MAXN],cnt,Idfn,Icnt,Istack[MAXN],Ist[MAXN],Idfsrk[MAXN],slt[MAXN],Ipoint[MAXN],In,Ie,Ibeg[MAXN],Inex[MAXM<<1],Ito[MAXM<<1],f[MAXN][2],lmt[MAXN],g[MAXN][2];
    std::pair<int,int> stack[MAXM];
    struct data{
    	int k0,k1;
    	data(int a=0,int b=0){
    		k0=a,k1=b;
    	}
    	inline data operator + (const data &A) const {
    		return data(k0+A.k0,k1+A.k1);
    	};
    	inline data operator * (const int a) const {
    		return data(1ll*k0*a%Mod,1ll*k1*a%Mod);
    	};
    	inline int value(int a,int b){
    		return (1ll*a*k0%Mod+1ll*b*k1%Mod)%Mod;
    	};
    };
    data coeffi[MAXN][2];
    template<typename T> inline void read(T &x)
    {
    	T data=0,w=1;
    	char ch=0;
    	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    	if(ch=='-')w=-1,ch=getchar();
    	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
    	x=data*w;
    }
    template<typename T> inline void write(T x,char ch='')
    {
    	if(x<0)putchar('-'),x=-x;
    	if(x>9)write(x/10);
    	putchar(x%10+'0');
    	if(ch!='')putchar(ch);
    }
    template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
    template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
    template<typename T> inline T min(T x,T y){return x<y?x:y;}
    template<typename T> inline T max(T x,T y){return x>y?x:y;}
    inline void insert(int x,int y)
    {
    	to[++e]=y;
    	nex[e]=beg[x];
    	beg[x]=e;
    	w[e]=1;
    }
    inline void dfs1(int x,int f)
    {
    	dep[x]=dep[f]+1;Jie[x][0]=f;
    	dfsrk[st[x]=++dfn]=x;
    	for(register int i=beg[x];i;i=nex[i])
    		if(to[i]==f)w[i]=0;
    		else
    		{
    			if(!dep[to[i]])dfs1(to[i],x);
    			else w[i]=0,stack[++cnt]=std::pair<int,int>(min(x,to[i]),max(x,to[i]));
    		}
    	ed[x]=dfn;
    }
    inline bool cmp(int a,int b)
    {
    	return st[a]<st[b];
    }
    inline int LCA(int u,int v)
    {
    	if(dep[u]<dep[v])std::swap(u,v);
    	if(dep[u]>dep[v])
    		for(register int i=19;i>=0;--i)
    			if(dep[Jie[u][i]]>=dep[v])u=Jie[u][i];
    	if(u==v)return u;
    	for(register int i=19;i>=0;--i)
    		if(Jie[u][i]!=Jie[v][i])u=Jie[u][i],v=Jie[v][i];
    	return Jie[u][0];
    }
    inline void Iinsert(int x,int y)
    {
    	Ito[++Ie]=y;
    	Inex[Ie]=Ibeg[x];
    	Ibeg[x]=Ie;
    }
    inline void dfs3(int x,int tp)
    {
    	f[x][0]=f[x][1]=1;
    	for(register int i=beg[x];i;i=nex[i])
    		if(to[i]!=Jie[x][0]&&to[i]!=tp&&!slt[to[i]])
    		{
    			dfs3(to[i],tp);
    			f[x][0]=1ll*f[x][0]*((f[to[i]][0]+f[to[i]][1])%Mod)%Mod;
    			f[x][1]=1ll*f[x][1]*f[to[i]][0]%Mod;
    		}
    }
    inline void calc(int s,int t)
    {
    	coeffi[s][0]=data(1,0);coeffi[s][1]=data(0,1);
    	data now;
    	for(register int i=s;Jie[i][0]!=t;i=Jie[i][0])
    	{
    		dfs3(Jie[i][0],i);
    		slt[Jie[i][0]]=1;
    		now=coeffi[s][0];
    		coeffi[s][0]=(coeffi[s][0]+coeffi[s][1])*f[Jie[i][0]][0];
    		coeffi[s][1]=now*f[Jie[i][0]][1];
    	}
    }
    inline void dfs2(int x)
    {
    	for(register int i=Ibeg[x];i;i=Inex[i])dfs2(Ito[i]),calc(Ito[i],x);
    	f[x][0]=f[x][1]=1;
    	for(register int i=beg[x];i;i=nex[i])
    		if(!slt[to[i]]&&to[i]!=Jie[x][0]&&w[i]>0)
    		{
    			dfs3(to[i],0);
    			f[x][0]=1ll*f[x][0]*((f[to[i]][0]+f[to[i]][1])%Mod)%Mod;
    			f[x][1]=1ll*f[x][1]*f[to[i]][0]%Mod;
    		}
    }
    inline void dfs4(int x)
    {
    	g[x][0]=f[x][0];
    	g[x][1]=f[x][1];
    	for(register int i=Ibeg[x],k0,k1;i;i=Inex[i])
    	{
    		dfs4(Ito[i]);
    		k0=coeffi[Ito[i]][0].value(g[Ito[i]][0],g[Ito[i]][1]);
    		k1=coeffi[Ito[i]][1].value(g[Ito[i]][0],g[Ito[i]][1]);
    		g[x][0]=1ll*g[x][0]*((k0+k1)%Mod)%Mod;
    		g[x][1]=1ll*g[x][1]*k0%Mod;
    	}
    	if(~lmt[x])g[x][lmt[x]^1]=0;
    }
    int main()
    {
    	freopen("duliu.in","r",stdin);
    	freopen("duliu.out","w",stdout);
    	read(n);read(m);
    	for(register int i=1;i<=m;++i)
    	{
    		int u,v;
    		read(u);read(v);
    		insert(u,v);insert(v,u);
    	}
    	dep[1]=1;
    	dfs1(1,0);
    	for(register int j=1;j<20;++j)
    		for(register int i=1;i<=n;++i)Jie[i][j]=Jie[Jie[i][j-1]][j-1];
    	std::sort(stack+1,stack+cnt+1);
    	cnt=std::unique(stack+1,stack+cnt+1)-stack-1;
    	for(register int i=1;i<=cnt;++i)
    	{
    		if(!slt[stack[i].first])
    		{
    			slt[stack[i].first]=1;
    			Ipoint[++In]=stack[i].first;
    			Ist[Idfsrk[++Idfn]=stack[i].first]=Idfn;
    		}
    		if(!slt[stack[i].second])
    		{
    			slt[stack[i].second]=1;
    			Ipoint[++In]=stack[i].second;
    			Ist[Idfsrk[++Idfn]=stack[i].second]=Idfn;
    		}
    	}
    	std::sort(Ipoint+1,Ipoint+In+1,cmp);
    	for(register int i=2,limit=In;i<=limit;++i)Ipoint[++In]=LCA(Ipoint[i-1],Ipoint[i]);
    	Ipoint[++In]=1;
    	std::sort(Ipoint+1,Ipoint+In+1,cmp);
    	In=std::unique(Ipoint+1,Ipoint+In+1)-Ipoint-1;
    	Istack[++Icnt]=Ipoint[1];
    	for(register int i=2;i<=In;++i)
    		if(Icnt)
    		{
    			while(Icnt&&ed[Istack[Icnt]]<st[Ipoint[i]])Icnt--;
    			Iinsert(Istack[Icnt],Ipoint[i]);
    			Istack[++Icnt]=Ipoint[i];
    		}
    	for(register int i=1;i<=In;++i)slt[Ipoint[i]]=1;
    	dfs2(1);
    	for(register int i=1;i<=n;++i)lmt[i]=-1;
    	int ans=0;
    	for(register int i=(1<<Idfn)-1;i>=0;--i)
    	{
    		int mark=0;
    		for(register int j=1;j<=cnt;++j)
    			if((i>>(Ist[stack[j].first]-1)&1)&&(i>>(Ist[stack[j].second]-1)&1))
    			{
    				mark=1;
    				break;
    			}
    		if(mark)continue;
    		for(register int j=1;j<=Idfn;++j)lmt[Idfsrk[j]]=(i>>(j-1))&1;
    		dfs4(1);
    		(ans+=(g[1][0]+g[1][1])%Mod)%=Mod;
    	}
    	write(ans,'
    ');
    	return 0;
    }
    
  • 相关阅读:
    Jmeter 添加信息头
    Jmeter 添加 计数器
    Jmeter 用户定义的变量的使用
    Jmeter 获取系统时间
    Fiddler抓包工具使用技巧
    使用adb安装apk包到手机上的安装步骤:
    如何验证所使用的电脑Windows系统是正版还是盗版的方法
    VMware 15 虚拟机安装 win 7 操作系统步骤
    MySQL在windows上安装过程
    SecureCRT的下载、安装过程
  • 原文地址:https://www.cnblogs.com/hongyj/p/8907490.html
Copyright © 2020-2023  润新知