Submission #432971

# Submission time Handle Problem Language Result Execution time Memory
432971 2021-06-18T16:20:32 Z JeanBombeur Tropical Garden (IOI11_garden) C++17
100 / 100
123 ms 17448 KB
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include "garden.h"
#include "gardenlib.h"
using namespace std;

//  <|°_°|>

const int INFINI = (1000 * 1000 * 1000 + 1);
const int MAX_NOEUDS = (150 * 1000);
const int MAX_ARETES = (300 * 1000 + 1);
const int MAX_REQUETES = (2000);

vector <int> Voisins[MAX_NOEUDS];

bool Depart[MAX_ARETES];

bool Vu[MAX_ARETES];

int Fils[MAX_ARETES];

int Dist[MAX_ARETES];

pair <int, int> Ans[MAX_REQUETES];

int Occ[MAX_ARETES];

pair <int, int> AretesSorties;

void Set(int noeud, bool start) {
    int mini = INFINI, mini2 = INFINI;
    int dest = -1, dest2 = -1;
    for (int id : Voisins[noeud])
    {
        if (id / 2 < mini)
        {
            mini = id / 2;
            dest = id;
        }
    }
    dest2 = dest;
    for (int id : Voisins[noeud])
    {
        if (id / 2 != mini && id / 2 < mini2)
        {
            mini2 = id / 2;
            dest2 = id;
        }
    }
    for (int id : Voisins[noeud])
    {
        if (id != dest)
            Fils[id ^ 1] = dest;
        else
            Fils[id ^ 1] = dest2;
    }
    if (dest2 == dest)
        dest2 = -1;
    if (start)
        AretesSorties = {dest, dest2};
    Depart[dest] = true;
    return;
}

int FindDist(int noeud, int out) {
    if (noeud == out)
        return 0;
    if (Vu[noeud])
        return Dist[noeud];
    Vu[noeud] = true;
    return Dist[noeud] = FindDist(Fils[noeud], out) + 1;
}

void SetDist(int dep, int nbAretes) {
    for (int i = 0; i < nbAretes; i ++)
    {
        Dist[i] = INFINI;
        Vu[i] = false;
    }
    for (int i = 0; i < nbAretes; i ++)
    {
        if (!Vu[i])
            FindDist(i, dep);
    }
    return;
}

void count_routes(int nbNoeuds, int nbAretes, int sortie, int Aretes[][2], int nbRequetes, int Requetes[]) {
    
    for (int i = 0; i < nbAretes; i ++)
    {
        Voisins[Aretes[i][0]].push_back(2 * i);
        Voisins[Aretes[i][1]].push_back(2 * i + 1);
    }
    nbAretes *= 2;
    for (int i = 0; i < nbNoeuds; i ++)
    {
        Set(i, i == sortie);
    }
    for (int i = 0; i < nbRequetes; i ++)
    {
        Ans[i] = {Requetes[i], i};
    }
    sort(Ans, Ans + nbRequetes);
    for (int i = 0; i < nbRequetes; i ++)
    {
        Ans[i].first = 0;
    }
    SetDist(AretesSorties.first, nbAretes);
    int tailleCycle = Dist[Fils[AretesSorties.first]] + 1;
    Dist[AretesSorties.first] = tailleCycle;
    int maxi = 0;
    for (int i = 0; i < nbAretes; i ++)
    {
        if (Depart[i] && Dist[i] < INFINI)
        {
            maxi = max(maxi, Dist[i]);
            Occ[Dist[i]] ++;
        }
    }
    int cur = 0;
    for (int i = 0; i < nbRequetes; i ++)
    {
        while (cur <= min(maxi, Requetes[Ans[i].second]))
        {
            if (cur >= tailleCycle)
                Occ[cur % tailleCycle] += Occ[cur];
            cur ++;
        }
        if (Requetes[Ans[i].second] % tailleCycle < MAX_ARETES)
            Ans[i].first += Occ[Requetes[Ans[i].second] % tailleCycle];
    }
    if (AretesSorties.second >= 0)
    {
        SetDist(AretesSorties.second, nbAretes);
        tailleCycle = Dist[Fils[AretesSorties.second]] + 1;
        Dist[AretesSorties.second] = tailleCycle;
        
        for (int i = 0; i <= maxi; i ++)
        {
            Occ[i] = 0;
        }
        maxi = 0;
        for (int i = 0; i < nbAretes; i ++)
        {
            if (Depart[i] && Dist[i] < INFINI)
            {
                maxi = max(maxi, Dist[i]);
                Occ[Dist[i]] ++;
            }
        }
        cur = 0;
        for (int i = 0; i < nbRequetes; i ++)
        {
            while (cur <= min(maxi, Requetes[Ans[i].second]))
            {
                if (cur >= tailleCycle)
                    Occ[cur % tailleCycle] += Occ[cur];
                cur ++;
            }
            if (Requetes[Ans[i].second] % tailleCycle < MAX_ARETES)
                Ans[i].first += Occ[Requetes[Ans[i].second] % tailleCycle];
        }
    }
    for (int i = 0; i < nbRequetes; i ++)
    {
        swap(Ans[i].first, Ans[i].second);
    }
    sort(Ans, Ans + nbRequetes);
    for (int i = 0; i < nbRequetes; i ++)
    {
        answer(Ans[i].second);
    }
    return;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3916 KB Output is correct
2 Correct 3 ms 3916 KB Output is correct
3 Correct 3 ms 3916 KB Output is correct
4 Correct 3 ms 3788 KB Output is correct
5 Correct 3 ms 3788 KB Output is correct
6 Correct 3 ms 3876 KB Output is correct
7 Correct 3 ms 3788 KB Output is correct
8 Correct 3 ms 3916 KB Output is correct
9 Correct 5 ms 4172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3916 KB Output is correct
2 Correct 3 ms 3916 KB Output is correct
3 Correct 3 ms 3916 KB Output is correct
4 Correct 3 ms 3788 KB Output is correct
5 Correct 3 ms 3788 KB Output is correct
6 Correct 3 ms 3876 KB Output is correct
7 Correct 3 ms 3788 KB Output is correct
8 Correct 3 ms 3916 KB Output is correct
9 Correct 5 ms 4172 KB Output is correct
10 Correct 2 ms 3788 KB Output is correct
11 Correct 12 ms 5196 KB Output is correct
12 Correct 26 ms 6640 KB Output is correct
13 Correct 37 ms 12120 KB Output is correct
14 Correct 102 ms 12040 KB Output is correct
15 Correct 123 ms 12388 KB Output is correct
16 Correct 80 ms 10816 KB Output is correct
17 Correct 70 ms 10488 KB Output is correct
18 Correct 27 ms 7140 KB Output is correct
19 Correct 79 ms 13696 KB Output is correct
20 Correct 94 ms 14092 KB Output is correct
21 Correct 91 ms 12364 KB Output is correct
22 Correct 72 ms 11976 KB Output is correct
23 Correct 86 ms 14624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 3916 KB Output is correct
2 Correct 3 ms 3916 KB Output is correct
3 Correct 3 ms 3916 KB Output is correct
4 Correct 3 ms 3788 KB Output is correct
5 Correct 3 ms 3788 KB Output is correct
6 Correct 3 ms 3876 KB Output is correct
7 Correct 3 ms 3788 KB Output is correct
8 Correct 3 ms 3916 KB Output is correct
9 Correct 5 ms 4172 KB Output is correct
10 Correct 2 ms 3788 KB Output is correct
11 Correct 12 ms 5196 KB Output is correct
12 Correct 26 ms 6640 KB Output is correct
13 Correct 37 ms 12120 KB Output is correct
14 Correct 102 ms 12040 KB Output is correct
15 Correct 123 ms 12388 KB Output is correct
16 Correct 80 ms 10816 KB Output is correct
17 Correct 70 ms 10488 KB Output is correct
18 Correct 27 ms 7140 KB Output is correct
19 Correct 79 ms 13696 KB Output is correct
20 Correct 94 ms 14092 KB Output is correct
21 Correct 91 ms 12364 KB Output is correct
22 Correct 72 ms 11976 KB Output is correct
23 Correct 86 ms 14624 KB Output is correct
24 Correct 3 ms 3916 KB Output is correct
25 Correct 13 ms 5452 KB Output is correct
26 Correct 27 ms 7292 KB Output is correct
27 Correct 39 ms 13124 KB Output is correct
28 Correct 117 ms 13764 KB Output is correct
29 Correct 98 ms 14104 KB Output is correct
30 Correct 72 ms 12408 KB Output is correct
31 Correct 66 ms 12120 KB Output is correct
32 Correct 30 ms 7140 KB Output is correct
33 Correct 87 ms 13832 KB Output is correct
34 Correct 98 ms 14080 KB Output is correct
35 Correct 83 ms 12464 KB Output is correct
36 Correct 72 ms 12080 KB Output is correct
37 Correct 82 ms 14604 KB Output is correct
38 Correct 111 ms 17448 KB Output is correct