Submission #482892

#TimeUsernameProblemLanguageResultExecution timeMemory
482892MohamedAliSaidaneCat in a tree (BOI17_catinatree)C++14
11 / 100
1081 ms9516 KiB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> ti;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ld,ld> pld;
#define pb push_back
#define popb pop_back()
#define pf push_front
#define popf pop_front
#define ff first
#define ss second
#define MOD (ll)(1000000007)
#define INF (ll) (1e18)
#define all(v) (v).begin(),(v).end()
const int nx[8] = {0, 0, 1, -1,1,1,-1,-1}, ny[8] = {1, -1, 0, 0,1,-1,1,-1}; //East, West, South, North+
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;}
ll lcm(ll a, ll b){return (a / gcd(a, b)) * b;}
////////////******SOLUTION******\\\\\\\\\\\

const int MAX_N = 1504;
int n, d;
int dist[MAX_N][MAX_N];
set<int> seen;
vi adj[MAX_N];
int par[MAX_N];
void f(int x, int par)
{
    dist[x][par] = dist[par][x] = 1;
    seen.insert(x);
    for(int y = 0; y <n; y ++)
    {
        if(y == x)
            dist[x][y] = 0;
        if(y == par) continue;
        if(seen.count(y) != 0)
            dist[x][y] = dist[y][x] = dist[par][y] + 1;
    }
    //cout << x << ' ' << par << '\n' << flush;
    if(adj[x].size() == 0)
        return;
    for(auto e: adj[x])
        f(e,x);
    //cout << x << " returning\n" << flush;
    return ;

}
void solve()
{
    cin >> n >> d;
    memset(dist,-1,sizeof(dist));
    for(int i = 1; i <n; i++)
    {
        cin >> par[i];
        adj[par[i]].pb(i);
    }
    f(0,n);
    int ans = 1;
    for(int mask = 1; mask < (1 << n); mask ++)
    {
        //cout << mask << '\n' << flush;
        vi nodes;
        for(int i = 0; i < n; i ++)
            if(mask & (1<<i))
                nodes.pb(i);
        bool flag = true;
        for(int i = 0; i < nodes.size()-1; i ++)
        {
            for(int j = i + 1; j < nodes.size(); j ++)
            {
                //cout << i << ' ' << j << ' ' << mask << '\n' << flush;
                if(dist[nodes[i]][nodes[j]] < d)
                {
                    flag = false;
                    break;
                }
            }
        }
        if(flag)
            ans = max(ans,(int)(nodes.size()));
        //cout << '\t' << ans << '\n' << flush;
    }
    cout << ans;
    return ;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int tt =1;
    while(tt--)
    {
        solve();
    }

}
/*

*/

Compilation message (stderr)

catinatree.cpp:24:1: warning: multi-line comment [-Wcomment]
   24 | ////////////******SOLUTION******\\\\\\\\\\\
      | ^
catinatree.cpp: In function 'void solve()':
catinatree.cpp:72:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |         for(int i = 0; i < nodes.size()-1; i ++)
      |                        ~~^~~~~~~~~~~~~~~~
catinatree.cpp:74:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |             for(int j = i + 1; j < nodes.size(); j ++)
      |                                ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...