Codeforces Global Round 13 E. Fib-tree (Thinking)

Meaning of the question:
Given a tree, judge whether to remove all edges, the conditions for removing edges are as follows:

1. The size of the two sub-trees after removal must be a Fibonacci tree.

2. Your own number must be Fibonacci number.

answer:

If there is an edge removed, the sizes of the two subtrees are both Fibonacci, because Fibonacci satisfies f [i] = f [i − 1] + f [i − 2] f[i]=f[ i-1]+f[i-2]f[i]=f[i1]+f[i2 ] , so the size of the subtree must bef [i − 1] f[i-1]f[i1 ] Sumf [i − 2] f [i-2]f[i2 ] , because all sides have to be dismantled, the dismantling is the same no matter what, as long as the one that can be dismantled is dismantled.

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<ctime>
#define iss ios::sync_with_stdio(false)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int mod=1e9+7;
const int MAXN=2e5+5;
int dp[MAXN*10];
bool vis[MAXN*10];
int sz[MAXN];
struct node
{
    
    
    int to;
    int next;
}e[MAXN<<1];
int head[MAXN];
int cnt;
int pre[MAXN<<1];
int vt,pos,fa;
void add(int u,int v){
    
    
    e[cnt].to=v;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs(int u,int f,int n,int he)
{
    
    
    
    sz[u]=1;
    for(int i=head[u];i!=-1;i=e[i].next){
    
    
        int v=e[i].to;
        if(v==f) continue;
        if(pre[i]) continue;
        dfs(v,u,n,i);
        sz[u]+=sz[v];
    }
    if(vis[n-sz[u]]&&vis[sz[u]]){
    
    
        fa=f;
        vt=u;
        pos=he;
    }
}
void solve(int u,int n)
{
    
    
    
    if(n==1) return ;
    vt=0;
    dfs(u,0,n,-1);
    if(!vt){
    
    
        printf("NO\n");
        exit(0);
    } 
    
    pre[pos]=pre[pos^1]=1;
    int vtt=fa,w=n-sz[vt];
    solve(vt,sz[vt]);
    solve(vtt,w);
}
int main(int argc, char const *argv[])
{
    
    
    memset(head,-1,sizeof head);
    memset(vis,false,sizeof vis);
    dp[0]=dp[1]=1;
    for(int i=2;i<=30;i++){
    
    
        dp[i]=dp[i-1]+dp[i-2];
        vis[dp[i]]=true;
    }
    vis[1]=true;
    int n;
    cin>>n;
    for(int i=1;i<=n-1;i++){
    
    
        int u,v;
        cin>>u>>v;
        add(u,v);
        add(v,u);
    }
    if(!vis[n]){
    
    
        printf("NO\n");
        return 0;
    }
    solve(1,n);
    printf("YES\n");
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45755679/article/details/114421871