Submission #255148

# Submission time Handle Problem Language Result Execution time Memory
255148 2020-07-31T11:59:36 Z SamAnd Tropical Garden (IOI11_garden) C++17
100 / 100
3022 ms 42400 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pari
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
typedef long long ll;
const int N = 300005;

int n, f;
vector<int> sg[N];
vector<int> g[N];

int u[N];

int s;
int c[N];
bool dfs(int x)
{
    c[x] = 1;
    if (c[u[x]] == 1)
    {
        s = x;
        c[x] = 2;
        return true;
    }
    if (c[u[x]] == 0)
    {
        if (dfs(u[x]))
        {
            c[x] = 2;
            return true;
        }
    }
    c[x] = 2;
    return false;
}

vector<vector<int> > vv;
int ci[N], cj[N];
bool cc[N];
int d[N];

void dfs1(int x)
{
    if (c[x] == x)
        d[x] = 0;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        if (cc[h])
            continue;
        c[h] = c[x];
        d[h] = d[x] + 1;
        dfs1(h);
    }
}

int ans[N];

void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
    n = N;
    f = P;
    for (int i = 0; i < M; ++i)
    {
        int x = R[i][0];
        int y = R[i][1];
        sg[x].push_back(y);
        sg[y].push_back(x);
    }

    for (int x = 0; x < n; ++x)
    {
        int h = sg[x][0];
        if (sg[h][0] == x)
        {
            u[x] = h + n;
        }
        else
        {
            u[x] = h;
        }
    }
    for (int x = n; x < n + n; ++x)
    {
        int h;
        if (sz(sg[x - n]) == 1)
        {
            h = sg[x - n][0];
        }
        else
        {
            h = sg[x - n][1];
        }
        if (sg[h][0] == x - n)
        {
            u[x] = h + n;
        }
        else
        {
            u[x] = h;
        }
    }

    for (int x = 0; x < n + n; ++x)
    {
        g[u[x]].push_back(x);
    }

    for (int x = 0; x < n + n; ++x)
    {
        if (c[x])
            continue;
        if (dfs(x))
        {
            vector<int> v;
            for (int x = s; ; x = u[x])
            {
                v.push_back(x);
                if (u[x] == s)
                    break;
            }
            vv.push_back(v);
        }
    }

    for (int i = 0; i < vv.size(); ++i)
    {
        for (int j = 0; j < vv[i].size(); ++j)
        {
            int x = vv[i][j];
            ci[x] = i;
            cj[x] = j;
            cc[x] = true;
        }
        for (int j = 0; j < vv[i].size(); ++j)
        {
            int x = vv[i][j];
            c[x] = x;
            dfs1(x);
        }
    }

    {
        for (int i = 0; i < Q; ++i)
        {
            int kk = G[i];
            for (int xx = 0; xx < n; ++xx)
            {
                int x = xx;
                int k = kk;
                if (d[x] > k)
                    continue;
                k -= d[x];
                x = c[x];
                x = vv[ci[x]][(cj[x] + k) % sz(vv[ci[x]])];
                if (x == f || x == f + n)
                    ++ans[i];
            }
        }
    }

    if (!cc[f])
    {
        c[f] = f;
        dfs1(f);
        for (int i = 0; i < Q; ++i)
        {
            int k = G[i];
            for (int x = 0; x < n; ++x)
            {
                if (c[x] == f && d[x] == k)
                    ++ans[i];
            }
        }
    }

    if (!cc[f + n])
    {
        c[f + n] = f + n;
        dfs1(f + n);
        for (int i = 0; i < Q; ++i)
        {
            int k = G[i];
            for (int x = 0; x < n; ++x)
            {
                if (c[x] == f + n && d[x] == k)
                    ++ans[i];
            }
        }
    }

    for (int i = 0; i < Q; ++i)
        answer(ans[i]);
}


Compilation message

garden.cpp: In function 'void dfs1(int)':
garden.cpp:51:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < g[x].size(); ++i)
                     ~~^~~~~~~~~~~~~
garden.cpp: In function 'void count_routes(int, int, int, int (*)[2], int, int*)':
garden.cpp:131:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < vv.size(); ++i)
                     ~~^~~~~~~~~~~
garden.cpp:133:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < vv[i].size(); ++j)
                         ~~^~~~~~~~~~~~~~
garden.cpp:140:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int j = 0; j < vv[i].size(); ++j)
                         ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14592 KB Output is correct
2 Correct 9 ms 14592 KB Output is correct
3 Correct 12 ms 14592 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 9 ms 14464 KB Output is correct
6 Correct 10 ms 14720 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14592 KB Output is correct
9 Correct 11 ms 14756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14592 KB Output is correct
2 Correct 9 ms 14592 KB Output is correct
3 Correct 12 ms 14592 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 9 ms 14464 KB Output is correct
6 Correct 10 ms 14720 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14592 KB Output is correct
9 Correct 11 ms 14756 KB Output is correct
10 Correct 9 ms 14464 KB Output is correct
11 Correct 24 ms 17528 KB Output is correct
12 Correct 39 ms 19192 KB Output is correct
13 Correct 55 ms 31188 KB Output is correct
14 Correct 181 ms 29304 KB Output is correct
15 Correct 192 ms 29676 KB Output is correct
16 Correct 110 ms 25848 KB Output is correct
17 Correct 132 ms 24696 KB Output is correct
18 Correct 40 ms 19448 KB Output is correct
19 Correct 138 ms 29304 KB Output is correct
20 Correct 152 ms 29816 KB Output is correct
21 Correct 116 ms 26076 KB Output is correct
22 Correct 102 ms 24824 KB Output is correct
23 Correct 143 ms 31224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14592 KB Output is correct
2 Correct 9 ms 14592 KB Output is correct
3 Correct 12 ms 14592 KB Output is correct
4 Correct 11 ms 14464 KB Output is correct
5 Correct 9 ms 14464 KB Output is correct
6 Correct 10 ms 14720 KB Output is correct
7 Correct 11 ms 14464 KB Output is correct
8 Correct 11 ms 14592 KB Output is correct
9 Correct 11 ms 14756 KB Output is correct
10 Correct 9 ms 14464 KB Output is correct
11 Correct 24 ms 17528 KB Output is correct
12 Correct 39 ms 19192 KB Output is correct
13 Correct 55 ms 31188 KB Output is correct
14 Correct 181 ms 29304 KB Output is correct
15 Correct 192 ms 29676 KB Output is correct
16 Correct 110 ms 25848 KB Output is correct
17 Correct 132 ms 24696 KB Output is correct
18 Correct 40 ms 19448 KB Output is correct
19 Correct 138 ms 29304 KB Output is correct
20 Correct 152 ms 29816 KB Output is correct
21 Correct 116 ms 26076 KB Output is correct
22 Correct 102 ms 24824 KB Output is correct
23 Correct 143 ms 31224 KB Output is correct
24 Correct 11 ms 14464 KB Output is correct
25 Correct 319 ms 17660 KB Output is correct
26 Correct 545 ms 19320 KB Output is correct
27 Correct 1309 ms 31184 KB Output is correct
28 Correct 2807 ms 29688 KB Output is correct
29 Correct 2996 ms 29920 KB Output is correct
30 Correct 1659 ms 25976 KB Output is correct
31 Correct 1910 ms 24836 KB Output is correct
32 Correct 727 ms 19192 KB Output is correct
33 Correct 2800 ms 29560 KB Output is correct
34 Correct 3022 ms 29852 KB Output is correct
35 Correct 1864 ms 25976 KB Output is correct
36 Correct 2437 ms 24824 KB Output is correct
37 Correct 2672 ms 31352 KB Output is correct
38 Correct 2286 ms 42400 KB Output is correct