답안 #583123

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
583123 2022-06-24T21:05:27 Z definitelynotmee 늑대인간 (IOI18_werewolf) C++17
100 / 100
866 ms 143048 KB
#include "werewolf.h"
#include<bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
using namespace std;
template <typename T>
using matrix = vector<vector<T>>;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INFL = (1LL<<62)-1;
const int INF = (1<<30)-1;
const double EPS = 1e-7;
const int MOD = 1e9 + 7;
const int RANDOM = chrono::high_resolution_clock::now().time_since_epoch().count();
const int MAXN = 1e6+1;

struct UnionFind{
    vector<int> pai;
    vector<set<int>> lista;
    UnionFind(int n = 0){
        pai = vector<int>(n);
        lista = vector<set<int>>(n);
        iota(all(pai),0);
        for(int i = 0; i < n; i++)
            lista[i].insert(i);
    }
    int find(int id){
        if(pai[id] == id)
            return id;
        return pai[id] = find(pai[id]);
    }
    void onion(int a, int b){
        a = find(a);
        b = find(b);
        if(a == b)
            return;
        if(lista[a].size() > lista[b].size())
            swap(a,b);
        pai[a] = b;
        for(int i : lista[a])
            lista[b].insert(i);
    }
};

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();

    matrix<int> g(n);
    for(int i = 0; i < X.size(); i++){
        g[X[i]].push_back(Y[i]);
        g[Y[i]].push_back(X[i]);
    }

    matrix<int>child(n);
    vector<int> pai(n), dirpai(n);
    iota(all(pai),0);
    iota(all(dirpai),0);

    auto find=[&](int id, auto f)->int{
        if(pai[id] == id)
            return id;
        return pai[id] = f(pai[id],f);
    };
    auto onion =[&](int a, int b){
        a = find(a,find), b = find(b,find);
        if(a == b)
            return;
        if(a > b)
            swap(a,b);

        pai[b] = a;
        dirpai[b] = a;
        child[a].push_back(b);
    };

    for(int i = n-1; i >= 0; i--){
        for(int j : g[i]){
            if(j > i)
                onion(i,j);
        }
    }

    // for(int i = 0; i < n; i++){
    //     for(int j : child[i])
    //         cout << "aresta " << i << ' ' << j << '\n';
    // }

    vector<int> tin(n);
    vector<pii> range(n);

    int timer = -1;

    auto dfs =[&](int id, auto dfs)->void{
        tin[id] = ++timer;
        range[id].ff = timer;
        range[id].ss = timer;
        for(int i : child[id]){
            dfs(i,dfs);
            range[id].ss = range[i].ss;
        }
    };

    dfs(0,dfs);

    // for(int i = 0; i < n; i++)
    //     cout << i << " = " << range[i].ff << ' ' << range[i].ss << '\n';

    matrix<int> lift(20,vector<int>(n));
    for(int i = 0; i < n; i++){
        lift[0][i] = dirpai[i];
    }
    for(int i = 1; i < 20; i++){
        for(int j = 0; j < n; j++){
            lift[i][j] = lift[i-1][lift[i-1][j]];
            //cout << "lift " << i << ' ' << j << " = " << lift[i][j] << '\n';
        }
    }

    auto getroot =[&](int id, int l){
        for(int i = 19; i >= 0; i--)
            if(lift[i][id] >= l)
                id = lift[i][id];
        return id;
    };

    matrix<int> queries(n);

    for(int i = 0; i < Q; i++){
        queries[R[i]].push_back(i);
    }

    UnionFind uf(n);

    vector<int> resp(Q);

    for(int i = 0; i < n; i++){
        //cout << i << endl;
        for(int j : g[i]){
            if(j < i)
                uf.onion(tin[i],tin[j]);
        }
        for(int q : queries[i]){
            int root = getroot(S[q],L[q]);
            // cout << "root(" << q << ") = " << root << '\n';
            // cout << "[" << range[root].ff << ',' << range[root].ss << "]\n";
            int rep = uf.find(tin[E[q]]);
            auto it = uf.lista[rep].lower_bound(tin[root]);
            resp[q] = it!=uf.lista[rep].end()&& *it <= range[root].ss;
            // if(resp[q]){
            //     cout << "->" << q << ' ' << *it << '\n';
            // }
        }
    }

    return resp;
}

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:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for(int i = 0; i < X.size(); i++){
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 6 ms 1748 KB Output is correct
11 Correct 6 ms 1748 KB Output is correct
12 Correct 9 ms 2004 KB Output is correct
13 Correct 5 ms 1620 KB Output is correct
14 Correct 6 ms 1624 KB Output is correct
15 Correct 7 ms 1944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 796 ms 134252 KB Output is correct
2 Correct 671 ms 100844 KB Output is correct
3 Correct 576 ms 105668 KB Output is correct
4 Correct 640 ms 113980 KB Output is correct
5 Correct 652 ms 115304 KB Output is correct
6 Correct 729 ms 121560 KB Output is correct
7 Correct 743 ms 143048 KB Output is correct
8 Correct 599 ms 100860 KB Output is correct
9 Correct 462 ms 106344 KB Output is correct
10 Correct 448 ms 111704 KB Output is correct
11 Correct 460 ms 113300 KB Output is correct
12 Correct 564 ms 123180 KB Output is correct
13 Correct 674 ms 105976 KB Output is correct
14 Correct 699 ms 106084 KB Output is correct
15 Correct 676 ms 105952 KB Output is correct
16 Correct 673 ms 105836 KB Output is correct
17 Correct 745 ms 142320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 296 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 6 ms 1748 KB Output is correct
11 Correct 6 ms 1748 KB Output is correct
12 Correct 9 ms 2004 KB Output is correct
13 Correct 5 ms 1620 KB Output is correct
14 Correct 6 ms 1624 KB Output is correct
15 Correct 7 ms 1944 KB Output is correct
16 Correct 796 ms 134252 KB Output is correct
17 Correct 671 ms 100844 KB Output is correct
18 Correct 576 ms 105668 KB Output is correct
19 Correct 640 ms 113980 KB Output is correct
20 Correct 652 ms 115304 KB Output is correct
21 Correct 729 ms 121560 KB Output is correct
22 Correct 743 ms 143048 KB Output is correct
23 Correct 599 ms 100860 KB Output is correct
24 Correct 462 ms 106344 KB Output is correct
25 Correct 448 ms 111704 KB Output is correct
26 Correct 460 ms 113300 KB Output is correct
27 Correct 564 ms 123180 KB Output is correct
28 Correct 674 ms 105976 KB Output is correct
29 Correct 699 ms 106084 KB Output is correct
30 Correct 676 ms 105952 KB Output is correct
31 Correct 673 ms 105836 KB Output is correct
32 Correct 745 ms 142320 KB Output is correct
33 Correct 763 ms 112092 KB Output is correct
34 Correct 256 ms 32724 KB Output is correct
35 Correct 814 ms 106260 KB Output is correct
36 Correct 732 ms 115504 KB Output is correct
37 Correct 786 ms 106184 KB Output is correct
38 Correct 753 ms 112320 KB Output is correct
39 Correct 681 ms 94580 KB Output is correct
40 Correct 785 ms 113812 KB Output is correct
41 Correct 640 ms 107068 KB Output is correct
42 Correct 635 ms 115796 KB Output is correct
43 Correct 866 ms 108984 KB Output is correct
44 Correct 730 ms 106516 KB Output is correct
45 Correct 652 ms 96028 KB Output is correct
46 Correct 688 ms 95444 KB Output is correct
47 Correct 802 ms 106088 KB Output is correct
48 Correct 716 ms 105856 KB Output is correct
49 Correct 767 ms 105992 KB Output is correct
50 Correct 763 ms 105956 KB Output is correct
51 Correct 807 ms 112620 KB Output is correct
52 Correct 833 ms 112632 KB Output is correct