zoukankan      html  css  js  c++  java
  • csp-s模拟测试77+78(lrd day1&2)

    RP-=inf。。。。。。。

    一场考试把rp败光。。。由于本次考试本人在考试中乱说自己AK导致rp--,本人当选为机房倒数第二没素质

    不过AK一次还挺开心的。。。

    达哥出的题还是比较简单的。

    T1:考察位运算的技巧,对于所有的操作按位考虑即可。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 int a,b,c;
     7 long long bin[50];
     8 long long an[50];
     9 long long ans;
    10 inline void init()
    11 {
    12     for(int i=0;i<=40;++i)bin[i]=1ll<<i;
    13     an[0]=1;
    14     for(int i=1;i<=32;++i)an[i]=an[i-1]*3;
    15 }
    16 inline int getnum(int x)
    17 {
    18     int ret=0;
    19     while(x){ret+=x&1;x>>=1;}
    20     return ret;
    21 }
    22 inline void work1()
    23 {
    24     int t=getnum(b);
    25     printf("%lld
    ",an[t]);
    26 }
    27 inline void work5()
    28 {
    29     if((a^b)!=c){puts("0");return;}
    30     int t=getnum(c);
    31     printf("%lld
    ",bin[t]);
    32 }
    33 inline void work2()
    34 {
    35     if((b&c)!=c){puts("0");return;}
    36     a=b^c;
    37     work5();
    38 }
    39 inline void work3()
    40 {
    41     if(a&c){puts("0");return;}
    42     b=a|c;work5();
    43 }
    44 inline void work4()
    45 {
    46     if((b&a)!=a){puts("0");return;}
    47     c=a^b;work5();
    48 }
    49 int main()
    50 {
    51     init();
    52     int T;scanf("%d",&T);
    53     while(T--)
    54     {
    55         scanf("%d%d%d",&a,&b,&c);
    56         if(b==-1&&c==-1)
    57         {
    58             puts("inf");continue;
    59         }
    60         if(a==-1&&c==-1)
    61         {
    62             work1();continue;
    63         }
    64         if(a==-1&&b==-1)
    65         {
    66             puts("inf");continue;
    67         }
    68         if(a==-1)
    69         {
    70             work2();continue;
    71         }
    72         if(b==-1)
    73         {
    74             work3();continue;
    75         }
    76         if(c==-1)
    77         {
    78             work4();continue;
    79         }
    80         work5();
    81     }
    82 }
    冗长讨论

    T2:把对于全部的数的加减用变量维护即可,对于取交和取并直接打时间戳维护即可

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 char xch,xB[1<<15],*xS=xB,*xTT=xB;
     6 #define getc() (xS==xTT&&(xTT=(xS=xB)+fread(xB,1,1<<15,stdin),xS==xTT)?0:*xS++)
     7 inline int read()
     8 {
     9     int x=0,f=1;char ch=getc();
    10     while(ch<'0'|ch>'9'){if(ch=='-')f=-1;ch=getc();}
    11     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getc();}
    12     return x*f;
    13 }
    14 using namespace std;
    15 const long long kx=1000000;
    16 int pd[2000600],n;
    17 int sum,num=1;
    18 long long plu,ans;
    19 inline void work1()
    20 {
    21     int m=read(),a,b,c;
    22     for(int i=1;i<=m;++i)
    23     {
    24         a=read()+kx;
    25         if(pd[a-plu]==num)continue;
    26         pd[a-plu]=num;
    27         ++sum;ans+=a-plu;
    28     }
    29 }
    30 inline void work2()
    31 {
    32     int m=read(),a,b,c;
    33     sum=0;ans=0;
    34     for(int i=1;i<=m;++i)
    35     {
    36         a=read()+kx;
    37         if(pd[a-plu]!=num)continue;
    38         pd[a-plu]=num+1;
    39         ++sum;ans+=a-plu;
    40     }
    41     ++num;
    42 }
    43 int main()
    44 {
    45 //    freopen("ex_jihe4.in","r",stdin);
    46 //    freopen("my.out","w",stdout);//diff -b -B ex_jihe4.ans my.out
    47     n=read();int opt;
    48     while(n--)
    49     {
    50         opt=read();
    51         switch(opt){
    52             case 1:{
    53                 work1();
    54                 break;
    55             }
    56             case 2:{
    57                 work2();
    58                 break;
    59             }
    60             case 3:{
    61                 ++plu;
    62                 break;
    63             }
    64             case 4:{
    65                 --plu;
    66                 break;
    67             }
    68         }
    69         printf("%lld
    ",ans+plu*sum-kx*sum);
    70     }
    71 }
    感谢达哥的fread

    T3:稍神奇,图论+容斥原理。

    先对所有的白块染色,相连的染成同一种颜色。顺手用桶统计答案

    这时发现统计多了,对于一些情况会统计多。考虑容斥,奇加偶减,对于每个非空白点,先把周围的块统计一下,排序去重,手动枚举集合,用hash搞掉,用hash_map储存

    分类讨论奇加偶减即可。。。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 inline int read()
      6 {
      7     int x=0,f=1;char ch=getchar();
      8     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
      9     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     10     return x*f;
     11 }
     12 #define N 1050
     13 using namespace std;
     14 int a[N][N];
     15 int bk[1000500];
     16 long long ans;
     17 int n,num=1,m,k;
     18 int pd[N][N];
     19 const int dx[4]={1,-1,0,0};
     20 const int dy[4]={0,0,1,-1};
     21 struct QaQ{
     22     pair<int,int>a[1000500];
     23     int fr,ba;
     24     inline int size(){return ba-fr;}
     25     inline void push(int x,int y){a[ba]=make_pair(x,y);++ba;}
     26     inline void clear(){ba=fr=0;}
     27     inline void pop(){++fr;}
     28     inline int fir(){return a[fr].first;}
     29     inline int sec(){return a[fr].second;}
     30 }q;
     31 inline void bfs1(int x,int y)
     32 {
     33     q.clear();++num;pd[x][y]=num;
     34     int tx,ty,c;q.push(x,y);
     35     while(q.size())
     36     {
     37         x=q.fir();y=q.sec();q.pop();
     38         for(int i=0;i<4;++i)
     39         {
     40             tx=x+dx[i];ty=y+dy[i];
     41             if(tx<=0||ty<=0||tx>n||ty>m||pd[tx][ty]==num)continue;
     42             pd[tx][ty]=num;c=a[tx][ty];
     43             if(c){ans+=bk[c];++bk[c];}
     44             else q.push(tx,ty);
     45         }
     46     }
     47 }
     48 inline void bfs2(int x,int y)
     49 {
     50     q.clear();++num;pd[x][y]=num;
     51     int tx,ty,c;q.push(x,y);
     52     while(q.size())
     53     {
     54         x=q.fir();y=q.sec();q.pop();
     55         for(int i=0;i<4;++i)
     56         {
     57             tx=x+dx[i];ty=y+dy[i];
     58             if(tx<=0||ty<=0||tx>n||ty>m||pd[tx][ty]==num)continue;
     59             pd[tx][ty]=num;c=a[tx][ty];
     60             if(c)bk[c]=0;
     61             else q.push(tx,ty);
     62         }
     63     }
     64 }
     65 int pd2[1500000],t1[10];
     66 inline int get1(int x,int y)
     67 {
     68     int tx,ty;t1[0]=0;
     69     for(int i=0;i<4;++i)
     70     {
     71         tx=x+dx[i];ty=y+dy[i];
     72         if(tx<=0||ty<=0||tx>n||ty>m||a[tx][ty])continue;
     73         t1[++t1[0]]=pd[tx][ty];pd2[pd[tx][ty]]=1;
     74     }
     75 }
     76 inline int get2(int x,int y)
     77 {
     78     int tx,ty;
     79     for(int i=0;i<4;++i)
     80     {
     81         tx=x+dx[i];ty=y+dy[i];
     82         if(tx<=0||ty<=0||tx>n||ty>m||a[tx][ty])continue;
     83         if(pd2[pd[tx][ty]])return 0;
     84     }
     85     return 1;
     86 }
     87 void down1()
     88 {
     89     for(int i=1;i<=t1[0];++i)pd2[t1[i]]=0;
     90 }
     91 inline void work2()
     92 {
     93     for(int i=1;i<=n;++i)
     94         for(int j=1;j<=m;++j)
     95         {
     96             get1(i,j);
     97             if(a[i][j]&&a[i][j]==a[i][j+1])
     98                 ans+=get2(i,j+1);
     99             if(a[i][j]&&a[i][j]==a[i+1][j])
    100                 ans+=get2(i+1,j);
    101 //            printf("i:%d j:%d ans:%lld
    ",i,j,ans);
    102             down1();
    103         }
    104 }
    105 const int mod=233337;
    106 const int tt=1000003;
    107 #define ull unsigned long long
    108 struct hash_map{
    109     int he[mod],tot;
    110     int ne[10000007],to[10000007];
    111     ull val[10000007];
    112     inline void add(ull x){
    113         int k=x%mod;
    114         for(int i=he[k];i;i=ne[i])
    115         {
    116             if(val[i]==x)
    117             {
    118                 ++to[i];
    119                 return;
    120             }
    121         }
    122         to[++tot]=1;ne[tot]=he[k];he[k]=tot;val[tot]=x;
    123     }
    124     inline int getval(ull x){
    125         int k=x%mod;
    126         for(int i=he[k];i;i=ne[i])
    127             if(val[i]==x)
    128                 return to[i];
    129         return 0;
    130     }
    131 }H;
    132 inline void duce(int x,int y,int z)
    133 {
    134     ull t=x;t=t*tt+y;t=t*tt+z;
    135     ans-=H.getval(t);H.add(t);
    136 }
    137 inline void pluss(int w,int x,int y,int z)
    138 {
    139     ull t=w;t=t*tt+x;t=t*tt+y;t=t*tt+z;
    140     ans+=H.getval(t);H.add(t);
    141 }
    142 inline void duce2(int v,int w,int x,int y,int z)
    143 {
    144     ull t=v;t=t*tt+w;t=t*tt+x;t=t*tt+y;t=t*tt+z;
    145     ans-=H.getval(t);H.add(t);
    146 }
    147 inline void work3()
    148 {
    149     int c;
    150     for(int i=1;i<=n;++i)
    151         for(int j=1;j<=m;++j)
    152         {
    153             if(!a[i][j]||!pd[i][j])continue;
    154             get1(i,j);c=a[i][j];
    155             if(t1[0]<2)continue;
    156             sort(t1+1,t1+t1[0]+1);
    157             t1[0]=unique(t1+1,t1+t1[0]+1)-t1-1;
    158             if(t1[0]<2)continue;
    159             if(t1[0]==2)
    160             {
    161                 duce(t1[1],t1[2],c);
    162                 continue;
    163             }
    164             if(t1[0]==3)
    165             {
    166                 duce(t1[1],t1[2],c);
    167                 duce(t1[1],t1[3],c);
    168                 duce(t1[2],t1[3],c);
    169                 pluss(t1[1],t1[2],t1[3],c);
    170             }
    171             if(t1[0]==4)
    172             {
    173                 duce(t1[1],t1[2],c);
    174                 duce(t1[1],t1[3],c);
    175                 duce(t1[1],t1[4],c);
    176                 duce(t1[2],t1[3],c);
    177                 duce(t1[2],t1[4],c);
    178                 duce(t1[3],t1[4],c);
    179                 pluss(t1[1],t1[2],t1[3],c);
    180                 pluss(t1[1],t1[2],t1[4],c);
    181                 pluss(t1[1],t1[3],t1[4],c);
    182                 pluss(t1[2],t1[3],t1[4],c);
    183                 duce2(t1[1],t1[2],t1[3],t1[4],c);
    184             }
    185         }
    186 }
    187 int main()
    188 {
    189 //    freopen("ex_link5.in","r",stdin);
    190 //    freopen("my.out","w",stdout);
    191     scanf("%d%d%d",&n,&m,&k);
    192     for(int i=1;i<=n;++i)
    193         for(int j=1;j<=m;++j)
    194             a[i][j]=read();
    195     for(int i=1;i<=n;++i)
    196         for(int j=1;j<=m;++j)
    197             if(!a[i][j]&&!pd[i][j])
    198             {
    199                 bfs1(i,j);bfs2(i,j);
    200             }
    201     work2();
    202     work3();
    203     cout<<ans<<endl;
    204     /*
    205     for(int i=1;i<=n;++i)
    206     {
    207         for(int j=1;j<=m;++j)
    208         {
    209             printf("%d ",pd[i][j]);
    210         }
    211         puts("");
    212     }
    213     */
    214 }
    rp++

    最后:

     1  此刻我十分后悔,我做了一件错事,我简直不能原谅我自我!昨日我无视学校的规章制度,藐视校领导的决定,私自乱说AK。
     2 
     3   这天,我怀着愧疚、懊悔和忐忑的情绪给您写下这份检讨书,以向您表示我对这种恶劣行为的深痛恶绝及保证不再犯的决心。
     4 
     5   早在我刚踏进这个学校的时候,学校就已经三令五申,一再强调,学生,不得乱说AK。这两天,老师反复教导言犹在耳,严肃认真的表情犹在眼前,我深为震撼,也已经深刻认识到此事的重要性,在老师的耐心教导下,透过学习学生手则中学生管理规定,使我认识到了问题的严重性,并对自我违反校纪校规的行为进行了认真的反思和深刻的自剖。
     6 
     7   在此,我谨向各位领导、老师做出深刻检讨,并将我这两天透过反思认为深藏在本人思想中的致命错误有以下几点结果汇报如下:
     8 
     9   第一,我的行为不贴合一个中学生的要求。作为当代中学生,我们就应识大体、顾大局,在校纪校规面前人人平等,我不就应为了一己之便违反校纪校规。
    10 
    11   第二,思想觉悟不高,对重要事项重视严重不足。就算是有认识,也没能在行动上真正实行起来。
    12 
    13   第三,在学习期间,我们应主动配合学院搞好安全工作,学院老师三令五申、班干也一再强调,要求我们不能乱说AK,但我把这些都成了耳旁风,这些都是不就应的。
    14 
    15   第四,我的行为还在同学间造成了极坏的影响。同学之间本就应互相学习,互相促进,而我的表现给同学们带了一个坏头,不利于校风和院风的建设。同时,也对学校形象造成了必须损害。想着带一次也无所谓,当时的侥幸心理酿成了此刻的后果。虽然我这种行为方便了自我,但是,我是在自私自利的帽子下,方便自我的。只有认真反思,寻找极大错误后面的深刻根源,认清问题的本质,才能给群众和自我一个交待,从而得以进步。做为一名学生,我没有做好自我的本职,给学校老师和学生会干部的工作带来了很大的麻烦。
    16 
    17   据上,在深刻的自我反思之后,我决定有如下个人整改措施:第一,从错误根源进行深挖细找的整理,并认清其可能造成严重。第二,提高认识,狠抓落实,大力开展批评与自我批评。
    18 
    19   第三,制定学习计划,认真克服生活懒散、粗心的坏习惯,按照老师要求上交资料深刻的检讨书一份,对自我思想上心大意的缺点,努力将期考考好,以好成绩弥补我的过错。
    20 
    21   第四,和同学、班干以及学生会干部加强沟通。保证今后不再出现违反校纪校规的状况。我十分感谢老师和学生会干部对我所犯错误的及时指正,我保证今后不会再有类似行为发生在我身上,并决心为我校的安全工作和迎评工作作出自我的一份微薄之力。
    22 
    23   第五,从思想上,我重新检讨自我,坚持从认识上,从观念上转变,要求上进,关心群众,关心他人,多和优秀同学接触,交流。纪律上,此刻我必须要比以前要有了很大改变,此刻的我对自我的言行,始终持续严格的约束,不但能遵守校规校纪,更加懂得了身为一名学生哪些事能够做的,哪些是不能够做的。学习上,我能够不避困难,自始至终为掌握更多知识,使自我的素质全面得到提升。
    24 
    25   第六,保证不再出现上述错误。期望老师能够原谅我!最后请关心爱护我的老师同学继续监督、帮忙我改正缺点,取得更大的进步。
    buyaotui

    果然,第二场炸了。(rp守恒.flag)

    然而某脸又双ruozhuo rank1了。

    开场康T1,水,康T2,推出式子发现第三个大样例过不了第一问,然后还剩下一个小时结束,就扔了看T3,然后看出正解,还剩40分钟,疯狂码码码,最后由于rp--没调出来。

    然后就100+40+5结束day2。。。

    T1:太水了不想说

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define ull unsigned long long
     6 const int p=131;
     7 using namespace std;
     8 ull po[1100007];
     9 long long ans,al;
    10 int n,m;
    11 ull h1,h2;
    12 char s[1100007];
    13 int pd[1100007];
    14 inline void init()
    15 {
    16     po[0]=1;
    17     for(register int i=1;i<=1000000;++i)po[i]=po[i-1]*p;
    18 }
    19 int main()
    20 {
    21 //    freopen("ex_ccx2.in","r",stdin);
    22     int T;scanf("%d",&T);init();
    23     while(T--)
    24     {
    25         scanf("%d%d%s",&n,&m,s+1);
    26         h1=h2=0;ans=al=0;
    27         for(int i=1;i<m;++i)
    28         {
    29             pd[m-i+1]=0;
    30             h1+=po[i-1]*s[i];
    31             h2=h2*p+s[m-i+1];
    32             if(h1==h2)
    33             {
    34                 ans=i;pd[m-i+1]=1;
    35                 if(i>=m-i&&pd[i+1])
    36                     al=i;
    37             }
    38         }
    39 //        cout<<"ans:"<<ans<<endl;
    40 //        if(ans<(m+1)/2){ans=max(ans,1ll*(n-1)*m);}
    41 //        else 
    42         ans=max(ans,al+1ll*(n-1)*m);
    43         if(!ans)puts("-1");
    44         else printf("%lld
    ",ans);
    45     }
    46 }
    using hash or kmp

    T2:胡图图,稍神,发现考场上推的第一问式子锅了。(其实第二问式子也锅了但不知道为啥第二问过了。。。)

    然而蒟蒻只会咕咕咕和粘链接(%%% DeepinC

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<queue>
      6 #define N 100050
      7 using namespace std;
      8 int n,m,s;
      9 queue<int>q;
     10 int tot,he[N],ne[N<<3],to[N<<3];
     11 int ind[N],outd[N],pd[N],tag;
     12 double dp[N],f[N],f2[N],mx[N],an1,an2;//vectory,lrd,wms
     13 inline void addedge(int x,int y)
     14 {
     15     ++outd[x];
     16     to[++tot]=y;
     17     ne[tot]=he[x];
     18     he[x]=tot;
     19 }
     20 inline void work1()//MAX
     21 {
     22     double ma=0.0,p,m1=0.0;
     23 //    for(int i=1;i<=n;++i)ma=max(ma,dp[i]);
     24     for(int i=n;i;--i)mx[i]=max(mx[i+1],dp[i]);
     25     for(int i=1;i<=n;++i)
     26     {
     27         ma=max(m1,mx[i+1]);
     28         
     29         if(f2[i]>f[i])
     30         p=(f2[i]-f[i])*(ma)/(double)(outd[i]+1) + f[i]*(1.0)/(double)(outd[i]+1) +
     31          (dp[s]-f[i]*dp[i]-f2[i]*(1-dp[i]))+f[i]*dp[i]*outd[i]/(outd[i]+1)+f2[i]*(1-dp[i])*outd[i]/(outd[i]+1);
     32         else
     33         p=f[i]*(1.0)/(double)(outd[i]+1) +
     34          (dp[s]-f[i]*dp[i]-f2[i]*(1-dp[i]))+f[i]*dp[i]*outd[i]/(outd[i]+1)+f2[i]*(1.0-dp[i])*outd[i]/(outd[i]+1);
     35         an1=max(an1,p);
     36         m1=max(m1,dp[i]);
     37     }
     38 }
     39 inline void work2()//AVR
     40 {
     41     double al=0.0,p;an2=0.0;
     42     for(int i=1;i<=n;++i)al+=dp[i];
     43     for(int i=1;i<=n;++i)
     44     {
     45         p=(al-dp[i])/(n-1);
     46 an2+=f2[i]*(p)/(double)(outd[i]+1)+f[i]*(1.0-p)/(double)(outd[i]+1)+(1.0-(f[i]+f2[i])/(double)(outd[i]+1))*dp[s];
     47     }
     48     an2/=(double)n;
     49 }
     50 void sear(int g)
     51 {
     52     dp[g]=0.0;pd[g]=1;
     53     if(!outd[g])return;
     54     for(int i=he[g];i;i=ne[i])
     55     {
     56         if(!pd[to[i]])sear(to[i]);
     57         if(tag)++ind[to[i]];
     58         dp[g]+=1.0-dp[to[i]];
     59     }
     60     dp[g]=dp[g]/(double)outd[g];
     61 }
     62 inline void tope()
     63 {
     64     q.push(s);f[s]=1;f2[s]=0;
     65     int g,y;double g1,g2;
     66     while(!q.empty())
     67     {
     68         g=q.front();q.pop();
     69         if(!outd[g])continue;
     70         g1=f[g]/(double)outd[g];
     71         g2=f2[g]/(double)outd[g];
     72         for(int i=he[g];i;i=ne[i])
     73         {
     74             y=to[i];--ind[y];
     75             f2[y]+=g1;f[y]+=g2;
     76             if(!ind[y])q.push(y);
     77         }
     78     }
     79 }
     80 int main()
     81 {
     82 //    freopen("ex_htt3.in","r",stdin);
     83     scanf("%d%d%d",&n,&m,&s);
     84     for(int i=1,x,y;i<=m;++i){
     85         scanf("%d%d",&x,&y);
     86         addedge(x,y);
     87     }
     88     scanf("%lf%lf",&an1,&an2);
     89     tag=1;sear(s);tag=0;
     90     for(int i=1;i<=n;++i)
     91         if(!pd[i])sear(i);
     92     
     93     tope();
     94     /*
     95     for(int i=1;i<=n;++i)
     96     {
     97         if(dp[i])
     98         printf("i:%d ind:%d f:%.3lf f2:%.3lf dp:%.3lf
    ",i,ind[i],f[i],f2[i],dp[i]);
     99     }
    100     */
    101     
    102     if(an1<0.0){work1();}
    103     if(an2<0.0){work2();}
    104     printf("%.3lf %.3lf
    ",an1,an2);
    105 }
    View Code

    T3:数据结构题,考察树上倍增的应用,预处理一波直接倍增维护即可

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<queue>
      6 #define N 200050
      7 using namespace std;
      8 int n,m;
      9 int he[N],ne[N<<2],to[N<<2],w[N<<2],tot;
     10 int dep[N],len;
     11 int dp[N],ma1[N][2],ma2[N][2],ma3[N],fat[N];//dp值,最大,次大
     12 inline void addedge(int x,int y){to[++tot]=y;ne[tot]=he[x];he[x]=tot;}
     13 int f[N][21],ma[N][21];
     14 inline void dfs1(int g,int fa)
     15 {
     16     int y;dep[g]=dep[fa]+1;f[g][0]=fa;dp[g]=0;
     17     for(int i=he[g];i;i=ne[i])
     18     {
     19         if(to[i]==fa)continue;
     20         dfs1(to[i],g);y=to[i];
     21         dp[g]=max(dp[g],dp[y]);
     22         if(dp[y]>ma1[g][0])
     23         {
     24             ma3[g]=ma2[g][0];
     25             ma2[g][0]=ma1[g][0];ma2[g][1]=ma1[g][1];
     26             ma1[g][0]=dp[y];ma1[g][1]=y;
     27         }
     28         else if(dp[y]>ma2[g][0])
     29         {
     30             ma3[g]=ma2[g][0];
     31             ma2[g][0]=dp[y];ma2[g][1]=y;
     32         }
     33         else if(dp[y]>ma3[g])
     34         {
     35             ma3[g]=dp[y];
     36         }
     37     }
     38     len=max(len,ma1[g][0]+ma2[g][0]+1);
     39     ++dp[g];
     40 }
     41 inline int getval(int g,int fa)
     42 {
     43     if(ma1[fa][1]==g)return ma2[fa][0];
     44     return ma1[fa][0];
     45 }
     46 inline void dfs2(int g,int fa)
     47 {
     48     int y;fat[g]=dp[fa];
     49     ma[g][0]=getval(g,fa);
     50     dp[g]=0;
     51     for(int i=he[g];i;i=ne[i])
     52     {
     53         if(to[i]==fa)continue;y=to[i];
     54         if(y==ma1[g][1])dp[g]=max(fat[g]+1,ma2[g][0]+1);
     55         else dp[g]=max(fat[g]+1,ma1[g][0]+1);
     56         dfs2(to[i],g);
     57     }
     58 }
     59 inline void init()
     60 {
     61     for(int j=1;j<=20;++j)
     62     {
     63         for(int i=1;i<=n;++i)
     64         {
     65             f[i][j]=f[f[i][j-1]][j-1];
     66             ma[i][j]=max(ma[i][j-1],ma[f[i][j-1]][j-1]);
     67         }
     68     }
     69 }
     70 int ans;
     71 inline int getans(int x,int y)
     72 {
     73     if(dep[x]>dep[y])swap(x,y);
     74     for(int i=20;~i;--i)
     75     {
     76         if(dep[f[y][i]]<dep[x])continue;
     77         ans=max(ans,ma[y][i]);
     78         y=f[y][i];
     79     }
     80     if(x==y)return x;
     81     for(int i=20;~i;--i)
     82     {
     83         if(f[y][i]==f[x][i])continue;
     84         ans=max(ans,ma[y][i]);ans=max(ans,ma[x][i]);
     85         y=f[y][i];x=f[x][i];
     86     }
     87     int lca=f[x][0];
     88     if(x==ma1[lca][1]||y==ma1[lca][1])
     89     {
     90         if(x==ma2[lca][1]||y==ma2[lca][1])
     91             ans=max(ans,ma3[lca]);
     92         else
     93             ans=max(ans,ma2[lca][0]);
     94     }
     95     else
     96     {
     97         ans=max(ans,ma1[lca][0]);
     98     }
     99     return lca;
    100 }
    101 inline void work2()
    102 {
    103     dfs1(1,0);dfs2(1,0);
    104     init();
    105     int lca;len=(len+1)>>1;
    106     for(int i=1,x,y;i<=m;++i)
    107     {
    108         ans=0;
    109         scanf("%d%d",&x,&y);
    110         if(x==y){printf("%d
    ",len);continue;}
    111         lca=getans(x,y);
    112         if(lca!=x)ans=max(ans,ma1[x][0]);
    113         if(lca!=y)ans=max(ans,ma1[y][0]);
    114         ans=max(ans,fat[lca]);
    115         printf("%d
    ",ans);
    116     }
    117 }
    118 int main()
    119 {
    120     scanf("%d",&n);
    121     int ok1=1;
    122     for(int i=1,x,y;i<n;++i){
    123         scanf("%d%d",&x,&y);
    124         if(x>y)swap(x,y);if(y!=x+1)ok1=0;
    125         addedge(x,y);addedge(y,x);
    126     }
    127     scanf("%d",&m);
    128     work2();
    129 }
    View Code

    记得攒rp

  • 相关阅读:
    小甲鱼Python第021讲函数:lambda表达式 | 课后测试题及参考答案
    小甲鱼Python第020讲函数:内嵌函数和闭包 | 课后测试题及参考答案
    How to reference two table when lack reference column.
    how find a record import other database.
    when create a table,then show error ora-00952 tablespace tsb_1 not exist
    TELNET_COMMAND
    ORACLE_SPOOL
    OFFICE_EXCEL_Combine text from two or more cells into one cell.
    ORACLE_LPAD_FUNCTION
    ORACLE_PROCEDURE_DROPTABLE
  • 原文地址:https://www.cnblogs.com/loadingkkk/p/11695300.html
Copyright © 2011-2022 走看看