Submission #286451

# Submission time Handle Problem Language Result Execution time Memory
286451 2020-08-30T12:21:55 Z stoyan_malinin Simurgh (IOI17_simurgh) C++14
30 / 100
3000 ms 6128 KB
#include "simurgh.h"
//#include "grader.cpp"

#include <set>
#include <random>
#include <vector>
#include <assert.h>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXN = 250;
mt19937 rnd(22);

int ASKED = 0;

struct DSU
{
    int parent[MAXN*MAXN];
    int color[MAXN*MAXN];

    DSU(){}
    DSU(int n)
    {
        for(int i = 0;i<=n;i++)
        {
            this->parent[i] = i;
            this->color[i] = -1;
        }
    }

    void reset(int n)
    {
        for(int i = 0;i<=n;i++)
        {
            this->parent[i] = i;
            this->color[i] = -1;
        }
    }

    int Find(int x)
    {
        if(parent[x]==x) return x;

        parent[x] = Find(parent[x]);
        return parent[x];
    }

    bool UnionSimple(int u, int v)
    {
        u = Find(u);
        v = Find(v);
        if(u==v) return false;

        parent[v] = u;
        return true;
    }

    bool UnionFull(int u, int v)
    {
        u = Find(u);
        v = Find(v);
        if(u==v) return false;

        int newColor = max(color[u], color[v]);
        color[u] = newColor;
        color[v] = newColor;

        parent[v] = u;
        return true;
    }

    int getColor(int x)
    {
        return color[Find(x)];
    }

    void colorize(int x, int col)
    {
        x = Find(x);
        color[x] = col;
    }
};

DSU relationKeeper;

int n;
vector <pair <int, int>> edges;

DSU T;
vector <int> inds;

vector <int> constructST()
{
    T.reset(n);
    vector <int> ST;

    for(int i: inds)
    {
        if(relationKeeper.getColor(i)==1)
        {
            T.UnionSimple(edges[i].first, edges[i].second);
            ST.push_back(i);
        }
    }

    for(int i: inds)
    {
        if(relationKeeper.getColor(i)!=0 && T.UnionSimple(edges[i].first, edges[i].second)==true)
        {
            ST.push_back(i);
        }
    }

    return ST;
}

int askVector(vector <int> v)
{
    ASKED++;
    if(ASKED==30*1000) assert(false);

    return count_common_roads(v);
}

int askSet(set <int> s)
{
    ASKED++;
    if(ASKED==30*1000) assert(false);

    vector <int> v;
    for(int x: s) v.push_back(x);

    return count_common_roads(v);
}

vector <int> getReplacements(vector <int> &ST, int e)
{
    T.reset(n);

    for(int edgeInd: ST)
        if(edgeInd!=e) T.UnionSimple(edges[edgeInd].first, edges[edgeInd].second);

    vector <int> out;
    for(int i = 0;i<edges.size();i++)
    {
        if(i==e) continue;
        if(T.Find(edges[i].first)==T.Find(edges[i].second)) continue;

        out.push_back(i);
        if(relationKeeper.getColor(i)!=-1) break;
    }

    return out;
}

int getDiff(vector <int> &ST, int e1, int e2, int base)
{
    ST.push_back(e2);
    int res = askVector(ST);
    ST.pop_back();

    if(base==res) return 0;
    if(base<res) return 1;
    if(base>res) return 2;
}

void processDif(int e1, int e2, int diff)
{
    if(diff==0)
    {
        relationKeeper.UnionFull(e1, e2);
    }
    else if(diff==1)
    {
        relationKeeper.colorize(e1, 0);
        relationKeeper.colorize(e2, 1);
    }
    else if(diff==2)
    {
        relationKeeper.colorize(e1, 1);
        relationKeeper.colorize(e2, 0);
    }
}

vector <int> set2Vector(set <int> s)
{
    vector <int> out;
    for(int x: s) out.push_back(x);

    return out;
}

vector<int> find_roads(int N, vector<int> U, vector<int> V)
{
    n = N;
    for(int i = 0;i<U.size();i++) edges.push_back({U[i], V[i]});
    relationKeeper = DSU(edges.size());

    for(int i = 0;i<edges.size();i++)
        inds.push_back(i);

    for(int iter = 0;iter<=n*n/2;iter++)
    {
        shuffle(inds.begin(), inds.end(), rnd);
        vector <int> ST = constructST();

        //cout << "currST: ";
        //for(int x: ST) cout << " " << x;
        //cout << '\n';

        int royalCnt = askVector(ST);
        if(royalCnt==n-1) return ST;

        int e = -1;
        for(int i = 0;i<ST.size();i++)
        {
            if(relationKeeper.getColor(ST[i])==-1)
            {
                e = ST[i];
                ST.erase(ST.begin()+i);

                break;
            }
        }
        //if(e==-1) return {};

        //cout << " --- " << e << " --- " << '\n';

        vector <int> replacements = getReplacements(ST, e);

        //cout << "replacements:";
        //for(int x: replacements) cout << " " << x;
        //cout << '\n';

        for(int edgeInd: replacements)
        {
            if(relationKeeper.getColor(edgeInd)!=-1)
            {
                int diff = getDiff(ST, e, edgeInd, royalCnt);
                processDif(e, edgeInd, diff);

                break;
            }
        }

        if(relationKeeper.getColor(e)==-1)
        {
            for(int edgeInd: replacements)
            {
                //if(edgeInd==e) return {};
                //if(!(edgeInd>=0 && edgeInd<edges.size())) return {};

                int diff = getDiff(ST, e, edgeInd, royalCnt);
                processDif(e, edgeInd, diff);
            }

            if(relationKeeper.getColor(e)==-1)
            {
                relationKeeper.colorize(e, 1);
                for(int edgeInd: replacements)
                    relationKeeper.colorize(edgeInd, 1);
            }
        }

        //cout << "color of " << e << " is " << relationKeeper.getColor(e) << '\n';
    }

    return {};
}
/*
4 6
0 1
0 2
0 3
1 2
1 3
2 3
0 1 5
*/

Compilation message

simurgh.cpp: In function 'std::vector<int> getReplacements(std::vector<int>&, int)':
simurgh.cpp:146:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  146 |     for(int i = 0;i<edges.size();i++)
      |                   ~^~~~~~~~~~~~~
simurgh.cpp: In function 'std::vector<int> find_roads(int, std::vector<int>, std::vector<int>)':
simurgh.cpp:198:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  198 |     for(int i = 0;i<U.size();i++) edges.push_back({U[i], V[i]});
      |                   ~^~~~~~~~~
simurgh.cpp:201:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  201 |     for(int i = 0;i<edges.size();i++)
      |                   ~^~~~~~~~~~~~~
simurgh.cpp:217:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  217 |         for(int i = 0;i<ST.size();i++)
      |                       ~^~~~~~~~~~
simurgh.cpp: In function 'int getDiff(std::vector<int>&, int, int, int)':
simurgh.cpp:167:1: warning: control reaches end of non-void function [-Wreturn-type]
  167 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB correct
2 Correct 1 ms 768 KB correct
3 Correct 1 ms 768 KB correct
4 Correct 1 ms 768 KB correct
5 Correct 1 ms 768 KB correct
6 Correct 1 ms 768 KB correct
7 Correct 1 ms 768 KB correct
8 Correct 1 ms 768 KB correct
9 Correct 1 ms 768 KB correct
10 Correct 1 ms 768 KB correct
11 Correct 1 ms 768 KB correct
12 Correct 1 ms 768 KB correct
13 Correct 1 ms 896 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB correct
2 Correct 1 ms 768 KB correct
3 Correct 1 ms 768 KB correct
4 Correct 1 ms 768 KB correct
5 Correct 1 ms 768 KB correct
6 Correct 1 ms 768 KB correct
7 Correct 1 ms 768 KB correct
8 Correct 1 ms 768 KB correct
9 Correct 1 ms 768 KB correct
10 Correct 1 ms 768 KB correct
11 Correct 1 ms 768 KB correct
12 Correct 1 ms 768 KB correct
13 Correct 1 ms 896 KB correct
14 Correct 38 ms 896 KB correct
15 Correct 38 ms 896 KB correct
16 Correct 40 ms 896 KB correct
17 Correct 33 ms 916 KB correct
18 Correct 5 ms 896 KB correct
19 Correct 33 ms 896 KB correct
20 Correct 16 ms 896 KB correct
21 Correct 21 ms 896 KB correct
22 Correct 17 ms 1024 KB correct
23 Correct 13 ms 896 KB correct
24 Correct 11 ms 896 KB correct
25 Correct 2 ms 896 KB correct
26 Correct 14 ms 896 KB correct
27 Correct 16 ms 896 KB correct
28 Correct 5 ms 1024 KB correct
29 Correct 2 ms 896 KB correct
30 Correct 17 ms 896 KB correct
31 Correct 16 ms 896 KB correct
32 Correct 16 ms 896 KB correct
33 Correct 15 ms 896 KB correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB correct
2 Correct 1 ms 768 KB correct
3 Correct 1 ms 768 KB correct
4 Correct 1 ms 768 KB correct
5 Correct 1 ms 768 KB correct
6 Correct 1 ms 768 KB correct
7 Correct 1 ms 768 KB correct
8 Correct 1 ms 768 KB correct
9 Correct 1 ms 768 KB correct
10 Correct 1 ms 768 KB correct
11 Correct 1 ms 768 KB correct
12 Correct 1 ms 768 KB correct
13 Correct 1 ms 896 KB correct
14 Correct 38 ms 896 KB correct
15 Correct 38 ms 896 KB correct
16 Correct 40 ms 896 KB correct
17 Correct 33 ms 916 KB correct
18 Correct 5 ms 896 KB correct
19 Correct 33 ms 896 KB correct
20 Correct 16 ms 896 KB correct
21 Correct 21 ms 896 KB correct
22 Correct 17 ms 1024 KB correct
23 Correct 13 ms 896 KB correct
24 Correct 11 ms 896 KB correct
25 Correct 2 ms 896 KB correct
26 Correct 14 ms 896 KB correct
27 Correct 16 ms 896 KB correct
28 Correct 5 ms 1024 KB correct
29 Correct 2 ms 896 KB correct
30 Correct 17 ms 896 KB correct
31 Correct 16 ms 896 KB correct
32 Correct 16 ms 896 KB correct
33 Correct 15 ms 896 KB correct
34 Execution timed out 3080 ms 2172 KB Time limit exceeded
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB correct
2 Correct 1 ms 896 KB correct
3 Runtime error 24 ms 6128 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 768 KB correct
2 Correct 1 ms 768 KB correct
3 Correct 1 ms 768 KB correct
4 Correct 1 ms 768 KB correct
5 Correct 1 ms 768 KB correct
6 Correct 1 ms 768 KB correct
7 Correct 1 ms 768 KB correct
8 Correct 1 ms 768 KB correct
9 Correct 1 ms 768 KB correct
10 Correct 1 ms 768 KB correct
11 Correct 1 ms 768 KB correct
12 Correct 1 ms 768 KB correct
13 Correct 1 ms 896 KB correct
14 Correct 38 ms 896 KB correct
15 Correct 38 ms 896 KB correct
16 Correct 40 ms 896 KB correct
17 Correct 33 ms 916 KB correct
18 Correct 5 ms 896 KB correct
19 Correct 33 ms 896 KB correct
20 Correct 16 ms 896 KB correct
21 Correct 21 ms 896 KB correct
22 Correct 17 ms 1024 KB correct
23 Correct 13 ms 896 KB correct
24 Correct 11 ms 896 KB correct
25 Correct 2 ms 896 KB correct
26 Correct 14 ms 896 KB correct
27 Correct 16 ms 896 KB correct
28 Correct 5 ms 1024 KB correct
29 Correct 2 ms 896 KB correct
30 Correct 17 ms 896 KB correct
31 Correct 16 ms 896 KB correct
32 Correct 16 ms 896 KB correct
33 Correct 15 ms 896 KB correct
34 Execution timed out 3080 ms 2172 KB Time limit exceeded
35 Halted 0 ms 0 KB -