Submission #312724

# Submission time Handle Problem Language Result Execution time Memory
312724 2020-10-14T08:48:32 Z mohamedsobhi777 ICC (CEOI16_icc) C++14
18 / 100
387 ms 1144 KB
#include<bits/stdc++.h>
#include "icc.h"

#define I inline void 
#define S struct 
#define vi vector<int> 
#define vii vector<pair<int,int>>
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std ; 
using ll = long long ; 
using ld = long double ; 

const int N = 1e5 + 7 , mod = 1e9 + 7 ; 
const int inf = N ; 
// How interesting!

int n ; 
int fat[N] ;
vector<pair<int,int> > ed ; 
int U , V ; 

inline int find(int x){return fat[x] = (x == fat[x] ? x : find(fat[x])) ; }

void link(int u , int v){
        u = find(u) ; 
        v = find(v) ;
        if(u!=v) 
                fat[u] = v; 
}
/*
bool ok(int u , int v){
        if( u == U && v == V)
                return 1 ; 
        if(u == V && v == U)
                return 1; 
        return 0 ; 
}

void setRoad(int u , int v){

        if(ok(u , v)){
                cout<<"ok " << u <<" " << v <<"\n" ;
                link(u , v) ; 
        }
        else{
                cout<< u <<" "<< v <<">>" ; 
                cout<<"bad" ;
                exit(0) ; 
        }
}

int query(int x , int y , vector<int> v1 , vector<int> v2){
        set<int> st ; 
        for(auto u : v1)
                st.insert(find(u)) ; 
        for(auto u : v2){
                if(st.find(find(u)) != st.end())
                        return 1 ; 
        }
        int m1 = 0 , m2 = 0 ;
        for(auto u : v1)
                if(u == U || u == V)
                        m1 = 1; 
        for(auto u : v2)
                if(u == U || u == V)
                        m2 = 1 ;
        if( m1 == 1 && m2 == 1 ){
                return 1 ; 
        }
        return 0 ; 
}
*/

int query1(int x , int y , vector<int> v1 , vector<int> v2){
        //return query(x , y , v1 , v2) ; 
        int *v11;
        int *v22 ; 
        v11 = new int[x] ; 
        v22 = new int [y] ; 
        for(int i = 0 ;i < x ; ++ i)
                v11[i] = v1[i] ; 
        for(int i = 0 ;i < y ; ++ i)
                v22[i] = v2[i] ; 
       return query(x , y , v11 , v22) ; 
}

void divide(vector<vector<int> > &vec){
        vector<vector<int> > ret ; 
        int sz = (int) vec.size() ; 
        for(int i = 0 ;i < sz ; ++ i){
                int sz2 = (int) vec[i].size() ; 
                //if(sz2 < 2)continue ; 
                vector<int> lhs, rhs ;
                for(int j = 0 ;j < sz2 ;++ j){
                        if(j < sz2 / 2 )
                                lhs.push_back(vec[i][j]) ; 
                        else 
                                rhs.push_back(vec[i][j]) ; 
                }
                if(lhs.size())
                ret.push_back(lhs) ; 
                if(rhs.size())
                ret.push_back(rhs) ; 
        }
        vec = ret ; 
}

vector<int> chd(int x){
        vector<int> ret ;
        for(int i = 1 ;i <= n; ++ i){
                if(find(i) == x)
                        ret.push_back(i) ;
        }
        return ret ;
}

int conquer(vector<vector<int> > &vec, int lmt = 1000000){
        int ret = 0 ; 
        vector<int> lhs, rhs ; 
        for(int i = 0 ; i < min(lmt , (int) vec.size() ) ; ++ i){
                for(auto u : vec[i]){
                        if(i&1)
                                for(auto v : chd(u))
                                        rhs.push_back(v) ; 
                        else for(auto v : chd(u))
                                lhs.push_back(v) ; 
                }
        }  
        return query1(lhs.size() , rhs.size() , lhs , rhs ) ; 
}

void print(vector<vector<int> > &vec){
        for(auto u : vec){
                for(auto v : u )
                        cout<< v <<" " ; 
                cout<<"\n" ; 
        } 
        cout<<"----------\n" ; 
}

vector<int> qur(vector<int> ve){
        vector<int> ret ; 
        for(auto u : ve)
                for(auto v : chd(u))
                        ret.push_back(v) ; 
        return ret; 
}

void divide_or_conquer(vector<int> hs){
        int sz = (int) hs.size() ; 
        vector<vector<int> > dac { hs } ; 

        divide(dac) ; 
        
        int k = 0 ; 
        while(!conquer(dac)){
                divide(dac) ; 
        }       
        sz = dac.size() ; 
        
        int lo = 1, hi = (int) dac.size() ; 
        int lst = (int) dac.size() ; 
        while(lo<=hi){
                int mid = (lo+hi) >> 1;
                if(conquer(dac, mid)){
                        lst = mid ; 
                        hi = mid - 1; 
                }else lo = mid + 1; 
        }
        vector<int> qlhs = dac[lst-2] ,qrhs = dac[lst-1] ; 

        int llhs = 0 , rrhs = 0 ; 
        vector<int> q = qur(qlhs) ; 
        vector<int> q2 = qur(qrhs) ; 
        while(qlhs.size() && query1(q.size() , q2.size() , q , q2 )){
                llhs = qlhs.back() ; 
                qlhs.pop_back() ; 
                q = qur(qlhs) ; 
        }
        if(llhs)qlhs.push_back(llhs) ; 
        q = qur(qrhs) ; 
        q2 = qur(qlhs) ;
        while(qrhs.size() && query1(q2.size() , q.size() , q2 , q )){
                rrhs = qrhs.back() ; 
                qrhs.pop_back() ; 
                q = qur(qrhs) ; 
        }
        if(rrhs)qrhs.push_back(rrhs) ; 

        
        int a = qlhs.back() , b = qrhs.back() ; 

        for(int i = 1;i <= n; ++ i){
                for(int j = 1;j <= n ;++ j){
                        if(find(i) == a && find(j) == b && query1(1 ,1 , {i} , {j})){
                                setRoad(i , j) ; 
                                link(i , j) ;
                                return ; 
                        }
                }
        }

}

void run(int x){
        n = x;
        for(int i = 1;i <= n; ++ i)
                fat[i] = i ;
        /*bad_solve(n) ; 
        return ;*/ 
        for(int r = 0 ; r < n - 1; ++ r){
                //U = ed[r].first ; 
                //V = ed[r].second ; 

                vector<int> v ; 
                for(int i = 1;i <= n; ++ i){
                        if(i == fat[i])
                                v.push_back(i) ; 
                }
                divide_or_conquer(v) ; 
        }
}

Compilation message

icc.cpp: In function 'int conquer(std::vector<std::vector<int> >&, int)':
icc.cpp:120:13: warning: unused variable 'ret' [-Wunused-variable]
  120 |         int ret = 0 ;
      |             ^~~
icc.cpp: In function 'void divide_or_conquer(std::vector<int>)':
icc.cpp:152:13: warning: variable 'sz' set but not used [-Wunused-but-set-variable]
  152 |         int sz = (int) hs.size() ;
      |             ^~
icc.cpp:157:13: warning: unused variable 'k' [-Wunused-variable]
  157 |         int k = 0 ;
      |             ^
# Verdict Execution time Memory Grader output
1 Correct 16 ms 512 KB Ok! 262 queries used.
2 Correct 15 ms 512 KB Ok! 235 queries used.
# Verdict Execution time Memory Grader output
1 Correct 146 ms 888 KB Ok! 2097 queries used.
2 Correct 123 ms 632 KB Ok! 1734 queries used.
3 Correct 134 ms 796 KB Ok! 1792 queries used.
# Verdict Execution time Memory Grader output
1 Incorrect 387 ms 1144 KB Number of queries more than 4500 out of 2250
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 363 ms 1016 KB Number of queries more than 4000 out of 2000
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 324 ms 1144 KB Number of queries more than 3550 out of 1775
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 297 ms 1144 KB Number of queries more than 3250 out of 1625
2 Halted 0 ms 0 KB -