답안 #83512

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
83512 2018-11-08T14:52:28 Z radoslav11 늑대인간 (IOI18_werewolf) C++14
100 / 100
1203 ms 157044 KB
/*
    We build a prefix and suffix tree with dsu, by incrementing them by 1 vertex at each step. Now queries become:
    Do two subtrees have a common vertex (the first subtree is from the prefix tree and the second subtree is from the suffix tree).
    This can be done with DFS order + dsu on tree by keeping a set for each vertex. The complexity will be O(N log^2 N + Q log N).
*/

#include <bits/stdc++.h>
#include "werewolf.h"
//#include "grader.cpp"

#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()

using namespace std;
template<class T, class T1> int chkmax(T &x, const T1 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T1> int chkmin(T &x, const T1 &y) { return x > y ? x = y, 1 : 0; }
const int MAXN = (1 << 18);

int tp[MAXN], sz[MAXN], par[MAXN];

void init(int n)
{
    for(int i = 0; i <= n; i++)
        par[i] = i, sz[i] = 1, tp[i] = i;
}

int root(int x) { return x == par[x] ? x : (par[x] = root(par[x])); }

bool connected(int u, int v)
{
    return root(u) == root(v);
}

void unite(int u, int v)
{
    u = root(u), v = root(v);
    if(u == v) return;
    if(sz[u] > sz[v]) swap(u, v);
    par[u] = v;
    sz[v] += sz[u];
}

struct Tree
{
    vector<int> adj[MAXN];
    int st[MAXN], en[MAXN], dfs_time;

    void add_edge(int u, int v)
    {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    void pre_dfs(int u, int pr)
    {
        st[u] = ++dfs_time;
        for(int v: adj[u])
            if(v != pr)
                pre_dfs(v, u);
        en[u] = dfs_time;
    }

    void order(int root)
    {
        dfs_time = 0;
        pre_dfs(root, root);
    }
} t1, t2;

vector<int> adj[MAXN];
vector<int> pref[MAXN], suff[MAXN];
vector<int> ans;
int vert1[MAXN], vert2[MAXN];

set<int> st[MAXN];
vector<int> que[MAXN];

void solve(int u, int pr = -1)
{
    st[u].insert(t1.st[u]);
    for(int v: t2.adj[u])
        if(v != pr)
        {
            solve(v, u);
            if(st[u].size() < st[v].size()) swap(st[u], st[v]);

            for(int x: st[v])
                st[u].insert(x);

            st[v].clear();
        }

    for(int i: que[u])
    {
        int L = t1.st[vert1[i]], R = t1.en[vert1[i]];

        auto it = st[u].lower_bound(L);
        if(it != st[u].end() && *it <= R)
            ans[i] = 1;
    }
}

std::vector<int> check_validity(int n, std::vector<int> X, std::vector<int> Y, std::vector<int> S, std::vector<int> E, std::vector<int> L, std::vector<int> R)
{
    int q = S.size(), m = X.size();
    ans.assign(q, 0);

    for(int i = 0; i < m; i++)
    {
        int u = X[i], v = Y[i];
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for(int i = 0; i < q; i++)
    {
        suff[L[i]].push_back(i);
        pref[R[i]].push_back(i);
    }

    init(n);
    for(int i = 0; i < n; i++)
    {
        for(int v: adj[i])
            if(i > v)
                if(!connected(i, v))
                {
                    t1.add_edge(tp[root(i)], tp[root(v)]);
                    unite(i, v);
                    tp[root(i)] = i;
                }

        for(int it: pref[i])
            vert1[it] = tp[root(E[it])];
    }

    int r1 = tp[root(0)];
    t1.order(r1);

    init(n);
    for(int i = n - 1; i >= 0; i--)
    {
        for(int v: adj[i])
            if(i < v)
                if(!connected(i, v))
                {
                    t2.add_edge(tp[root(i)], tp[root(v)]);
                    unite(i, v);
                    tp[root(i)] = i;
                }

        for(int it: suff[i])
            vert2[it] = tp[root(S[it])];
    }

    int r2 = tp[root(0)];
    t2.order(r2);

    for(int i = 0; i < q; i++)
        que[vert2[i]].push_back(i);

    solve(r2);

    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 49656 KB Output is correct
2 Correct 50 ms 49660 KB Output is correct
3 Correct 50 ms 49756 KB Output is correct
4 Correct 48 ms 49912 KB Output is correct
5 Correct 48 ms 49912 KB Output is correct
6 Correct 49 ms 49912 KB Output is correct
7 Correct 52 ms 49912 KB Output is correct
8 Correct 49 ms 49912 KB Output is correct
9 Correct 47 ms 49912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 49656 KB Output is correct
2 Correct 50 ms 49660 KB Output is correct
3 Correct 50 ms 49756 KB Output is correct
4 Correct 48 ms 49912 KB Output is correct
5 Correct 48 ms 49912 KB Output is correct
6 Correct 49 ms 49912 KB Output is correct
7 Correct 52 ms 49912 KB Output is correct
8 Correct 49 ms 49912 KB Output is correct
9 Correct 47 ms 49912 KB Output is correct
10 Correct 58 ms 50968 KB Output is correct
11 Correct 63 ms 51116 KB Output is correct
12 Correct 59 ms 51116 KB Output is correct
13 Correct 57 ms 51316 KB Output is correct
14 Correct 58 ms 51552 KB Output is correct
15 Correct 57 ms 51560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1203 ms 112152 KB Output is correct
2 Correct 870 ms 112152 KB Output is correct
3 Correct 862 ms 112152 KB Output is correct
4 Correct 926 ms 112152 KB Output is correct
5 Correct 980 ms 112152 KB Output is correct
6 Correct 985 ms 112152 KB Output is correct
7 Correct 992 ms 112152 KB Output is correct
8 Correct 861 ms 112152 KB Output is correct
9 Correct 816 ms 112152 KB Output is correct
10 Correct 854 ms 112152 KB Output is correct
11 Correct 867 ms 112152 KB Output is correct
12 Correct 1116 ms 112152 KB Output is correct
13 Correct 830 ms 124484 KB Output is correct
14 Correct 861 ms 124484 KB Output is correct
15 Correct 853 ms 124488 KB Output is correct
16 Correct 876 ms 124516 KB Output is correct
17 Correct 985 ms 124516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 49656 KB Output is correct
2 Correct 50 ms 49660 KB Output is correct
3 Correct 50 ms 49756 KB Output is correct
4 Correct 48 ms 49912 KB Output is correct
5 Correct 48 ms 49912 KB Output is correct
6 Correct 49 ms 49912 KB Output is correct
7 Correct 52 ms 49912 KB Output is correct
8 Correct 49 ms 49912 KB Output is correct
9 Correct 47 ms 49912 KB Output is correct
10 Correct 58 ms 50968 KB Output is correct
11 Correct 63 ms 51116 KB Output is correct
12 Correct 59 ms 51116 KB Output is correct
13 Correct 57 ms 51316 KB Output is correct
14 Correct 58 ms 51552 KB Output is correct
15 Correct 57 ms 51560 KB Output is correct
16 Correct 1203 ms 112152 KB Output is correct
17 Correct 870 ms 112152 KB Output is correct
18 Correct 862 ms 112152 KB Output is correct
19 Correct 926 ms 112152 KB Output is correct
20 Correct 980 ms 112152 KB Output is correct
21 Correct 985 ms 112152 KB Output is correct
22 Correct 992 ms 112152 KB Output is correct
23 Correct 861 ms 112152 KB Output is correct
24 Correct 816 ms 112152 KB Output is correct
25 Correct 854 ms 112152 KB Output is correct
26 Correct 867 ms 112152 KB Output is correct
27 Correct 1116 ms 112152 KB Output is correct
28 Correct 830 ms 124484 KB Output is correct
29 Correct 861 ms 124484 KB Output is correct
30 Correct 853 ms 124488 KB Output is correct
31 Correct 876 ms 124516 KB Output is correct
32 Correct 985 ms 124516 KB Output is correct
33 Correct 1144 ms 124516 KB Output is correct
34 Correct 403 ms 124516 KB Output is correct
35 Correct 1187 ms 124516 KB Output is correct
36 Correct 1100 ms 124516 KB Output is correct
37 Correct 1085 ms 124516 KB Output is correct
38 Correct 1092 ms 124516 KB Output is correct
39 Correct 1139 ms 124516 KB Output is correct
40 Correct 1086 ms 124516 KB Output is correct
41 Correct 1103 ms 124516 KB Output is correct
42 Correct 1016 ms 124516 KB Output is correct
43 Correct 1172 ms 124516 KB Output is correct
44 Correct 1093 ms 124516 KB Output is correct
45 Correct 1008 ms 124516 KB Output is correct
46 Correct 1089 ms 124516 KB Output is correct
47 Correct 894 ms 124636 KB Output is correct
48 Correct 893 ms 124636 KB Output is correct
49 Correct 859 ms 132940 KB Output is correct
50 Correct 857 ms 141432 KB Output is correct
51 Correct 898 ms 145524 KB Output is correct
52 Correct 971 ms 157044 KB Output is correct