답안 #1107672

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1107672 2024-11-01T22:09:07 Z laight 열대 식물원 (Tropical Garden) (IOI11_garden) C++17
100 / 100
2012 ms 30104 KB
#include "garden.h"
#include "gardenlib.h"
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pi pair<int, int>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
#define umi unordered_map<int, int>
#define umll unordered_map<ll, ll>
#define umllv unordered_map<ll, vector<ll>>
 
#define getchar() (*_pinp?*_pinp++:(_inp[fread(_pinp=_inp, 1, 4096, stdin)]='\0', *_pinp++))
char _inp[4097], *_pinp=_inp;
#define scan(x) do{while((x=getchar())<'-'); _ssign=x=='-'; if(_ssign) while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0'); x=_ssign?-x:x;}while(0)
char _; bool _ssign;
 
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx2")
 
using namespace std; using namespace __gnu_pbds;
 
const int MM = 1e5+5e4+5;
const ll inf = 2e15+1;
const int lg = 17; // 17 for 1e5, 31 for 1e9
 
int n, m, p, a, b, dis[2*MM], lv, cycl, q, inp; bool cyc[2*MM], vis[2*MM];
vector<int> rev[2*MM], g(2005), ans(2005), to(2*MM); vector<pi> graph[MM];
 
void g_f(int par, int c, int d){
    dis[c] = d;
    for (int i : rev[c]){
        if (i == par || cyc[i]) continue;
        g_f(c, i, d+1);
    }
}
void cyc_f(int c){
    vis[c] = 1;
    if (vis[to[c]]){
        int st = to[c];
        while (st != c){
            cyc[st] = 1; st = to[st];
            ++lv;
        }
        cyc[c] = 1;
        ++lv;
        return;
    }
    cyc_f(to[c]);
}
void sol(int sp){
    memset(dis, -1, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    memset(cyc, 0, sizeof(cyc));
    lv=0;
    bool incyc=0;
    cyc_f(sp);
    cycl = lv;
    if (cyc[sp]){
        incyc=1;
        g_f(-1, sp, 0);
        int nxt=to[sp];
        while (nxt != sp){
            g_f(-1, nxt, --lv);
            nxt = to[nxt];
        }
    }
    else{
        cyc[to[sp]] = 1;
        g_f(-1, sp, 0);
    }
    for (int i=0; i<q; ++i){
        for (int j=0; j<n; ++j){
            if (dis[j] == -1) continue;
            if (incyc){
                if (g[i] >= dis[j] && (g[i]%cycl == dis[j]%cycl)){
                    ++ans[i];
                }
            }
            else{
                if (g[i] == dis[j]) ++ans[i];
            }
        }
    }
}
void count_routes(int N, int M, int P, int R[][2], int Q, int G[]){
    // cin.tie(0); cin.sync_with_stdio(0);
    n = N; m = M; p = P;
    for (int i=0; i<m; ++i){
        graph[R[i][0]].pb({i+1, R[i][1]}); graph[R[i][1]].pb({i+1, R[i][0]});
    }
    q = Q; for (int i=0; i<q; i++) {g[i] = G[i];}
    for (int i=0; i<n; ++i){
        if (graph[i][0].fi == graph[graph[i][0].se][0].fi){
            to[i] = n+graph[i][0].se; rev[n+graph[i][0].se].pb(i);
        }
        else {to[i] = graph[i][0].se; rev[graph[i][0].se].pb(i);}
        if (graph[i].size() >= 2){
            if (graph[i][1].fi == graph[graph[i][1].se][0].fi){
                to[n+i] = n+graph[i][1].se; rev[n+graph[i][1].se].pb(n+i);
            }
            else {to[n+i] = graph[i][1].se; rev[graph[i][1].se].pb(n+i);}
        }
        else{
            if (graph[i][0].fi == graph[graph[i][0].se][0].fi){
                to[n+i] = n+graph[i][0].se; rev[n+graph[i][0].se].pb(n+i);
            }
            else {to[n+i] = graph[i][0].se; rev[graph[i][0].se].pb(n+i);}
        }
    }
    sol(p); sol(n+p);
    for (int i=0; i<q; ++i){
        answer(ans[i]);
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14160 KB Output is correct
2 Correct 3 ms 14160 KB Output is correct
3 Correct 4 ms 14160 KB Output is correct
4 Correct 3 ms 13904 KB Output is correct
5 Correct 3 ms 13904 KB Output is correct
6 Correct 3 ms 14276 KB Output is correct
7 Correct 3 ms 13904 KB Output is correct
8 Correct 3 ms 14160 KB Output is correct
9 Correct 5 ms 14160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14160 KB Output is correct
2 Correct 3 ms 14160 KB Output is correct
3 Correct 4 ms 14160 KB Output is correct
4 Correct 3 ms 13904 KB Output is correct
5 Correct 3 ms 13904 KB Output is correct
6 Correct 3 ms 14276 KB Output is correct
7 Correct 3 ms 13904 KB Output is correct
8 Correct 3 ms 14160 KB Output is correct
9 Correct 5 ms 14160 KB Output is correct
10 Correct 3 ms 13904 KB Output is correct
11 Correct 10 ms 17736 KB Output is correct
12 Correct 19 ms 19024 KB Output is correct
13 Correct 28 ms 24312 KB Output is correct
14 Correct 53 ms 25672 KB Output is correct
15 Correct 58 ms 25928 KB Output is correct
16 Correct 55 ms 23588 KB Output is correct
17 Correct 50 ms 22856 KB Output is correct
18 Correct 20 ms 19024 KB Output is correct
19 Correct 63 ms 25812 KB Output is correct
20 Correct 68 ms 25912 KB Output is correct
21 Correct 52 ms 23736 KB Output is correct
22 Correct 53 ms 22856 KB Output is correct
23 Correct 64 ms 26696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14160 KB Output is correct
2 Correct 3 ms 14160 KB Output is correct
3 Correct 4 ms 14160 KB Output is correct
4 Correct 3 ms 13904 KB Output is correct
5 Correct 3 ms 13904 KB Output is correct
6 Correct 3 ms 14276 KB Output is correct
7 Correct 3 ms 13904 KB Output is correct
8 Correct 3 ms 14160 KB Output is correct
9 Correct 5 ms 14160 KB Output is correct
10 Correct 3 ms 13904 KB Output is correct
11 Correct 10 ms 17736 KB Output is correct
12 Correct 19 ms 19024 KB Output is correct
13 Correct 28 ms 24312 KB Output is correct
14 Correct 53 ms 25672 KB Output is correct
15 Correct 58 ms 25928 KB Output is correct
16 Correct 55 ms 23588 KB Output is correct
17 Correct 50 ms 22856 KB Output is correct
18 Correct 20 ms 19024 KB Output is correct
19 Correct 63 ms 25812 KB Output is correct
20 Correct 68 ms 25912 KB Output is correct
21 Correct 52 ms 23736 KB Output is correct
22 Correct 53 ms 22856 KB Output is correct
23 Correct 64 ms 26696 KB Output is correct
24 Correct 4 ms 13904 KB Output is correct
25 Correct 54 ms 17916 KB Output is correct
26 Correct 82 ms 19024 KB Output is correct
27 Correct 1356 ms 24176 KB Output is correct
28 Correct 671 ms 25672 KB Output is correct
29 Correct 1436 ms 26084 KB Output is correct
30 Correct 820 ms 23760 KB Output is correct
31 Correct 820 ms 23084 KB Output is correct
32 Correct 106 ms 19024 KB Output is correct
33 Correct 689 ms 25636 KB Output is correct
34 Correct 1384 ms 26064 KB Output is correct
35 Correct 834 ms 23624 KB Output is correct
36 Correct 1225 ms 22856 KB Output is correct
37 Correct 479 ms 26736 KB Output is correct
38 Correct 2012 ms 30104 KB Output is correct