QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#234255#1957. Friendship GraphsBUET_Twilight#TL 0ms0kbC++235.7kb2023-11-01 15:12:552023-11-01 15:12:56

Judging History

你现在查看的是最新测评结果

  • [2023-11-01 15:12:56]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-11-01 15:12:55]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0) 
#define setbit0(n, i) ((n) & (~(1LL << (i)))) 
#define setbit1(n, i) ((n) | (1LL << (i))) 
#define togglebit(n, i) ((n) ^ (1LL << (i))) 
#define lastone(n) ((n) & (-(n))) 
char gap = 32;
#define int long long
#define ll long long 
#define lll __int128_t
#define pb push_back

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = 1005;

vector<int> adj[N];
set<int> st[N];
int pushed[N];
bool cmp(int a, int b) {
    return adj[a].size() < adj[b].size();
}

set<int> recurse(vector<int> v){
    //for(auto vv:v) cout<<vv<<" ";
    //cout<<endl;
    
    shuffle(v.begin(),v.end(),rng);
    
    bool isCliq = true;
    for(int i=0;i<v.size();i++){
        for(int j=i+1;j<v.size();j++){
            if( st[v[i]].count(v[j]) == 0 ){
                isCliq = false;
                break;
            }
        }
    }

    if( isCliq ){
        set<int> ret;
        for(int i=0;i<v.size();i++){
            ret.insert(i);
        }
        return ret;
    }

    int a,b;
    for(int i=0;i<v.size();i++){
        for(int j=i+1;j<v.size();j++){
            if( st[v[i]].count(v[j]) == 0 ){
                a = v[i];
                b = v[j];
                break;
            }
        }
    }
    //cout<<"de "<<a<<" "<<b<<endl;
    int n = v.size();
    vector<int> va,vb,vc,vall;

    va.push_back(a);
    vb.push_back(b);
   // cout<<"de "<<a<<" "<<b<<endl;
   // shuffle(v.begin(),v.end(),rng);

    vector<int> nxt;

    while(true){
        bool flag = true;
        nxt.clear();
        for(auto i:v){
            if( i == a ) continue;
            if( i == b ) continue;
            
            bool posa = true;
            bool posb = true;

            for(auto x:va){
                if( st[i].count(x) == 0 ){
                    posa = false;
                    break;
                }
            }

            for(auto x:vb){
                if( st[i].count(x) == 0 ){
                    posb = false;
                    break;
                }
            }

            if( posa and !posb ){
                //cout<<"inserted in va "<<i<<endl;
                va.push_back(i);
                flag = false;
                continue;
            }
            if( posb and !posa ){
               // cout<<"inserted in vb "<<i<<endl;
                vb.push_back(i);
                flag = false;
                continue;
            }

            if( !posa and !posb ){
                cout<<-1;
                exit(0);
            }

            nxt.push_back(i);
        
        }

        if( flag == true ) break;
        v = nxt;

    }

    if( nxt.size()){
        set<int> ret = recurse(nxt);
        set<int> ret2;
        for(auto r:ret) {
            //cout<<r<<"< ";
            ret2.insert(r+va.size());
            ret2.insert(r+vb.size());
        }
        return ret2;
    }else{
        return {(int)va.size(),(int)vb.size()};
    }
    return {};
}

void solve() {
    int n, m;
    cin>>n>>m;
    for (int i=0; i<m; i++) {
        int u, v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
        st[u].insert(v);
        st[v].insert(u);
    }
    
    if( m == n*(n-1)/2 ){
        cout<<n%2;
        return;
    }
    vector<int> v;
    for(int i=1;i<=n;i++) v.pb(i);
    set<int> ret = recurse(v);

    int mn = 1e9;
    cout<<endl;
    for(auto r:ret){
       // cout<<r<<"<< ";
        mn = min(mn,abs(n-2*r));
    }

    cout<<mn;
    
/*
    // for(auto a:va) cout<<a<<" "; cout<<endl;
    // for(auto a:vb) cout<<a<<" "; cout<<endl;
    // cout<<endl;

    for(int i=0;i<va.size();i++){
        for(int j=i+1;j<va.size();j++){
            if( st[va[i]].count(va[j]) == 0 ){
               // cout<<"aa "<<va[i]<<" "<<va[j]<<endl;
                cout<<-1;
                return;
            }
        }
    }
    
    for(int i=0;i<vb.size();i++){
        for(int j=i+1;j<vb.size();j++){
            if( st[vb[i]].count(vb[j]) == 0 ){
                cout<<-1;
                return;
            }
        }
    }

    

    sort(vc.begin(),vc.end(),cmp);
    for(int it=0;it<vc.size();it++){
        int c = vc[it];
    //    cout<<"de "<<c<<" ";
        bool posa = true;
        for(auto x:va){
            if( st[c].count(x) == 0 ){
                posa = false;
                break;
            }
        }
        
        bool posb = true;
        for(auto x:vb){
            if( st[c].count(x) == 0 ){
                posb = false;
                break;
            }
        }
        if(posb and !posa){
            vb.push_back(c);
            continue;
        }
        if(posa and !posb){
            va.push_back(c);
            continue;
        }
        if( posa and posb ){
            if(pushed[c]>=10){
                if( va.size() >= vb.size() ) vb.push_back(c);
                else va.push_back(c);
                
            }else{
                pushed[c]++;
                vc.push_back(c);
            }
            
            continue;
        }
        cout<<-1;
        return;
    }
    while(vall.size()){
        if( va.size()>=vb.size() ){
            vb.push_back(vall.back());
            vall.pop_back();
        } else{
            va.push_back(vall.back());
            vall.pop_back();
        }
    }

    cout<<abs((int)va.size()-(int)vb.size())<<endl;
    */

}
signed main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    
    solve();
    return 0;
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

1000 499000
20 615
260 390
779 37
13 563
650 605
358 684
783 370
162 90
379 662
88 208
458 371
178 3
590 762
455 514
738 641
270 805
205 881
205 315
837 54
976 579
519 532
982 669
563 804
648 274
268 293
182 392
337 772
961 603
294 607
546 772
189 218
702 266
515 610
691 965
643 235
509 193
184 302
...

output:


result: