Submission #594431

# Submission time Handle Problem Language Result Execution time Memory
594431 2022-07-12T12:37:12 Z Bench0310 Tropical Garden (IOI11_garden) C++17
100 / 100
3534 ms 33720 KB
#include <bits/stdc++.h>
#include "garden.h"
#include "gardenlib.h"

using namespace std;
typedef long long ll;

const int N=150000;
const int M=150000;
int p;
array<int,2> opt[N];
int endpoint[2*M];
int to[2*M];
array<int,2> precycle[2*M];
array<int,2> incycle[2*M];
bool partcycle[2*M];
int cyclelen[2*M];
int st[2*M];
vector<int> now;

int one(array<int,2> &a)
{
    int t=0;
    while(t<2&&a[t]!=-1) t++;
    return t;
}

void dfs(int a)
{
    int b=to[a];
    st[a]=1;
    now.push_back(a);
    if(st[b]==1)
    {
        int sz=now.size();
        int pos=sz-1;
        while(now[pos]!=b) pos--;
        int len=sz-pos;
        vector<int> v;
        for(int i=pos;i<sz;i++)
        {
            partcycle[now[i]]=1;
            cyclelen[now[i]]=len;
            if(endpoint[now[i]]==p) v.push_back(i);
        }
        assert(v.size()<=2);
        for(int i=pos;i<sz;i++)
        {
            for(int x:v)
            {
                int t=(((x-i)%len)+len)%len;
                incycle[now[i]][one(incycle[now[i]])]=t;
            }
        }
    }
    else
    {
        if(st[b]==0) dfs(b);
        if(!partcycle[a])
        {
            cyclelen[a]=cyclelen[b];
            for(int i=0;i<2;i++) if(incycle[b][i]!=-1) incycle[a][i]=incycle[b][i]+1;
            for(int i=0;i<2;i++) if(precycle[b][i]!=-1) precycle[a][i]=precycle[b][i]+1;
        }
        if(!partcycle[a]&&endpoint[a]==p) precycle[a][one(precycle[a])]=0;
    }
    st[a]=2;
    now.pop_back();
}

void count_routes(int n,int m,int tp,int r[][2],int q,int g[])
{
    for(int i=0;i<n;i++) opt[i]={-1,-1};
    for(int i=0;i<2*m;i++)
    {
        precycle[i]=incycle[i]={-1,-1};
        cyclelen[i]=-1;
    }
    auto add_edge=[&](int x,int id)
    {
        int t=one(opt[x]);
        if(t<2) opt[x][t]=id;
    };
    for(int i=0;i<m;i++)
    {
        int a=r[i][0],b=r[i][1];
        endpoint[2*i]=b;
        endpoint[2*i+1]=a;
        add_edge(a,2*i);
        add_edge(b,2*i+1);
    }
    for(int i=0;i<2*m;i++)
    {
        int x=endpoint[i];
        to[i]=opt[x][(opt[x][0]/2==i/2&&opt[x][1]!=-1)];
    }
    p=tp;
    for(int i=0;i<2*m;i++) if(st[i]==0) dfs(i);
    for(int i=0;i<q;i++)
    {
        int k=g[i]-1;
        int res=0;
        for(int j=0;j<n;j++)
        {
            bool ok=0;
            int a=opt[j][0];
            for(int t=0;t<2;t++)
            {
                ok|=(precycle[a][t]==k);
                if(incycle[a][t]!=-1) ok|=(k>=incycle[a][t]&&((k-incycle[a][t])%cyclelen[a])==0);
            }
            res+=ok;
        }
        answer(res);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 6 ms 3004 KB Output is correct
12 Correct 15 ms 5588 KB Output is correct
13 Correct 39 ms 30272 KB Output is correct
14 Correct 50 ms 13024 KB Output is correct
15 Correct 50 ms 13492 KB Output is correct
16 Correct 43 ms 12620 KB Output is correct
17 Correct 42 ms 11980 KB Output is correct
18 Correct 16 ms 4984 KB Output is correct
19 Correct 45 ms 13008 KB Output is correct
20 Correct 46 ms 13564 KB Output is correct
21 Correct 49 ms 12364 KB Output is correct
22 Correct 60 ms 12128 KB Output is correct
23 Correct 49 ms 14400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 724 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 980 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 6 ms 3004 KB Output is correct
12 Correct 15 ms 5588 KB Output is correct
13 Correct 39 ms 30272 KB Output is correct
14 Correct 50 ms 13024 KB Output is correct
15 Correct 50 ms 13492 KB Output is correct
16 Correct 43 ms 12620 KB Output is correct
17 Correct 42 ms 11980 KB Output is correct
18 Correct 16 ms 4984 KB Output is correct
19 Correct 45 ms 13008 KB Output is correct
20 Correct 46 ms 13564 KB Output is correct
21 Correct 49 ms 12364 KB Output is correct
22 Correct 60 ms 12128 KB Output is correct
23 Correct 49 ms 14400 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 122 ms 3216 KB Output is correct
26 Correct 196 ms 5620 KB Output is correct
27 Correct 2245 ms 30316 KB Output is correct
28 Correct 1100 ms 13252 KB Output is correct
29 Correct 2423 ms 13632 KB Output is correct
30 Correct 1433 ms 12668 KB Output is correct
31 Correct 1376 ms 12136 KB Output is correct
32 Correct 215 ms 5068 KB Output is correct
33 Correct 1126 ms 13128 KB Output is correct
34 Correct 2390 ms 13640 KB Output is correct
35 Correct 1531 ms 12564 KB Output is correct
36 Correct 1416 ms 12164 KB Output is correct
37 Correct 952 ms 14640 KB Output is correct
38 Correct 3534 ms 33720 KB Output is correct