关于开O2就RE
查看原帖
关于开O2就RE
383524
阿陶陶陶楼主2022/2/3 11:51

求助,写了个树上DP,正常交 AC,开 O2 后莫名 RE,开大数组也没用。

#include<bits/stdc++.h>
typedef long long LL;
typedef long double LD;
using namespace std;
const int N=1e5+10,M=1e6+10,INF=0x3f3f3f3f;
inline int read(){
	char c=getchar();int x=0,f=1;
	while(c<'0'||'9'<c){if(c=='-')f=-1;c=getchar();}
	while('0'<=c&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}

struct edge
{
	int from,to,val,next;
	edge(){from=to=val=next=0;}
	edge(int f,int t,int v,int n):from(f),to(t),val(v),next(n){}
}e[N*2];
int n,cnt=1,head[N],ans,f[N][3];
bool vis[N];

inline int add(int f,int t,int v)
{
	e[++cnt]=edge(f,t,v,head[f]);
	head[f]=cnt;
}

int gcd(int a,int b)
{
	return b? gcd(b,a%b):a;
}

void dfs(int u)
{
	vis[u]=1;
	f[u][0]=1;
	for(int i=head[u];i;i=e[i].next)
	{
		int v=e[i].to,w=e[i].val;
		if(vis[v])	continue;
		dfs(v);
		ans+=f[u][0]*f[v][(3-w)%3];	f[u][0]+=f[v][(3-w)%3];
		ans+=f[u][1]*f[v][2-w];		f[u][1]+=f[v][(4-w)%3];
		ans+=f[u][2]*f[v][(4-w)%3];	f[u][2]+=f[v][2-w];
	}
	vis[u]=0;
}

int main()
{
	n=read();
	for(int i=1;i<n;++i)
	{
		int x=read(),y=read(),w=read();
		add(x,y,w%3);
		add(y,x,w%3);
	}
	dfs(1);
	int sum=n*n;	ans=ans*2+n;
	int g=gcd(ans,sum);
	printf("%d/%d\n",ans/g,sum/g);
	return 0;
}
/*
检查文件读写
检查空间大小
暴力优先
不要头铁
适当换题
*/
2022/2/3 11:51
加载中...