2017.11.26【清华集训2017】模拟
T1 5483. 【清华集训2017模拟11.26】简单路径
T2 5484. 【清华集训2017模拟11.26】快乐树
T3 5485. 【清华集训2017模拟11.26】字符串
T1 结论题,结论很显然任意两条路径权异或后,会将两条路径的交的贡献删去。
然后用个桶存一下出现过的异或和,暴力判一下就可以了
code
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 #define fo(i,a,b) for(int i=a;i<=b;i++) 6 #define fd(i,a,b) for(int i=a;i>=b;i--) 7 #define fh(i,x) for(int i=head[x];i;i=next[i]) 8 typedef long long LL; 9 using namespace std; 10 inline int max(int x,int y) {return (x<y)?y:x;} 11 inline int min(int x,int y) {return (x<y)?x:y;} 12 inline int read() { 13 int x=0,f=1;char ch=getchar(); 14 while(ch<'0'||ch>'9') f=(ch=='-')?-1:f,ch=getchar(); 15 while(ch>='0'&&ch<='9') x=x*10+(ch-'0'),ch=getchar();return f*x; 16 } 17 const int N=1e3+100; 18 struct node { 19 int _lca,v; 20 node() {} 21 node(int a,int b):_lca(a),v(b) {} 22 }; 23 struct node1 { 24 int val,u,v,_lca; 25 node1() {} 26 node1(int _val,int _u,int _v,int l):val(_val),u(_u),v(_v),_lca(l){} 27 }to1[N*N],g[N*N]; 28 int to[N],w[N],next[N],head[N],tot,cnt,mx; 29 int tot1,next1[N*N],head1[N*N],fa[N],ans; 30 int dep[N],go[N][20],gw[N][20],n,x; 31 void add(int x,int y,int ww) {to[++tot]=y,w[tot]=ww,next[tot]=head[x],head[x]=tot;} 32 void add1(int val,int u,int v,int _lca) {to1[++tot1]=node1(val,u,v,_lca),next1[tot1]=head1[val],head1[val]=tot1;} 33 void dfs(int x) { 34 fh(i,x) { 35 dep[to[i]]=dep[x]+1,go[to[i]][0]=x,gw[to[i]][0]=w[i]; 36 dfs(to[i]); 37 } 38 } 39 void init_lca() { 40 dep[0]=1,dfs(0); 41 fo(i,1,17) fo(j,0,n-1) go[j][i]=go[go[j][i-1]][i-1],gw[j][i]=gw[j][i-1]^gw[go[j][i-1]][i-1]; 42 } 43 node lca(int a,int b) { 44 if(dep[a]>dep[b]) swap(a,b); 45 int f=dep[b]-dep[a],z=0; 46 fo(i,0,17) if(f&(1<<i)) z=z^gw[b][i],b=go[b][i]; 47 if(a==b) return node(a,z); 48 fo(i,0,17) { 49 if(go[a][i]!=go[b][i]) z^=gw[a][i]^gw[b][i],a=go[a][i],b=go[b][i]; 50 } 51 return node(go[a][0],z^gw[a][0]^gw[b][0]); 52 } 53 int main() { 54 n=read(); 55 fo(i,1,n-1) fa[i]=read(); 56 fo(i,1,n-1) x=read(),add(fa[i],i,x); 57 init_lca(); 58 fo(i,0,n-1) { 59 fo(j,i+1,n-1) { 60 node tmp=lca(i,j); 61 add1(tmp.v,i,j,tmp._lca); 62 g[++cnt]=node1(tmp.v,i,j,tmp._lca); 63 mx=max(mx,tmp.v); 64 } 65 } 66 fd(i,2048,mx) { 67 fo(j,1,cnt) { 68 if(!head1[i^g[j].val])continue; 69 else { 70 printf("%d\n",i);return 0; 71 } 72 } 73 } 74 printf("%d\n",max(mx,ans)); 75 return 0; 76 }
T2 算是一道dp题吧?有一个结论:“0”的影响范围一定是包括0的联通块。
先做一遍不考虑有“0”的dp,f[x]表示以x为子树的最优解。
然后g[x]表示x在“0”的联通块里,则g[x]=Σmax(f[son1],g[son1])
ans=max(f[0],g[0]);
Code
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 #define fo(i,a,b) for(int i=a;i<=b;i++) 6 #define fd(i,a,b) for(int i=a;i>=b;i--) 7 #define fh(i,x) for(int i=head[x];i;i=next[i]) 8 typedef long long LL; 9 using namespace std; 10 inline int max(int x,int y) {return (x<y)?y:x;} 11 inline int min(int x,int y) {return (x<y)?x:y;} 12 inline int read() { 13 int x=0,f=1;char ch=getchar(); 14 while(ch<'0'||ch>'9') f=(ch=='-')?-1:f,ch=getchar(); 15 while(ch>='0'&&ch<='9') x=x*10+(ch-'0'),ch=getchar();return f*x; 16 } 17 const int N=1e3+50; 18 int f[N],g[N],val[N],n,fa[N]; 19 int to[N],next[N],head[N],tot; 20 void add(int x,int y) {to[++tot]=y,next[tot]=head[x],head[x]=tot;} 21 void dfs(int x) { 22 f[x]=val[x]; 23 fh(i,x) dfs(to[i]); 24 fh(i,x) f[x]+=max(f[to[i]],0); 25 } 26 void dp(int x) { 27 fh(i,x) dp(to[i]); 28 fh(i,x) g[x]+=max(f[to[i]],g[to[i]]); 29 } 30 int main() { 31 n=read(); 32 fo(i,1,n-1) fa[i]=read(),add(fa[i],i); 33 fo(i,0,n-1) val[i]=read(); 34 dfs(0),dp(0); 35 printf("%d\n",max(f[0],g[0])); 36 return 0; 37 }
T3
显然dp,设dp[i][j],i表示做到了第i位,且当前贡献为k'+j的最小分段数。
可以O(26)转移方程预处理出在i点的右推贡献,贪心转移。
Code
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 #define fo(i,a,b) for(int i=a;i<=b;i++) 6 #define fd(i,a,b) for(int i=a;i>=b;i--) 7 #define fh(i,x) for(int i=head[x];i;i=next[i]) 8 typedef long long LL; 9 using namespace std; 10 inline int max(int x,int y) {return (x<y)?y:x;} 11 inline int min(int x,int y) {return (x<y)?x:y;} 12 inline int read() { 13 int x=0,f=1;char ch=getchar(); 14 while(ch<'0'||ch>'9') f=(ch=='-')?-1:f,ch=getchar(); 15 while(ch>='0'&&ch<='9') x=x*10+(ch-'0'),ch=getchar();return f*x; 16 } 17 const int N=1e5+50,inf=0x3f3f3f3f; 18 char s[N]; 19 int g[N][27],b[27],cnt[N][27],f[N][27],l,r,n,m,now; 20 int main() { 21 //freopen("c.in","r",stdin),freopen("c.out","w",stdout); 22 n=read(),m=read(); 23 scanf("%s",s+1); 24 fo(i,1,n) cnt[i][s[i]-'a']++; 25 fo(j,0,25) fo(i,1,n) cnt[i][j]+=cnt[i-1][j]; 26 fo(j,1,26) g[0][j]=1; 27 fo(i,1,n) { 28 int t=s[i]-'a'; 29 fo(j,1,26) b[j]=n+1; 30 fo(j,1,26) { 31 if(cnt[i-1][t]-cnt[g[i-1][j]-1][t]==0) b[j+1]=min(b[j+1],g[i-1][j]); 32 else b[j]=min(b[j],g[i-1][j]); 33 } 34 b[1]=min(b[1],i); 35 fo(j,1,26) g[i][j]=b[j]; 36 } 37 fo(i,0,27) f[1][i]=1; 38 fo(i,2,n) { 39 fo(j,0,26) f[i][j]=n+1; 40 fo(j,0,26) { 41 fo(t,1,26) { 42 l=g[i][t],l--; 43 (t==1)?r=i:r=g[i][t-1]; 44 r--; 45 fo(st,0,j+1-t) f[i][j]=min(f[i][j],f[l][st]+1); 46 } 47 } 48 if(i<n) fo(j,1,26) f[i][j]=min(f[i][j],f[i][j-1]); 49 } 50 fd(j,26,0) { 51 if(f[n][j]<=m) now=j; 52 else break; 53 } 54 printf("%d\n",m+now); 55 }