답안 #347874

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
347874 2021-01-13T17:19:41 Z idk321 열대 식물원 (Tropical Garden) (IOI11_garden) C++11
100 / 100
4323 ms 21596 KB
#include "garden.h"
#include "gardenlib.h"

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int N = 150001;
vector<array<int, 2>> adj[N];
int dist[N][2][2];
int onCycle[N][2];
int p;

void dfs(int node, int type, int c)
{

    //cout << c << " oj " << node << " " << type << " " <<dist[node][type][c] << endl;
    for (int i = 0; i < adj[node].size(); i++)
    {
        if (adj[node].size() > 1 && i == type) continue;
        if (type == 1 && i > 0) break;
        auto adjacent = adj[node][i];
        //cout << "eh " << node << " " << type << " " << dist[node][type][c] << " " << adjacent[0] << endl;
        int next = -1;
        if (adj[adjacent[0]].size() == 1 || adj[adjacent[0]][0][1] == adjacent[1])
        {
            next = 0;
        } else if (adj[adjacent[0]][1][1] == adjacent[1])
        {
            next = 1;
        }
        if (next != -1)
        {
            if (dist[adjacent[0]][next][c] != -1)
            {
                if (adjacent[0] == p && next == c)
                {
                    //cout << "oj" << endl;
                    onCycle[p][c] = dist[node][type][c] + 1;
                }
                continue;
            }
            dist[adjacent[0]][next][c] = dist[node][type][c] + 1;
            dfs(adjacent[0], next, c);
        }
    }
}

void count_routes(int n, int m, int pa, int R[][2], int Q, int G[])
{

    p = pa;
  for (int i = 0; i < m; i++)
  {
    adj[R[i][0]].push_back({R[i][1], i + 1});
    adj[R[i][1]].push_back({R[i][0], i + 1});
  }

  for (int i = 0; i < N; i++)
  {
        for (int j = 0; j <= 1; j++) for (int l = 0; l <= 1; l++) dist[i][j][l] = -1;
    }

    dist[p][0][0] = 0;
    dist[p][1][1] = 0;
    dfs(p, 0, 0);
    dfs(p, 1, 1);



  for(int i=0; i<Q; i++)
  {
    int sum = 0;
    int cdist = G[i];
    for (int j = 0; j < n; j++)
    {
        //if (i == 0) cout << "a " << onCycle[p][0]<< " " << onCycle[p][1] << " " << dist[j][0][0] << " " << dist[j][0][1] << " " << j <<  endl;
        for (int k = 0; k <= 1; k++)
        {
            if (dist[j][0][k] != -1)
            {
                if (dist[j][0][k] == cdist) sum++;
                else if (onCycle[p][k] && cdist > dist[j][0][k] && dist[j][0][k] % onCycle[p][k] == cdist % onCycle[p][k])
                {
                    sum++;
                }
            }
        }
    }
      answer(sum);
  }
}

/*
4 4 0
1 2
2 3
3 1
0 1
20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

*/


Compilation message

garden.cpp: In function 'void dfs(int, int, int)':
garden.cpp:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int i = 0; i < adj[node].size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6380 KB Output is correct
2 Correct 4 ms 6252 KB Output is correct
3 Correct 5 ms 6252 KB Output is correct
4 Correct 4 ms 6252 KB Output is correct
5 Correct 4 ms 6252 KB Output is correct
6 Correct 5 ms 6380 KB Output is correct
7 Correct 4 ms 6252 KB Output is correct
8 Correct 5 ms 6252 KB Output is correct
9 Correct 7 ms 6508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6380 KB Output is correct
2 Correct 4 ms 6252 KB Output is correct
3 Correct 5 ms 6252 KB Output is correct
4 Correct 4 ms 6252 KB Output is correct
5 Correct 4 ms 6252 KB Output is correct
6 Correct 5 ms 6380 KB Output is correct
7 Correct 4 ms 6252 KB Output is correct
8 Correct 5 ms 6252 KB Output is correct
9 Correct 7 ms 6508 KB Output is correct
10 Correct 5 ms 6252 KB Output is correct
11 Correct 12 ms 7276 KB Output is correct
12 Correct 23 ms 8428 KB Output is correct
13 Correct 46 ms 20204 KB Output is correct
14 Correct 71 ms 12652 KB Output is correct
15 Correct 99 ms 12780 KB Output is correct
16 Correct 83 ms 12288 KB Output is correct
17 Correct 83 ms 11884 KB Output is correct
18 Correct 24 ms 8428 KB Output is correct
19 Correct 80 ms 12524 KB Output is correct
20 Correct 98 ms 12780 KB Output is correct
21 Correct 82 ms 11884 KB Output is correct
22 Correct 78 ms 11628 KB Output is correct
23 Correct 70 ms 13036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6380 KB Output is correct
2 Correct 4 ms 6252 KB Output is correct
3 Correct 5 ms 6252 KB Output is correct
4 Correct 4 ms 6252 KB Output is correct
5 Correct 4 ms 6252 KB Output is correct
6 Correct 5 ms 6380 KB Output is correct
7 Correct 4 ms 6252 KB Output is correct
8 Correct 5 ms 6252 KB Output is correct
9 Correct 7 ms 6508 KB Output is correct
10 Correct 5 ms 6252 KB Output is correct
11 Correct 12 ms 7276 KB Output is correct
12 Correct 23 ms 8428 KB Output is correct
13 Correct 46 ms 20204 KB Output is correct
14 Correct 71 ms 12652 KB Output is correct
15 Correct 99 ms 12780 KB Output is correct
16 Correct 83 ms 12288 KB Output is correct
17 Correct 83 ms 11884 KB Output is correct
18 Correct 24 ms 8428 KB Output is correct
19 Correct 80 ms 12524 KB Output is correct
20 Correct 98 ms 12780 KB Output is correct
21 Correct 82 ms 11884 KB Output is correct
22 Correct 78 ms 11628 KB Output is correct
23 Correct 70 ms 13036 KB Output is correct
24 Correct 6 ms 6252 KB Output is correct
25 Correct 166 ms 7404 KB Output is correct
26 Correct 232 ms 8428 KB Output is correct
27 Correct 4323 ms 20268 KB Output is correct
28 Correct 1412 ms 12912 KB Output is correct
29 Correct 4154 ms 12908 KB Output is correct
30 Correct 2550 ms 12396 KB Output is correct
31 Correct 2352 ms 12012 KB Output is correct
32 Correct 225 ms 8556 KB Output is correct
33 Correct 1406 ms 12652 KB Output is correct
34 Correct 4134 ms 12908 KB Output is correct
35 Correct 2676 ms 12016 KB Output is correct
36 Correct 2368 ms 11676 KB Output is correct
37 Correct 1168 ms 13256 KB Output is correct
38 Correct 3813 ms 21596 KB Output is correct