Submission #296003

# Submission time Handle Problem Language Result Execution time Memory
296003 2020-09-10T07:27:00 Z albertolg101 Split the Attractions (IOI19_split) C++17
40 / 100
2000 ms 18088 KB
#include<bits/stdc++.h>
#include "split.h"

using namespace std;

vector<int> a_is_equal_to_one (int b, int c, int n, vector<vector<int>> &G)
{
    vector<bool> flag(n); 
    int cnt = 1;

    function<void(int)> dfs1 = [&]( int nod )
    {
        for(auto i: G[nod])
        {
            if( cnt == b ) 
                return ;

            if(flag[i])
                continue; 

            flag[i] = 1; 
            cnt++; 

            dfs1(i); 
        }
    } ;

    flag[0] = true; 
    dfs1(0); 

    bool a = false ; 
    vector<int> ans(n);

    for( int i = 0 ; i < n ; i++ )
    {
        if( flag[i] ) 
            ans[i] = 2 ; 
        
        else if( a )
            ans[i] = 3 ; 
        
        else
            ans[i] = 1, a = true; 
    }

    return ans ; 
}

vector<int> is_a_line (int a, int b, int c, int n, vector<vector<int>> &G)
{
    int root = 0;
    for( int i = 0 ; i < n ; i++ ) 
        if( G[i].size() == 1 ) 
            root = i ; 

    vector<int> ans(n);

    function<void(int, int)> dfs = [&](int nod, int lvl)
    {
        if( lvl <= a ) 
            ans[nod] = 1; 
        
        else if( lvl <= a + b ) 
            ans[nod] = 2 ;
        
        else 
            ans[nod] = 3 ; 

        for( auto target : G[nod] )
        {
            if( ans[target] == 0 )
                dfs( target , lvl + 1 ) ; 
        }
    } ;

    dfs(root, 1);

    return ans;
}

vector<int> is_a_tree ( int a , int b , int c , int n , vector<vector<int>> &G )
{
    vector<int> size(n); 
    
    vector<int> ar = {a, b, c}, map;
    sort( ar.begin() , ar.end() );

    for( int i = 0 ; i <= 2 ; i++ ) 
    {
        if( ar[i] == a )
            map.push_back(1), a = -1;
        
        else if(ar[i] == b)
            map.push_back(2), b = -1; 
        
        else if(ar[i] == c )
            map.push_back(3), c = -1;
    }

    function<int(int, int)> dfs1 = [&]( int nod , int father )
    {
        size[nod] = 1 ; 

        for( auto target : G[nod] ) 
        {
            if( target == father ) 
                continue ;

            size[nod] += dfs1(target, nod); 
        }

        return size[nod]; 
    } ;

    dfs1(0, -1);

    function<pair<int, pair<int, int>>(int, int, int)> dfs2 = [&]( int nod , int father , int fatherSize )
    {
        pair<int, pair<int, int>> ans = {0, {nod, nod}}; 
        vector<pair<int, int>> child ;

        child.push_back({fatherSize, father});

        for( auto target : G[nod] )
        {
            if( target == father ) 
                continue ; 

            child.push_back({size[target], target});
            ans = max( ans , dfs2(target, nod, fatherSize + size[nod] - size[target] )); 
        }

        int Size = n - 1 ; 
        for( auto i : child )
            if( Size - i.first >= ar[1] )
                ans = max( ans , {i.first, {nod, i.second}} );  

        return ans ;
    };

    int cnt = 0 ; 
    vector<int> ans(n);
    
    function<void(int, int, int)> dfs3 = [&]( int nod , int father , int val )
    {
        ans[nod] = map[val] ; 
        cnt++; 

        if( cnt == ar[val] )
            return ; 

        for( auto target : G[nod] ) 
        {
            if( father == target )
                continue ; 

            dfs3(target, nod, val); 
            if( cnt == ar[val] ) 
                return ; 
        }
    };
    
    pair<int, pair<int, int>> dfsAns = dfs2(0, 0, 0); 

    if( dfsAns.first >= ar[0] ) 
    {
        dfs3( dfsAns.second.second, dfsAns.second.first, 0 ); 

        cnt = 0; 
        dfs3( dfsAns.second.first, dfsAns.second.second, 1 );

        for( auto &i : ans )
            if( i == 0 )
                i = map[2]; 
    }
    
    return ans ; 

}

vector<int> is_a_graph ( int a, int b, int c, int n, vector<vector<int>> &G)
{
    for( int j = 1 ; j <= 10 ; j++ ) 
    {
        for( int i = 0 ; i < n ; i++ ) 
        {
            vector<vector<int>> g(n + 1);

            function<void(int)> dfs1 = [&]( int nod )
            {
                for( auto target : G[nod] ) 
                {
                    if( !g[target].empty() )
                        continue ; 

                    g[nod].push_back(target);
                    g[target].push_back(nod); 
                    dfs1(target); 
                }
            } ;

            dfs1( i ); 

            vector<int> ans = is_a_tree(a, b, c, n, g); 

            if( ans != vector<int>(n, 0) )
                return ans ; 
        }

        mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
        uniform_int_distribution<int>(0,5000) (rng);
        
        for( auto &i : G ) 
            shuffle( i.begin() , i.end() , rng );
    }

    return vector<int> (n, 0);
}

vector<int> find_split(int n, int a, int b, int c, vector<int> p, vector<int> q) {
    
    vector<vector<int>> G(n + 1);
    int m = p.size(), degree = 0; 

    for(int i = 0; i < m; i++)
    {
        G[p[i]].push_back(q[i]);
        G[q[i]].push_back(p[i]);

        degree = max({ degree, (int)G[p[i]].size() , (int)G[q[i]].size() });
    }

    if(a == 1) 
        return a_is_equal_to_one(b, c, n, G); 

    else if( degree == 2 )
        return is_a_line(a, b, c, n, G);

    else if( m == n - 1 ) 
        return is_a_tree(a, b, c, n, G);

    else //if( n <= 2500 and m <= 5000 ) 
        return is_a_graph(a, b, c, n, G);

}           
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB ok, correct split
2 Correct 0 ms 256 KB ok, correct split
3 Correct 0 ms 256 KB ok, correct split
4 Correct 0 ms 256 KB ok, correct split
5 Correct 0 ms 256 KB ok, correct split
6 Correct 1 ms 256 KB ok, correct split
7 Correct 107 ms 14072 KB ok, correct split
8 Correct 96 ms 11896 KB ok, correct split
9 Correct 92 ms 14072 KB ok, correct split
10 Correct 72 ms 7800 KB ok, correct split
11 Correct 99 ms 13972 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB ok, correct split
2 Correct 0 ms 256 KB ok, correct split
3 Correct 1 ms 256 KB ok, correct split
4 Correct 99 ms 8952 KB ok, correct split
5 Correct 75 ms 7928 KB ok, correct split
6 Correct 72 ms 7800 KB ok, correct split
7 Correct 84 ms 10872 KB ok, correct split
8 Correct 124 ms 10104 KB ok, correct split
9 Correct 76 ms 7932 KB ok, correct split
10 Correct 58 ms 8176 KB ok, correct split
11 Correct 59 ms 8176 KB ok, correct split
12 Correct 61 ms 8176 KB ok, correct split
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB ok, correct split
2 Correct 114 ms 8440 KB ok, correct split
3 Correct 35 ms 3456 KB ok, correct split
4 Correct 0 ms 256 KB ok, correct split
5 Correct 145 ms 15480 KB ok, correct split
6 Correct 147 ms 14712 KB ok, correct split
7 Correct 151 ms 14328 KB ok, correct split
8 Correct 148 ms 18088 KB ok, correct split
9 Correct 145 ms 13816 KB ok, correct split
10 Correct 32 ms 3192 KB ok, no valid answer
11 Correct 44 ms 4344 KB ok, no valid answer
12 Correct 94 ms 9204 KB ok, no valid answer
13 Correct 128 ms 8568 KB ok, no valid answer
14 Correct 75 ms 9712 KB ok, no valid answer
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB ok, correct split
2 Correct 0 ms 256 KB ok, no valid answer
3 Correct 0 ms 256 KB ok, correct split
4 Correct 0 ms 256 KB ok, correct split
5 Correct 0 ms 256 KB ok, correct split
6 Correct 1 ms 256 KB ok, correct split
7 Correct 0 ms 256 KB ok, correct split
8 Correct 1 ms 256 KB ok, correct split
9 Correct 4 ms 896 KB ok, correct split
10 Correct 4 ms 768 KB ok, correct split
11 Correct 1 ms 384 KB ok, correct split
12 Correct 3 ms 768 KB ok, correct split
13 Correct 0 ms 256 KB ok, correct split
14 Correct 0 ms 256 KB ok, correct split
15 Correct 1 ms 384 KB ok, correct split
16 Correct 0 ms 256 KB ok, correct split
17 Correct 0 ms 256 KB ok, correct split
18 Correct 1 ms 256 KB ok, correct split
19 Correct 1 ms 384 KB ok, correct split
20 Correct 1 ms 512 KB ok, correct split
21 Correct 3 ms 640 KB ok, correct split
22 Correct 2 ms 640 KB ok, correct split
23 Correct 2 ms 768 KB ok, correct split
24 Correct 2 ms 768 KB ok, correct split
25 Correct 2 ms 640 KB ok, correct split
26 Execution timed out 2085 ms 768 KB Time limit exceeded
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 256 KB ok, correct split
2 Correct 0 ms 256 KB ok, correct split
3 Correct 0 ms 256 KB ok, correct split
4 Correct 0 ms 256 KB ok, correct split
5 Correct 0 ms 256 KB ok, correct split
6 Correct 1 ms 256 KB ok, correct split
7 Correct 107 ms 14072 KB ok, correct split
8 Correct 96 ms 11896 KB ok, correct split
9 Correct 92 ms 14072 KB ok, correct split
10 Correct 72 ms 7800 KB ok, correct split
11 Correct 99 ms 13972 KB ok, correct split
12 Correct 0 ms 256 KB ok, correct split
13 Correct 0 ms 256 KB ok, correct split
14 Correct 1 ms 256 KB ok, correct split
15 Correct 99 ms 8952 KB ok, correct split
16 Correct 75 ms 7928 KB ok, correct split
17 Correct 72 ms 7800 KB ok, correct split
18 Correct 84 ms 10872 KB ok, correct split
19 Correct 124 ms 10104 KB ok, correct split
20 Correct 76 ms 7932 KB ok, correct split
21 Correct 58 ms 8176 KB ok, correct split
22 Correct 59 ms 8176 KB ok, correct split
23 Correct 61 ms 8176 KB ok, correct split
24 Correct 0 ms 256 KB ok, correct split
25 Correct 114 ms 8440 KB ok, correct split
26 Correct 35 ms 3456 KB ok, correct split
27 Correct 0 ms 256 KB ok, correct split
28 Correct 145 ms 15480 KB ok, correct split
29 Correct 147 ms 14712 KB ok, correct split
30 Correct 151 ms 14328 KB ok, correct split
31 Correct 148 ms 18088 KB ok, correct split
32 Correct 145 ms 13816 KB ok, correct split
33 Correct 32 ms 3192 KB ok, no valid answer
34 Correct 44 ms 4344 KB ok, no valid answer
35 Correct 94 ms 9204 KB ok, no valid answer
36 Correct 128 ms 8568 KB ok, no valid answer
37 Correct 75 ms 9712 KB ok, no valid answer
38 Correct 0 ms 256 KB ok, correct split
39 Correct 0 ms 256 KB ok, no valid answer
40 Correct 0 ms 256 KB ok, correct split
41 Correct 0 ms 256 KB ok, correct split
42 Correct 0 ms 256 KB ok, correct split
43 Correct 1 ms 256 KB ok, correct split
44 Correct 0 ms 256 KB ok, correct split
45 Correct 1 ms 256 KB ok, correct split
46 Correct 4 ms 896 KB ok, correct split
47 Correct 4 ms 768 KB ok, correct split
48 Correct 1 ms 384 KB ok, correct split
49 Correct 3 ms 768 KB ok, correct split
50 Correct 0 ms 256 KB ok, correct split
51 Correct 0 ms 256 KB ok, correct split
52 Correct 1 ms 384 KB ok, correct split
53 Correct 0 ms 256 KB ok, correct split
54 Correct 0 ms 256 KB ok, correct split
55 Correct 1 ms 256 KB ok, correct split
56 Correct 1 ms 384 KB ok, correct split
57 Correct 1 ms 512 KB ok, correct split
58 Correct 3 ms 640 KB ok, correct split
59 Correct 2 ms 640 KB ok, correct split
60 Correct 2 ms 768 KB ok, correct split
61 Correct 2 ms 768 KB ok, correct split
62 Correct 2 ms 640 KB ok, correct split
63 Execution timed out 2085 ms 768 KB Time limit exceeded
64 Halted 0 ms 0 KB -