[CF842C]Ilya And The Tree

题面

设f[i]表示根为i的子树改了0\1条边的权值为0时的GCD最大值,记忆化搜索+不正确剪枝(最多搜向上推10层)

Code:

/* CF842C Ilya And The Tree */
/* Developed By WYCTSTF */
/* 迎难而上 */
#include
#define int long long
using namespace std;
const int maxn=2e5+10;
int a[maxn];
int f[maxn],g[maxn],fat[maxn];
vector  e[maxn];
int read(){
    int x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch))
        f=(ch=='-')?-1:1,ch=getchar();
    while(isdigit(ch))
        x=x*10+(ch-'0'),ch=getchar();
    return x*f;
}
int gcd(const int &a,const int &b){
    return (b==0)?a:gcd(b,a%b);
}
void getf(int u,int fa){
    if(fa==0)
        g[u]=a[u];
    else
        g[u]=gcd(a[u],g[fa]);
    if(fa!=0){
        f[u]=g[u];
        f[u]=max(g[fa],gcd(f[fa],a[u]));
        int v=fa,gg=a[u],i=10;
        while(v&&i>0){
            f[u]=max(f[u],gcd(gg,f[v]));
            if(fat[v]!=0){
                f[u]=max(f[u],gcd(gg,g[fat[v]]));
            }
            else{
                f[u]=max(f[u],gg);
            }
            gg=gcd(gg,a[v]);
            v=fat[v];
            --i;
        }
    }
    else{
        f[u]=g[u];
    }
    int v,i,us=e[u].size();
    for (int i=0;i

你可能感兴趣的:([CF842C]Ilya And The Tree)