标签:
Description
Input
Output
Sample Input
6 2 5 3 7 4 1 0 0 5 2 6 3 0 0 0 0 1 8 13 14 0 0
Sample Output
AYE AYE NAY AYE .
询问树上有没有长度为k的路径……对于询问一个一个点分治……反正n才10000m才100
输入好蛋疼……第一行n,接下来n行,每两个数ci、di,表示i和ci有长度为di的边,以0结束。接下来输入询问,以0结束。又有多组数据,以0结束
#include<cstdio> #include<iostream> #include<cstring> #define LL long long #define N 40010 #define mod 10207 using namespace std; inline LL read() { LL x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } struct edge{int to,next,v;}e[2*N]; struct hashing{int v,next;}hash[2*N]; int he[N],head[N]; int f[N],son[N],s[N],d[N],query[N]; bool vis[N],ok[N]; int n,m,cnt,tot,root,sum,len,ans; inline void ins(int u,int v,int w) { e[++cnt].to=v; e[cnt].v=w; e[cnt].next=head[u]; head[u]=cnt; } inline void insert(int u,int v,int w) { ins(u,v,w); ins(v,u,w); } inline void inh(int u) { int w=u%mod+132; hash[++tot].v=u; hash[tot].next=he[w]; he[w]=tot; } inline bool fnd(int u) { if (u<0)return 0; int w=u%mod+132; for (int i=he[w];i;i=hash[i].next) if (u==hash[i].v)return 1; return 0; } inline void getroot(int x,int fa) { son[x]=1;f[x]=0; for (int i=head[x];i;i=e[i].next) if (fa!=e[i].to&&!vis[e[i].to]) { getroot(e[i].to,x); son[x]+=son[e[i].to]; f[x]=max(f[x],son[e[i].to]); } f[x]=max(f[x],sum-son[x]); if (f[x]<f[root])root=x; } inline void dfs(int x,int fa) { s[++len]=d[x]; for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]&&fa!=e[i].to) { d[e[i].to]=d[x]+e[i].v; dfs(e[i].to,x); } } inline void solve(int x) { vis[x]=1;tot=0; memset(he,0,sizeof(he));inh(0); for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) { len=0; d[e[i].to]=e[i].v; dfs(e[i].to,0); for (int j=1;j<=m;j++) { if (ok[j])continue; for (int k=1;k<=len;k++) if (fnd(query[j]-s[k])) { ok[j]=1; break; } } for (int j=1;j<=len;j++)inh(s[j]); } for (int i=head[x];i;i=e[i].next) if (!vis[e[i].to]) { sum=son[e[i].to]; root=0; getroot(e[i].to,0); solve(root); } } inline void work() { memset(head,0,sizeof(head)); memset(vis,0,sizeof(vis)); memset(ok,0,sizeof(ok)); cnt=ans=0; for(int i=1;i<=n;i++) { int x=read(),y; while (x) { y=read(); insert(i,x,y); x=read(); } } m=0; int xx=read(); while (xx) { query[++m]=xx; xx=read(); } f[0]=n+1;sum=n;root=0; getroot(1,0); solve(root); for (int i=1;i<=m;i++) if (ok[i])printf("AYE\n");else printf("NAY\n"); printf(".\n"); } int main() { while (~scanf("%d",&n)&&n)work(); }
标签:
原文地址:http://www.cnblogs.com/zhber/p/4216260.html