Submission #104647

# Submission time Handle Problem Language Result Execution time Memory
104647 2019-04-08T13:46:38 Z alexpetrescu File Paths (BOI15_fil) C++14
100 / 100
99 ms 1044 KB
#include <cstdio>
#include <algorithm>
#include <vector>

//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");
#define fin stdin
#define fout stdout

#define MAXN 3009

char sir[2][5] = {
    {
        'N', 'O', '\n', '\0', '\0'
    },
    {
        'Y', 'E', 'S', '\n', '\0'
    }
};

int n, k, l, vf, cnt;
bool ans[MAXN];
int G[MAXN], D[MAXN]; /// gauche, droite
int st[MAXN], r[MAXN], h[MAXN], v[MAXN];
std::vector < int > g[MAXN], e[MAXN];

bool cmp(const int &a, const int &b) {
    return h[a] < h[b];
}

inline bool check0(int x) { /// nu folosesc niciun symlink
    return k == (h[st[vf]] + x);
}

inline bool check1(int x) { /// folosesc un singur symlink
    /// conditia e sa existe i, j a.i. : k = l + x + h[st[vf]] - h[st[i]] + h[r[j]], 0 < i <= vf, 0 <= j <= n
    /// h[r[j]] = h[st[i]] + k - l - x - h[st[vf]]
    int j = 0;
    for (int i = 1; i <= vf; i++) {
        while (j <= n && h[r[j]] < h[st[i]] + k - l - x - h[st[vf]])
            j++;
        if (j <= n && h[r[j]] == h[st[i]] + k - l - x - h[st[vf]])
            return 1;
    }
    return 0;
}

inline bool stramos(int x, int y) { /// este x stramos al lui y?
    return G[x] <= G[y] && D[y] <= D[x];
}

inline bool solve(int x) {
    /// 0 <= i <= n, 0 < j <= vf, r[i] e in subarborele lui st[j], a.i. : x = l + h[r[i]] - h[st[j]]
    /// h[st[j]] = h[r[i]] + l - x
    int j = 1;
    for (int i = 0; i <= n; i++) {
        while (j <= vf && h[st[j]] < h[r[i]] + l - x)
            j++;
        if (j <= vf && h[st[j]] == h[r[i]] + l - x && stramos(st[j], r[i]))
            return 1;
    }
    return 0;
}

inline bool check2(int x) { /// folosesc mai multe symlinkuri
    /// conditia e sa existe i, j a.i. : (l + h[r[i]] - h[st[j]]) divide (k - x - h[st[vf]]), 0 <= i <= n, 0 < j <= vf, r[i] e in subarborele lui st[j]
    int q = k - x - h[st[vf]];
    for (int i = 1; i * i <= q; i++)
        if (q % i == 0 && (solve(i) || solve(q / i)))
            return 1;
    return 0;
}

void dfs1(int x) {
    G[x] = ++cnt;
    for (auto &y : g[x]) {
        h[y] += h[x];
        dfs1(y);
    }
    D[x] = cnt;
}

void dfs2(int x) {
    st[++vf] = x;
    for (auto &y : g[x])
        dfs2(y);
    for (auto &y : e[x])
        ans[y] = (check0(v[y]) || check1(v[y]) || check2(v[y]));
    vf--;
}

int main() {
    int m;
    fscanf(fin, "%d%d%d%d", &n, &m, &k, &l);
    l++;

    for (int i = 1; i <= n; i++) {
        int x;
        fscanf(fin, "%d%d", &x, &h[i]);
        h[i]++;

        g[x].push_back(i);
    }

    for (int i = 0; i < m; i++) {
        int x;
        fscanf(fin, "%d%d", &x, &v[i]);
        v[i]++;

        e[x].push_back(i);
    }

    dfs1(0);

    for (int i = 0; i <= n; i++)
        r[i] = i;
    std::sort(r, r + n + 1, cmp);

    dfs2(0);

    for (int i = 0; i < m; i++)
        fputs(sir[ans[i]], fout);

    fclose(fin);
    fclose(fout);
    return 0;
}

Compilation message

fil.cpp: In function 'int main()':
fil.cpp:93:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d%d%d%d", &n, &m, &k, &l);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fil.cpp:98:15: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf(fin, "%d%d", &x, &h[i]);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fil.cpp:106:15: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         fscanf(fin, "%d%d", &x, &v[i]);
         ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 3 ms 508 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 7 ms 512 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 7 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 99 ms 676 KB Output is correct
2 Correct 93 ms 676 KB Output is correct
3 Correct 92 ms 640 KB Output is correct
4 Correct 88 ms 760 KB Output is correct
5 Correct 69 ms 896 KB Output is correct
6 Correct 44 ms 896 KB Output is correct
7 Correct 84 ms 856 KB Output is correct
8 Correct 64 ms 768 KB Output is correct
9 Correct 88 ms 684 KB Output is correct
10 Correct 72 ms 760 KB Output is correct
11 Correct 5 ms 640 KB Output is correct
12 Correct 46 ms 896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 512 KB Output is correct
2 Correct 3 ms 508 KB Output is correct
3 Correct 5 ms 512 KB Output is correct
4 Correct 7 ms 512 KB Output is correct
5 Correct 6 ms 512 KB Output is correct
6 Correct 5 ms 512 KB Output is correct
7 Correct 7 ms 512 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 3 ms 512 KB Output is correct
11 Correct 3 ms 504 KB Output is correct
12 Correct 4 ms 512 KB Output is correct
13 Correct 99 ms 676 KB Output is correct
14 Correct 93 ms 676 KB Output is correct
15 Correct 92 ms 640 KB Output is correct
16 Correct 88 ms 760 KB Output is correct
17 Correct 69 ms 896 KB Output is correct
18 Correct 44 ms 896 KB Output is correct
19 Correct 84 ms 856 KB Output is correct
20 Correct 64 ms 768 KB Output is correct
21 Correct 88 ms 684 KB Output is correct
22 Correct 72 ms 760 KB Output is correct
23 Correct 5 ms 640 KB Output is correct
24 Correct 46 ms 896 KB Output is correct
25 Correct 81 ms 760 KB Output is correct
26 Correct 64 ms 640 KB Output is correct
27 Correct 63 ms 760 KB Output is correct
28 Correct 87 ms 760 KB Output is correct
29 Correct 74 ms 896 KB Output is correct
30 Correct 63 ms 988 KB Output is correct
31 Correct 97 ms 864 KB Output is correct
32 Correct 87 ms 896 KB Output is correct
33 Correct 63 ms 732 KB Output is correct
34 Correct 56 ms 640 KB Output is correct
35 Correct 8 ms 768 KB Output is correct
36 Correct 77 ms 1044 KB Output is correct