Submission #432981

# Submission time Handle Problem Language Result Execution time Memory
432981 2021-06-18T16:52:33 Z JeanBombeur Tropical Garden (IOI11_garden) C++17
100 / 100
138 ms 17436 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 dest2, dest;
    dest2 = dest = Voisins[noeud][0];
    if (Voisins[noeud].size() > 1)
        dest2 = Voisins[noeud][1];
    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;
    int newCur = 0;
    for (int i = 0; i < nbRequetes; i ++)
    {
        while (cur <= min(maxi, Requetes[Ans[i].second]))
        {
            if (cur >= tailleCycle)
                Occ[newCur] += Occ[cur];
            cur ++;
            if (++ newCur == tailleCycle)
                newCur = 0;
        }
        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;
        newCur = 0;
        for (int i = 0; i < nbRequetes; i ++)
        {
            while (cur <= min(maxi, Requetes[Ans[i].second]))
            {
                if (cur >= tailleCycle)
                    Occ[newCur] += Occ[cur];
                cur ++;
                if (++ newCur == tailleCycle)
                    newCur = 0;
            }
            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 3916 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 3916 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 6692 KB Output is correct
13 Correct 38 ms 12164 KB Output is correct
14 Correct 93 ms 12036 KB Output is correct
15 Correct 83 ms 12356 KB Output is correct
16 Correct 72 ms 10864 KB Output is correct
17 Correct 67 ms 10464 KB Output is correct
18 Correct 27 ms 7172 KB Output is correct
19 Correct 78 ms 13804 KB Output is correct
20 Correct 96 ms 14040 KB Output is correct
21 Correct 84 ms 12400 KB Output is correct
22 Correct 71 ms 11972 KB Output is correct
23 Correct 84 ms 14752 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 3916 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 6692 KB Output is correct
13 Correct 38 ms 12164 KB Output is correct
14 Correct 93 ms 12036 KB Output is correct
15 Correct 83 ms 12356 KB Output is correct
16 Correct 72 ms 10864 KB Output is correct
17 Correct 67 ms 10464 KB Output is correct
18 Correct 27 ms 7172 KB Output is correct
19 Correct 78 ms 13804 KB Output is correct
20 Correct 96 ms 14040 KB Output is correct
21 Correct 84 ms 12400 KB Output is correct
22 Correct 71 ms 11972 KB Output is correct
23 Correct 84 ms 14752 KB Output is correct
24 Correct 3 ms 3788 KB Output is correct
25 Correct 15 ms 5528 KB Output is correct
26 Correct 27 ms 7224 KB Output is correct
27 Correct 37 ms 13148 KB Output is correct
28 Correct 91 ms 13816 KB Output is correct
29 Correct 82 ms 14112 KB Output is correct
30 Correct 74 ms 12500 KB Output is correct
31 Correct 96 ms 12080 KB Output is correct
32 Correct 29 ms 7212 KB Output is correct
33 Correct 84 ms 13816 KB Output is correct
34 Correct 91 ms 14148 KB Output is correct
35 Correct 79 ms 12420 KB Output is correct
36 Correct 73 ms 12040 KB Output is correct
37 Correct 120 ms 14660 KB Output is correct
38 Correct 138 ms 17436 KB Output is correct