답안 #253592

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
253592 2020-07-28T11:41:20 Z emil_physmath 늑대인간 (IOI18_werewolf) C++17
100 / 100
1072 ms 174572 KB
#include "werewolf.h"
#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
const int maxN = 200'005;
struct Quer {
    int l, r, s, e, ind, link1, link2;
};

int n;
vector<int> ans;
vector<Quer> quer, withLink1[maxN * 2];
vector<int> nei[maxN], nei1[maxN * 2], nei2[maxN * 2];
set<int> t[maxN * 2];
int col[maxN], linkl[maxN * 2], linkr[maxN * 2];

struct DSU
{
    vector<int> par, sz, link;
    DSU(int n)
        : par(n)
        , sz(n)
        , link(n)
    {
        for (int i = 0; i < n; ++i)
            par[i] = link[i] = i, sz[i] = 1;
    }
    int root(int v) {
        return v == par[v] ? v : par[v] = root(par[v]);
    }
    void unite(int u, int v, int curLink) {
        u = root(u); v = root(v);
        if (u == v) return;
        if (sz[u] > sz[v]) swap(u, v);
        sz[v] += sz[u];
        par[u] = v;
        link[v] = curLink;
    }
};
void DFS(int v)
{
    static int time = -1;
    linkl[v] = time + 1;
    if (nei2[v].empty())
        col[v] = ++time;
    for (int to: nei2[v])
    {
        // cerr << v << " -> " << to << '\n';
        DFS(to);
    }
    linkr[v] = time;
}
int Color1()
{
    DSU dsu(n);
    int nodes = n;
    sort(quer.begin(), quer.end(), [](const Quer& l, const Quer& r) {
        return l.l < r.l;
    });
    for (int v = n - 1, i = (int)quer.size() - 1; v >= 0; --v)
    {
        nei1[nodes].push_back(dsu.link[dsu.root(v)]);
        for (int to: nei[v])
        {
            if (to < v) continue;
            if (dsu.root(v) == dsu.root(to))
                continue;
            nei1[nodes].push_back(dsu.link[dsu.root(to)]);
            dsu.unite(v, to, nodes);
        }
        while (i >= 0 && quer[i].l == v) {
            quer[i].link1 = dsu.link[dsu.root(quer[i].s)];
            --i;
        }
        ++nodes;
    }
    return nodes - 1;
}
void Color2()
{
    DSU dsu(n);
    int nodes = n;
    sort(quer.begin(), quer.end(), [](const Quer& l, const Quer& r) {
        return l.r > r.r;
    });
    for (int v = 0, i = (int)quer.size() - 1; v < n; ++v)
    {
        int b = -1;
        nei2[nodes].push_back(b = dsu.link[dsu.root(v)]);
        for (int to: nei[v])
        {
            if (to > v) continue;
            if (dsu.root(v) == dsu.root(to))
                continue;
            nei2[nodes].push_back(b = dsu.link[dsu.root(to)]);
            dsu.unite(v, to, nodes);
        }
        while (i >= 0 && quer[i].r == v) {
            quer[i].link2 = dsu.link[dsu.root(quer[i].e)];
            --i;
        }
        ++nodes;
    }
    DFS(nodes - 1);
}
void Solve(int v)
{
    if (nei1[v].empty())
        t[v].insert(col[v]);
    for (int to: nei1[v])
    {
        Solve(to);
        if (t[v].size() < t[to].size())
            t[v].swap(t[to]);
        t[v].insert(t[to].begin(), t[to].end());
    }
    for (Quer& q: withLink1[v])
    {
        auto it = t[v].lower_bound(linkl[q.link2]);
        if (it != t[v].end() && *it <= linkr[q.link2])
            ans[q.ind] = 1;
    }
}
vector<int> check_validity(int N, vector<int> X, vector<int> Y,
                                vector<int> S, vector<int> E,
                                vector<int> L, vector<int> R) {
    n = N;
    quer.resize(S.size());
    for (int i = 0; i < S.size(); ++i)
    {
        quer[i].s = S[i];
        quer[i].e = E[i];
        quer[i].l = L[i];
        quer[i].r = R[i];
        quer[i].ind = i;
    }
    for (int i = 0; i < X.size(); ++i)
    {
        int u = X[i], v = Y[i];
        nei[u].push_back(v);
        nei[v].push_back(u);
    }
    int root = Color1();
    Color2();
    ans.resize(quer.size());
    for (Quer& q: quer)
        withLink1[q.link1].push_back(q);
    Solve(root);
    return ans;
}
/*
6 6 3
5 1
1 2
1 3
3 4
3 0
5 2
4 2 1 2
4 2 2 2
5 4 3 4
*/

Compilation message

werewolf.cpp: In function 'std::vector<int> check_validity(int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
werewolf.cpp:131:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < S.size(); ++i)
                     ~~^~~~~~~~~~
werewolf.cpp:139:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i = 0; i < X.size(); ++i)
                     ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 52096 KB Output is correct
2 Correct 30 ms 52096 KB Output is correct
3 Correct 29 ms 52088 KB Output is correct
4 Correct 29 ms 51968 KB Output is correct
5 Correct 28 ms 52088 KB Output is correct
6 Correct 29 ms 51968 KB Output is correct
7 Correct 29 ms 52088 KB Output is correct
8 Correct 32 ms 52096 KB Output is correct
9 Correct 31 ms 51968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 52096 KB Output is correct
2 Correct 30 ms 52096 KB Output is correct
3 Correct 29 ms 52088 KB Output is correct
4 Correct 29 ms 51968 KB Output is correct
5 Correct 28 ms 52088 KB Output is correct
6 Correct 29 ms 51968 KB Output is correct
7 Correct 29 ms 52088 KB Output is correct
8 Correct 32 ms 52096 KB Output is correct
9 Correct 31 ms 51968 KB Output is correct
10 Correct 44 ms 53240 KB Output is correct
11 Correct 38 ms 53376 KB Output is correct
12 Correct 37 ms 53504 KB Output is correct
13 Correct 45 ms 53368 KB Output is correct
14 Correct 39 ms 53632 KB Output is correct
15 Correct 38 ms 53368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 955 ms 172736 KB Output is correct
2 Correct 706 ms 140484 KB Output is correct
3 Correct 743 ms 149612 KB Output is correct
4 Correct 792 ms 163516 KB Output is correct
5 Correct 820 ms 166580 KB Output is correct
6 Correct 941 ms 172080 KB Output is correct
7 Correct 938 ms 174572 KB Output is correct
8 Correct 732 ms 140372 KB Output is correct
9 Correct 720 ms 148284 KB Output is correct
10 Correct 876 ms 163268 KB Output is correct
11 Correct 785 ms 164804 KB Output is correct
12 Correct 877 ms 170816 KB Output is correct
13 Correct 689 ms 140616 KB Output is correct
14 Correct 727 ms 140484 KB Output is correct
15 Correct 688 ms 140532 KB Output is correct
16 Correct 702 ms 140356 KB Output is correct
17 Correct 1020 ms 172484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 32 ms 52096 KB Output is correct
2 Correct 30 ms 52096 KB Output is correct
3 Correct 29 ms 52088 KB Output is correct
4 Correct 29 ms 51968 KB Output is correct
5 Correct 28 ms 52088 KB Output is correct
6 Correct 29 ms 51968 KB Output is correct
7 Correct 29 ms 52088 KB Output is correct
8 Correct 32 ms 52096 KB Output is correct
9 Correct 31 ms 51968 KB Output is correct
10 Correct 44 ms 53240 KB Output is correct
11 Correct 38 ms 53376 KB Output is correct
12 Correct 37 ms 53504 KB Output is correct
13 Correct 45 ms 53368 KB Output is correct
14 Correct 39 ms 53632 KB Output is correct
15 Correct 38 ms 53368 KB Output is correct
16 Correct 955 ms 172736 KB Output is correct
17 Correct 706 ms 140484 KB Output is correct
18 Correct 743 ms 149612 KB Output is correct
19 Correct 792 ms 163516 KB Output is correct
20 Correct 820 ms 166580 KB Output is correct
21 Correct 941 ms 172080 KB Output is correct
22 Correct 938 ms 174572 KB Output is correct
23 Correct 732 ms 140372 KB Output is correct
24 Correct 720 ms 148284 KB Output is correct
25 Correct 876 ms 163268 KB Output is correct
26 Correct 785 ms 164804 KB Output is correct
27 Correct 877 ms 170816 KB Output is correct
28 Correct 689 ms 140616 KB Output is correct
29 Correct 727 ms 140484 KB Output is correct
30 Correct 688 ms 140532 KB Output is correct
31 Correct 702 ms 140356 KB Output is correct
32 Correct 1020 ms 172484 KB Output is correct
33 Correct 893 ms 145344 KB Output is correct
34 Correct 403 ms 97624 KB Output is correct
35 Correct 941 ms 140444 KB Output is correct
36 Correct 913 ms 146484 KB Output is correct
37 Correct 967 ms 140644 KB Output is correct
38 Correct 930 ms 144964 KB Output is correct
39 Correct 914 ms 157184 KB Output is correct
40 Correct 853 ms 146752 KB Output is correct
41 Correct 846 ms 139588 KB Output is correct
42 Correct 893 ms 145348 KB Output is correct
43 Correct 1072 ms 144412 KB Output is correct
44 Correct 846 ms 140324 KB Output is correct
45 Correct 947 ms 148028 KB Output is correct
46 Correct 901 ms 157952 KB Output is correct
47 Correct 634 ms 140612 KB Output is correct
48 Correct 643 ms 140284 KB Output is correct
49 Correct 738 ms 140528 KB Output is correct
50 Correct 674 ms 140484 KB Output is correct
51 Correct 857 ms 146656 KB Output is correct
52 Correct 893 ms 146748 KB Output is correct