zoukankan      html  css  js  c++  java
  • Palindromes in a Tree CodeForces

    https://vjudge.net/problem/CodeForces-914E

    点分就没一道不卡常的?

    卡常记录:

    1.把不知道为什么设的(unordered_map)s换成了(int[])s

    2.减少一次cal2和clr

      1 #pragma GCC optimize("Ofast")
      2 #include<cstdio>
      3 #include<algorithm>
      4 #include<cstring>
      5 #include<vector>
      6 using namespace std;
      7 #define fi first
      8 #define se second
      9 #define mp make_pair
     10 #define pb push_back
     11 typedef long long ll;
     12 typedef unsigned long long ull;
     13 typedef pair<int,int> pi;
     14 struct E
     15 {
     16     int to,nxt;
     17 }e[400100];
     18 int f1[200100],ne;
     19 int sz[200100],fx[200100],d[200100];ll ans[200100],ta,an1[200100];
     20 char ss[200100];int a[200100];
     21 bool vis[200100];
     22 int root,sum;
     23 int lft[23];
     24 int s[2001000];
     25 void getroot(int x,int fa)
     26 {
     27     sz[x]=1;fx[x]=0;
     28     for(int k=f1[x];k;k=e[k].nxt)
     29         if(!vis[e[k].to]&&e[k].to!=fa)
     30         {
     31             getroot(e[k].to,x);
     32             sz[x]+=sz[e[k].to];
     33             fx[x]=max(fx[x],sz[e[k].to]);
     34         }
     35     fx[x]=max(fx[x],sum-sz[x]);
     36     if(fx[x]<fx[root])    root=x;
     37 }
     38 void getsz(int x,int fa)
     39 {
     40     sz[x]=1;
     41     for(int k=f1[x];k;k=e[k].nxt)
     42         if(!vis[e[k].to]&&e[k].to!=fa)
     43         {
     44             getsz(e[k].to,x);
     45             sz[x]+=sz[e[k].to];
     46         }
     47 }
     48 void calc(int u,int fa)
     49 {
     50     an1[u]+=s[d[u]^a[root]],ta+=s[d[u]^a[root]];
     51     for(int i=0;i<20;i++)    an1[u]+=s[d[u]^a[root]^lft[i]],ta+=s[d[u]^a[root]^lft[i]];
     52     for(int k=f1[u];k;k=e[k].nxt)
     53         if(!vis[e[k].to]&&e[k].to!=fa)
     54             calc(e[k].to,u);
     55 }
     56 void addd(int u,int fa)
     57 {
     58     s[d[u]]++;
     59     for(int k=f1[u];k;k=e[k].nxt)
     60         if(!vis[e[k].to]&&e[k].to!=fa)
     61         {
     62             d[e[k].to]=d[u]^a[e[k].to];
     63             addd(e[k].to,u);
     64         }
     65 }
     66 void deld(int u,int fa)
     67 {
     68     s[d[u]]--;
     69     for(int k=f1[u];k;k=e[k].nxt)
     70         if(!vis[e[k].to]&&e[k].to!=fa)
     71             deld(e[k].to,u);
     72 }
     73 void cal2(int u,int fa)
     74 {
     75     for(int k=f1[u];k;k=e[k].nxt)
     76         if(!vis[e[k].to]&&e[k].to!=fa)
     77         {
     78             cal2(e[k].to,u);
     79             an1[u]+=an1[e[k].to];
     80         }
     81     ans[u]+=an1[u];
     82 }
     83 void clr(int u,int fa)
     84 {
     85     an1[u]=0;
     86     for(int k=f1[u];k;k=e[k].nxt)
     87         if(!vis[e[k].to]&&e[k].to!=fa)
     88             clr(e[k].to,u);
     89 }
     90 void solve(int u)
     91 {
     92     d[u]=0;vis[u]=1;ta=0;
     93     for(int k=f1[u];k;k=e[k].nxt)
     94         if(!vis[e[k].to])
     95         {
     96             d[e[k].to]=a[e[k].to];
     97             addd(e[k].to,u);
     98         }
     99     for(int k=f1[u];k;k=e[k].nxt)
    100         if(!vis[e[k].to])
    101         {
    102             clr(e[k].to,u);
    103             deld(e[k].to,u);
    104             calc(e[k].to,u);
    105             addd(e[k].to,u);
    106         }
    107     ans[u]+=ta/2;ta=0;
    108     for(int k=f1[u];k;k=e[k].nxt)
    109         if(!vis[e[k].to])
    110             deld(e[k].to,u);
    111     s[0]++;
    112     for(int k=f1[u];k;k=e[k].nxt)
    113         if(!vis[e[k].to])
    114         {
    115             calc(e[k].to,u);
    116             cal2(e[k].to,u);
    117         }
    118     s[0]--;ans[u]+=ta;
    119     for(int k=f1[u];k;k=e[k].nxt)
    120         if(!vis[e[k].to])
    121         {
    122             root=0;getsz(e[k].to,0);sum=sz[e[k].to];
    123             getroot(e[k].to,0);solve(root);
    124         }
    125 }
    126 int n;
    127 int main()
    128 {
    129     fx[0]=0x3f3f3f3f;
    130     int i,x,y;
    131     lft[0]=1;
    132     for(i=1;i<=20;i++)    lft[i]=lft[i-1]<<1;
    133     scanf("%d",&n);
    134     for(i=1;i<n;i++)
    135     {
    136         scanf("%d%d",&x,&y);
    137         e[++ne].to=y;e[ne].nxt=f1[x];f1[x]=ne;
    138         e[++ne].to=x;e[ne].nxt=f1[y];f1[y]=ne;
    139     }
    140     scanf("%s",ss+1);
    141     for(i=1;i<=n;i++)    a[i]=lft[ss[i]-'a'];
    142     root=0;sum=n;getroot(1,0);
    143     solve(root);
    144     for(i=1;i<=n;i++)    printf("%lld ",ans[i]+1);
    145     return 0;
    146 }
  • 相关阅读:
    程序设计网站综合
    .net 获取url的方法(转)
    制作简单的语音识别系统(阅读文章)
    创建 WPF 不规则窗口
    高斯消元法
    How many ways(DFS记忆化搜索)
    I NEED A OFFER!
    免费馅饼
    N^N(Leftmost Digit )
    Common Subsequence
  • 原文地址:https://www.cnblogs.com/hehe54321/p/9285508.html
Copyright © 2011-2022 走看看