답안 #292702

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
292702 2020-09-07T11:59:57 Z SamAnd 늑대인간 (IOI18_werewolf) C++17
100 / 100
1971 ms 160360 KB
#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 400005;

int n, m, qq;
vector<int> g[N];

void dfs00(int x, int ul, int ur, vector<bool>& c)
{
    if (!(ul <= x && x <= ur))
        return;
    if (c[x])
        return;
    c[x] = true;
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        dfs00(h, ul, ur, c);
    }
}

vector<int> solv0(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)
{
    vector<int> ans;
    for (int i = 0; i < qq; ++i)
    {
        vector<bool> cs, ce;
        cs.assign(n, false);
        ce.assign(n, false);
        dfs00(S[i], L[i], n - 1, cs);
        dfs00(E[i], 0, R[i], ce);
        for (int x = 0; x < n; ++x)
        {
            if (cs[x] && ce[x])
            {
                ans.push_back(1);
                break;
            }
        }
        if (sz(ans) != (i + 1))
            ans.push_back(0);
    }
    return ans;
}

int p0[N];
int fi(int x)
{
    if (x == p0[x])
        return x;
    return p0[x] = fi(p0[x]);
}

vector<vector<int> > g1, g2;

int tin1[N], tout1[N], ti1;
int tin2[N], tout2[N], ti2;
const int k = 20;
int p1[N][k], p2[N][k];

void dfs0(int x, int p0, vector<vector<int> >& g, int tin[], int tout[], int& ti, int p[][k])
{
    tin[x] = ++ti;
    p[x][0] = p0;
    for (int i = 1; i < k; ++i)
        p[x][i] = p[p[x][i - 1]][i - 1];
    for (int i = 0; i < g[x].size(); ++i)
    {
        int h = g[x][i];
        dfs0(h, x, g, tin, tout, ti, p);
    }
    tout[x] = ti;
}

int go1(int x, int l)
{
    for (int i = k - 1; i >= 0; --i)
    {
        if (p1[x][i] >= l)
            x = p1[x][i];
    }
    return x;
}

int go2(int x, int r)
{
    for (int i = k - 1; i >= 0; --i)
    {
        if (p2[x][i] <= r)
            x = p2[x][i];
    }
    return x;
}

int rtin2[N], rtin1[N];

int z;
int t[N * k];
int ul[N * k], ur[N * k];
int ubd(int tl, int tr, int x, int pos)
{
    int ypos = ++z;
    ul[ypos] = ul[pos];
    ur[ypos] = ur[pos];
    t[ypos] = t[pos];
    if (tl == tr)
    {
        t[ypos]++;
        return ypos;
    }
    int m = (tl + tr) / 2;
    if (x <= m)
        ul[ypos] = ubd(tl, m, x, ul[pos]);
    else
        ur[ypos] = ubd(m + 1, tr, x, ur[pos]);
    t[ypos] = t[ul[ypos]] + t[ur[ypos]];
    return ypos;
}

int qry(int tl, int tr, int l, int r, int pos)
{
    if (l > r)
        return 0;
    if (tl == l && tr == r)
        return t[pos];
    int m = (tl + tr) / 2;
    return (qry(tl, m, l, min(m, r), ul[pos]) +
            qry(m + 1, tr, max(m + 1, l), r, ur[pos]));
}

int root[N];

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)
{
    n = N_;
    m = sz(X);
    qq = sz(S);
    for (int i = 0; i < m; ++i)
    {
        int x = X[i];
        int y = Y[i];
        g[x].push_back(y);
        g[y].push_back(x);
    }

    vector<int> ans;

    g1.resize(n);
    g2.resize(n);

    for (int x = 0; x < n; ++x)
        p0[x] = x;
    vector<pair<int, int> > v;
    for (int i = 0; i < m; ++i)
    {
        if (X[i] > Y[i])
            swap(X[i], Y[i]);
        v.push_back(m_p(X[i], Y[i]));
    }
    sort(all(v));
    reverse(all(v));
    for (int i = 0; i < m; ++i)
    {
        int x = v[i].fi;
        int y = v[i].se;
        x = fi(x);
        y = fi(y);
        if (x == y)
            continue;
        g1[x].push_back(y);
        p0[y] = x;
    }

    for (int x = 0; x < n; ++x)
        p0[x] = x;
    v.clear();
    for (int i = 0; i < m; ++i)
    {
        swap(X[i], Y[i]);
        v.push_back(m_p(X[i], Y[i]));
    }
    sort(all(v));
    for (int i = 0; i < m; ++i)
    {
        int x = v[i].fi;
        int y = v[i].se;
        x = fi(x);
        y = fi(y);
        if (x == y)
            continue;
        g2[x].push_back(y);
        p0[y] = x;
    }

    dfs0(0, 0, g1, tin1, tout1, ti1, p1);
    dfs0(n - 1, n - 1, g2, tin2, tout2, ti2, p2);

    for (int x = 0; x < n; ++x)
    {
        rtin2[tin2[x]] = x;
        rtin1[tin1[x]] = x;
    }

    for (int i = 1; i <= n; ++i)
        root[i] = ubd(1, n, tin2[rtin1[i]], root[i - 1]);

    for (int ii = 0; ii < qq; ++ii)
    {
        int x = S[ii];
        int y = E[ii];
        int l = L[ii];
        int r = R[ii];
        x = go1(x, l);
        y = go2(y, r);
        int s = qry(1, n, tin2[y], tout2[y], root[tout1[x]])
            - qry(1, n, tin2[y], tout2[y], root[tin1[x] - 1]);
        if (s)
            ans.push_back(1);
        else
            ans.push_back(0);
    }
    //if (ans != solv0(N_, X, Y, S, E, L, R))
    //    printf("WA\n");
    return ans;
}

Compilation message

werewolf.cpp: In function 'void dfs00(int, int, int, std::vector<bool>&)':
werewolf.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
werewolf.cpp: In function 'void dfs0(int, int, std::vector<std::vector<int> >&, int*, int*, int&, int (*)[20])':
werewolf.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |     for (int i = 0; i < g[x].size(); ++i)
      |                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 8 ms 9856 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 7 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 8 ms 9856 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 7 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
10 Correct 16 ms 11648 KB Output is correct
11 Correct 16 ms 11520 KB Output is correct
12 Correct 15 ms 11520 KB Output is correct
13 Correct 18 ms 11896 KB Output is correct
14 Correct 15 ms 11776 KB Output is correct
15 Correct 18 ms 11648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1010 ms 129064 KB Output is correct
2 Correct 1355 ms 138856 KB Output is correct
3 Correct 1195 ms 133352 KB Output is correct
4 Correct 1024 ms 131304 KB Output is correct
5 Correct 1050 ms 131176 KB Output is correct
6 Correct 1060 ms 131048 KB Output is correct
7 Correct 892 ms 130664 KB Output is correct
8 Correct 1318 ms 138856 KB Output is correct
9 Correct 970 ms 133608 KB Output is correct
10 Correct 688 ms 131176 KB Output is correct
11 Correct 726 ms 131048 KB Output is correct
12 Correct 812 ms 130792 KB Output is correct
13 Correct 1154 ms 145896 KB Output is correct
14 Correct 1193 ms 146028 KB Output is correct
15 Correct 1181 ms 145896 KB Output is correct
16 Correct 1174 ms 146016 KB Output is correct
17 Correct 900 ms 130792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 8 ms 9856 KB Output is correct
4 Correct 7 ms 9856 KB Output is correct
5 Correct 7 ms 9856 KB Output is correct
6 Correct 7 ms 9856 KB Output is correct
7 Correct 7 ms 9856 KB Output is correct
8 Correct 7 ms 9856 KB Output is correct
9 Correct 7 ms 9856 KB Output is correct
10 Correct 16 ms 11648 KB Output is correct
11 Correct 16 ms 11520 KB Output is correct
12 Correct 15 ms 11520 KB Output is correct
13 Correct 18 ms 11896 KB Output is correct
14 Correct 15 ms 11776 KB Output is correct
15 Correct 18 ms 11648 KB Output is correct
16 Correct 1010 ms 129064 KB Output is correct
17 Correct 1355 ms 138856 KB Output is correct
18 Correct 1195 ms 133352 KB Output is correct
19 Correct 1024 ms 131304 KB Output is correct
20 Correct 1050 ms 131176 KB Output is correct
21 Correct 1060 ms 131048 KB Output is correct
22 Correct 892 ms 130664 KB Output is correct
23 Correct 1318 ms 138856 KB Output is correct
24 Correct 970 ms 133608 KB Output is correct
25 Correct 688 ms 131176 KB Output is correct
26 Correct 726 ms 131048 KB Output is correct
27 Correct 812 ms 130792 KB Output is correct
28 Correct 1154 ms 145896 KB Output is correct
29 Correct 1193 ms 146028 KB Output is correct
30 Correct 1181 ms 145896 KB Output is correct
31 Correct 1174 ms 146016 KB Output is correct
32 Correct 900 ms 130792 KB Output is correct
33 Correct 1414 ms 138360 KB Output is correct
34 Correct 501 ms 43616 KB Output is correct
35 Correct 1777 ms 143708 KB Output is correct
36 Correct 1335 ms 137920 KB Output is correct
37 Correct 1698 ms 142056 KB Output is correct
38 Correct 1448 ms 139112 KB Output is correct
39 Correct 1361 ms 160104 KB Output is correct
40 Correct 1332 ms 149344 KB Output is correct
41 Correct 1338 ms 141160 KB Output is correct
42 Correct 826 ms 137964 KB Output is correct
43 Correct 1971 ms 150756 KB Output is correct
44 Correct 1640 ms 142024 KB Output is correct
45 Correct 1125 ms 160360 KB Output is correct
46 Correct 1299 ms 159976 KB Output is correct
47 Correct 1194 ms 151912 KB Output is correct
48 Correct 1171 ms 151656 KB Output is correct
49 Correct 1198 ms 151972 KB Output is correct
50 Correct 1187 ms 151656 KB Output is correct
51 Correct 1193 ms 149356 KB Output is correct
52 Correct 1211 ms 149416 KB Output is correct