Submission #316941

# Submission time Handle Problem Language Result Execution time Memory
316941 2020-10-28T16:06:35 Z knon0501 Tropical Garden (IOI11_garden) C++14
100 / 100
2905 ms 32912 KB
#include "garden.h"
#include "gardenlib.h"
#include <bits/stdc++.h>
using namespace std;
const int MX=3e5+5;
pair<int,int> e[MX];
int b[MX];
int c[MX];
int nxt[MX];
pair<int,int> d[MX];
int visit[MX];
int ans[MX];
int p;
vector<int> dd[MX];
pair<int,int> f(int k){
    if(d[k].first>=0)return d[k];
    int u=e[k].first;
    int v=e[k].second;
    if(v==p)return {1,k};

    auto ret=f(nxt[k]);
    return d[k]={ret.first+1,ret.second};
}
void dfs(int v){
    visit[v]=1;
    for(auto u: dd[v]){
        if(!visit[u])dfs(u);
    }
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[])
{
    p=P;
    int i,j;
    for(i=0 ; i<N ; i++)b[i]=c[i]=-1;
    for(i=0 ; i<M ; i++){
        int u=R[i][0];
        int v=R[i][1];
        e[i]={u,v};
        e[M+i]={v,u};
        if(b[u]==-1)b[u]=i;
        else if(c[u]==-1)c[u]=i;
        if(b[v]==-1)b[v]=M+i;
        else if(c[v]==-1)c[v]=M+i;
    }

    for(i=0 ; i<M*2 ; i++){
        int u=e[i].first;
        int v=e[i].second;
        int k=b[v];
        int t=c[v];
        if(t==-1){
            nxt[i]=k;
            dd[k].push_back(i);
            continue;
        }
        if(e[k].second==u){
            nxt[i]=t;
            dd[t].push_back(i);
        }
        else {
            nxt[i]=k;
            dd[k].push_back(i);
        }
    }

    for(i=0 ; i<M*2 ; i++){
        if(e[i].second==P)dfs(i);
    }
    for(i=0 ; i<M*2 ;i++)d[i]={-1,-1};
    for(i=0  ;i<M*2 ; i++){
      ///  cout<<e[i].first<<" "<<e[i].second<<" "<<visit[i]<<endl;
        if(visit[i]==1)d[i]=f(i);
    }

    for(i=0 ; i<N ; i++){
        int k=b[i];
        int w=-1,e=-1,r=-1;
        if(k>=0 && visit[k])
            w=nxt[d[k].second];
        if(w>=0 && visit[w])
            e=nxt[d[w].second];
        if(e>=0 && visit[e])
            r=nxt[d[e].second];

        if(w==-1)continue;
        for(j=0 ; j<Q ; j++){

            if(G[j]<d[k].first)continue;
            if(G[j]==d[k].first){
                ans[j]++;
                continue;
            }
            if(w==e && e>=0){
                if((G[j]-d[k].first)%d[w].first==0)ans[j]++;
            }
            else{
                if(e<0 )continue;
                if(G[j]<d[k].first+d[w].first)continue;
                if(G[j]==d[k].first+d[w].first){
                    ans[j]++;
                    continue;
                }
                if(r<0)continue;
                if(e==r){
                    if((G[j]-d[k].first-d[w].first)%d[e].first==0)ans[j]++;
                }
                else{
                    if((G[j]-d[k].first)%(d[w].first+d[e].first)==0 || (G[j]-d[k].first)%(d[w].first+d[e].first)==d[w].first)ans[j]++;
                }
            }
        }
    }
    for(i=0 ; i<Q ; i++)
        answer(ans[i]);
}


Compilation message

garden.cpp: In function 'std::pair<int, int> f(int)':
garden.cpp:17:9: warning: unused variable 'u' [-Wunused-variable]
   17 |     int u=e[k].first;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7552 KB Output is correct
2 Correct 6 ms 7552 KB Output is correct
3 Correct 6 ms 7552 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 5 ms 7424 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 6 ms 7552 KB Output is correct
9 Correct 10 ms 8064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7552 KB Output is correct
2 Correct 6 ms 7552 KB Output is correct
3 Correct 6 ms 7552 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 5 ms 7424 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 6 ms 7552 KB Output is correct
9 Correct 10 ms 8064 KB Output is correct
10 Correct 5 ms 7424 KB Output is correct
11 Correct 16 ms 10240 KB Output is correct
12 Correct 33 ms 13312 KB Output is correct
13 Correct 62 ms 24440 KB Output is correct
14 Correct 103 ms 23932 KB Output is correct
15 Correct 155 ms 24444 KB Output is correct
16 Correct 130 ms 22064 KB Output is correct
17 Correct 129 ms 21240 KB Output is correct
18 Correct 35 ms 13440 KB Output is correct
19 Correct 99 ms 24568 KB Output is correct
20 Correct 150 ms 25168 KB Output is correct
21 Correct 144 ms 22464 KB Output is correct
22 Correct 151 ms 21784 KB Output is correct
23 Correct 95 ms 25592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 7552 KB Output is correct
2 Correct 6 ms 7552 KB Output is correct
3 Correct 6 ms 7552 KB Output is correct
4 Correct 5 ms 7424 KB Output is correct
5 Correct 5 ms 7424 KB Output is correct
6 Correct 6 ms 7680 KB Output is correct
7 Correct 5 ms 7424 KB Output is correct
8 Correct 6 ms 7552 KB Output is correct
9 Correct 10 ms 8064 KB Output is correct
10 Correct 5 ms 7424 KB Output is correct
11 Correct 16 ms 10240 KB Output is correct
12 Correct 33 ms 13312 KB Output is correct
13 Correct 62 ms 24440 KB Output is correct
14 Correct 103 ms 23932 KB Output is correct
15 Correct 155 ms 24444 KB Output is correct
16 Correct 130 ms 22064 KB Output is correct
17 Correct 129 ms 21240 KB Output is correct
18 Correct 35 ms 13440 KB Output is correct
19 Correct 99 ms 24568 KB Output is correct
20 Correct 150 ms 25168 KB Output is correct
21 Correct 144 ms 22464 KB Output is correct
22 Correct 151 ms 21784 KB Output is correct
23 Correct 95 ms 25592 KB Output is correct
24 Correct 6 ms 7424 KB Output is correct
25 Correct 45 ms 10264 KB Output is correct
26 Correct 48 ms 13304 KB Output is correct
27 Correct 1285 ms 24472 KB Output is correct
28 Correct 587 ms 24696 KB Output is correct
29 Correct 2890 ms 25292 KB Output is correct
30 Correct 1644 ms 22904 KB Output is correct
31 Correct 1663 ms 21928 KB Output is correct
32 Correct 40 ms 13308 KB Output is correct
33 Correct 616 ms 24760 KB Output is correct
34 Correct 2905 ms 25340 KB Output is correct
35 Correct 1783 ms 22580 KB Output is correct
36 Correct 1695 ms 21880 KB Output is correct
37 Correct 332 ms 25720 KB Output is correct
38 Correct 2312 ms 32912 KB Output is correct