Submission #374966

#TimeUsernameProblemLanguageResultExecution timeMemory
374966BartolMMeetings (JOI19_meetings)C++17
29 / 100
2855 ms1052 KiB
#include "meetings.h"
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=2005;

int n, uk;
int node[N], komp[N];
vector <int> vec[N];

#define DEBUG 0
void upit(int x, int y) {
    #if DEBUG
        printf("%d <-> %d\n", x, y);
    #endif
    Bridge(min(x, y), max(x, y));
}



void resi(vector <int> &v) {
    int curr=0;
    int koji[18];
    int cent=v[rand()%(int)v.size()];
    #if DEBUG
        printf("cent==%d, resi: ", cent);
        for (int x:v) printf("%d ", x);
        printf("\n");
    #endif // DEBUG

    for (int x:v) {
        if (x==cent) continue;
        int fl=0;
        for (int i=0; i<curr; ++i) {
            int y=Query(node[cent], node[koji[i]], node[x]);
            if (y==node[cent]) continue;
            komp[x]=uk+i;
            fl=1;
            if (y==node[x]) koji[i]=x;
            break;
        }
        if (!fl) {
            komp[x]=curr+uk;
            koji[curr++]=x;
        }
    }
    for (int i=0; i<curr; ++i) upit(node[cent], node[koji[i]]);
    komp[cent]=uk+curr;
    uk+=curr+1;
}

void Solve(int nn) {
    n=nn;
    srand(time(0));
    for (int i=0; i<n; ++i) node[i]=i, komp[i]=0;
    random_shuffle(node, node+n);

    int da=1;
    while (da) {
        da=0;
        uk=0;
        for (int i=0; i<n; ++i) vec[i].clear();
        for (int i=0; i<n; ++i) vec[komp[i]].pb(i);
        for (int i=0; i<n; ++i) {
            if (vec[i].size()>1) {
                resi(vec[i]);
                da=1;
            }
            else if (vec[i].size()) komp[vec[i][0]]=uk++;
        }
    }
}
/*
5
0 1
0 2
1 3
1 4
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...