3697: 采药人的路径
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1131 Solved: 393
[ Submit][ Status][ Discuss]
Description
采药人的药田是一个树状结构,每条路径上都种植着同种药材。
采药人以自己对药材独到的见解,对每种药材进行了分类。大致分为两类,一种是阴性的,一种是阳性的。
采药人每天都要进行采药活动。他选择的路径是很有讲究的,他认为阴阳平衡是很重要的,所以他走的一定是两种药材数目相等的路径。采药工作是很辛苦的,所以他希望他选出的路径中有一个可以作为休息站的节点(不包括起点和终点),满足起点到休息站和休息站到终点的路径也是阴阳平衡的。他想知道他一共可以选择多少种不同的路径。
Input
第1行包含一个整数N。
接下来N-1行,每行包含三个整数a_i、b_i和t_i,表示这条路上药材的类型。
Output
输出符合采药人要求的路径数目。
Sample Input
7
1 2 0
3 1 1
2 4 0
5 2 0
6 3 1
5 7 1
1 2 0
3 1 1
2 4 0
5 2 0
6 3 1
5 7 1
Sample Output
1
HINT
对于100%的数据,N ≤ 100,000。
Source
【分析】
Orz,想不出来啊QAQ
【代码】
//bzoj 3697 采药人的路径
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=200005;
bool vis[mxn];
int n,m,mn,cnt,tot,root,mxdep;
ll h[mxn][2],g[mxn][2],ans;
struct edge {int to,w,next;} f[mxn<<1];
int head[mxn],size[mxn],dis[mxn],mx[mxn],dep[mxn],t[mxn];
inline void add(int u,int v,int w)
{
f[++cnt].to=v,f[cnt].w=w,f[cnt].next=head[u],head[u]=cnt;
}
inline void dfssize(int u,int fa)
{
size[u]=1,mx[u]=0;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v] || v==fa) continue;
dfssize(v,u);
size[u]+=size[v];
mx[u]=max(mx[u],size[v]);
}
}
inline void dfsroot(int r,int u,int fa)
{
mx[u]=max(mx[u],size[r]-size[u]);
if(mx[u]<mn) mn=mx[u],root=u;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v] || v==fa) continue;
dfsroot(r,v,u);
}
}
inline void dfsdis(int u,int fa)
{
mxdep=max(mxdep,dep[u]);
if(t[dis[u]]) h[dis[u]][1]++;
else h[dis[u]][0]++;
t[dis[u]]++;
for(int i=head[u];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v] || v==fa) continue;
dis[v]=dis[u]+f[i].w,dep[v]=dep[u]+1;
dfsdis(v,u);
}
t[dis[u]]--;
}
inline void calc(int u)
{
tot=0,mxdep=0;g[n][0]=1; //当前节点u
for(int i=head[u];i;i=f[i].next)
{
mxdep=0;
int v=f[i].to;
if(vis[v]) continue;
dep[v]=1,dis[v]=n+f[i].w;
dfsdis(v,u),tot=max(tot,mxdep);
ans+=(g[n][0]-1)*h[n][0]; //不含u
for(int j=-mxdep;j<=mxdep;j++)
ans+=g[n-j][1]*h[n+j][0]+g[n-j][0]*h[n+j][1]+g[n-j][1]*h[n+j][1];
for(int j=n-mxdep;j<=n+mxdep;j++)
{
g[j][0]+=h[j][0];
g[j][1]+=h[j][1];
h[j][0]=h[j][1]=0;
}
}
for(int i=n-tot;i<=n+tot;i++)
g[i][0]=g[i][1]=0;
}
inline void dfs(int u)
{
mn=n;
dfssize(u,0);
dfsroot(u,u,0);
calc(root);
vis[root]=1;
for(int i=head[root];i;i=f[i].next)
{
int v=f[i].to;
if(vis[v]) continue;
dfs(v);
}
}
int main()
{
int i,j,u,v,w;
scanf("%d",&n);
fo(i,2,n)
{
scanf("%d%d%d",&u,&v,&w);
if(!w) w=-1;
add(u,v,w),add(v,u,w);
}
dfs(1);
printf("%lld\n",ans);
return 0;
}