Submission #212780

# Submission time Handle Problem Language Result Execution time Memory
212780 2020-03-24T09:39:13 Z Alexa2001 Chameleon's Love (JOI20_chameleon) C++17
100 / 100
54 ms 512 KB
#include "chameleon.h"
#include <vector>
#include <bits/stdc++.h>

using namespace std;

namespace
{
    vector<int> bad[2000];
    int n;
    int color[2000];
}

void dfs(int node, int C = 1)
{
    color[node] = C;
    for(auto it : bad[node])
        if(!color[it])
            dfs(it, 3 - C);
}

static void color_nodes()
{
    memset(color, 0, sizeof(color));

    int i;
    for(i=1; i<=2*n; ++i)
        if(!color[i])
            dfs(i);
}

void add_edge(int x, int y)
{
    bad[x].push_back(y);
    bad[y].push_back(x);
}

void cbin(int node, const vector<int> &v)
{
    int i, st, dr, mid;
    st = 0; dr = v.size() - 1;

    while(1)
    {
        vector<int> q;
        for(i=st; i<=dr; ++i) q.push_back(v[i]);
        q.push_back(node);

        if(Query(q) == q.size()) return;

        while(st < dr)
        {
            mid = (st + dr) / 2;
            q.clear();
            for(i=st; i<=mid; ++i) q.push_back(v[i]);
            q.push_back(node);

            if(Query(q) == q.size()) st = mid + 1;
                else dr = mid;
        }

        add_edge(node, v[st]);

        ++st; dr = v.size() - 1;
    }
}

static void find_bad()
{
    int i, j;

    for(i=1; i<=2*n; ++i)
    {
        color_nodes();

        vector<int> A[2];

        for(j=1; j<i; ++j)
            A[color[j] - 1].push_back(j);

        cbin(i, A[0]);
        cbin(i, A[1]);
    }
}

static void find_opposite()
{
    color_nodes();

    int i, j, k;

    for(i=1; i<=2*n; ++i)
    {
        if(color[i] == 2) continue;

        for(auto it : bad[i])
            assert(color[i] + color[it] == 3);

        if(bad[i].size() == 1)
            Answer(i, bad[i][0]);
        else
        {
            assert(bad[i].size() == 3);

            bool ok[3];
            ok[0] = ok[1] = ok[2] = 1;

            for(j=0; j<3; ++j)
                for(k=j+1; k<3; ++k)
                {
                    vector<int> vv = {i, bad[i][j], bad[i][k]};
                    if(Query(vv) == 1) ok[3 - j - k] = 0;
                }

            for(j=0; j<3; ++j)
            {
                int all[2], nr = 0;

                for(auto it : bad[bad[i][j]])
                    if(it != i)
                        all[nr++] = it;

                vector<int> vv = {bad[i][j], all[0], all[1]};
                if(vv.back() == i) continue;

                if(Query(vv) == 1) ok[j] = 0;
            }

            bool cnt = 0;
            for(j=0; j<3; ++j) cnt += ok[j];
            assert(cnt == 1);

            for(j=0; j<3; ++j)
                if(ok[j])
                    Answer(i, bad[i][j]);
        }
    }
}

void Solve(int N)
{
    n = N;
    find_bad();
    find_opposite();
}

Compilation message

chameleon.cpp: In function 'void cbin(int, const std::vector<int>&)':
chameleon.cpp:49:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(Query(q) == q.size()) return;
            ~~~~~~~~~^~~~~~~~~~~
chameleon.cpp:58:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             if(Query(q) == q.size()) st = mid + 1;
                ~~~~~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 32 ms 384 KB Output is correct
4 Correct 32 ms 384 KB Output is correct
5 Correct 32 ms 384 KB Output is correct
6 Correct 33 ms 504 KB Output is correct
7 Correct 32 ms 384 KB Output is correct
8 Correct 34 ms 384 KB Output is correct
9 Correct 33 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 4 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 5 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 5 ms 384 KB Output is correct
11 Correct 5 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 5 ms 384 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 52 ms 460 KB Output is correct
4 Correct 52 ms 384 KB Output is correct
5 Correct 52 ms 384 KB Output is correct
6 Correct 52 ms 504 KB Output is correct
7 Correct 54 ms 512 KB Output is correct
8 Correct 51 ms 504 KB Output is correct
9 Correct 52 ms 488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 32 ms 384 KB Output is correct
4 Correct 32 ms 384 KB Output is correct
5 Correct 32 ms 384 KB Output is correct
6 Correct 33 ms 504 KB Output is correct
7 Correct 32 ms 384 KB Output is correct
8 Correct 34 ms 384 KB Output is correct
9 Correct 33 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 4 ms 384 KB Output is correct
12 Correct 5 ms 384 KB Output is correct
13 Correct 4 ms 384 KB Output is correct
14 Correct 4 ms 384 KB Output is correct
15 Correct 5 ms 384 KB Output is correct
16 Correct 5 ms 384 KB Output is correct
17 Correct 5 ms 384 KB Output is correct
18 Correct 4 ms 384 KB Output is correct
19 Correct 5 ms 384 KB Output is correct
20 Correct 5 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 5 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 5 ms 384 KB Output is correct
25 Correct 5 ms 384 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 5 ms 384 KB Output is correct
28 Correct 5 ms 384 KB Output is correct
29 Correct 4 ms 384 KB Output is correct
30 Correct 52 ms 460 KB Output is correct
31 Correct 52 ms 384 KB Output is correct
32 Correct 52 ms 384 KB Output is correct
33 Correct 52 ms 504 KB Output is correct
34 Correct 54 ms 512 KB Output is correct
35 Correct 51 ms 504 KB Output is correct
36 Correct 52 ms 488 KB Output is correct
37 Correct 52 ms 512 KB Output is correct
38 Correct 32 ms 384 KB Output is correct
39 Correct 52 ms 384 KB Output is correct
40 Correct 52 ms 504 KB Output is correct
41 Correct 52 ms 504 KB Output is correct
42 Correct 32 ms 384 KB Output is correct
43 Correct 52 ms 384 KB Output is correct
44 Correct 52 ms 504 KB Output is correct
45 Correct 52 ms 384 KB Output is correct