답안 #878069

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
878069 2023-11-24T04:08:05 Z sleepntsheep 늑대인간 (IOI18_werewolf) C++17
100 / 100
857 ms 159528 KB
#define SHINLENA2

#ifndef SHINLENA
#include "werewolf.h"
#endif 


#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cstddef>
#include <cassert>
using namespace std;
#define NN 200000
 
 
struct kruskal_reconstruction_tree
{
    vector<int> p, tin, tout, rv;
    int l;
    vector<vector<int>> g;
 
    kruskal_reconstruction_tree(int n, int n0) : p(n), tin(n), tout(n), rv(n), l(n0), g(n)
    {
        iota(p.begin(), p.end(), 0);
    }
 
    int find(int x)
    {
        return x == p[x] ? x : p[x] = find(p[x]);
    }
 
    int unite(int x, int y)
    {
        x = find(x); y = find(y);
        if (x == y) return 0;
        g[p[x] = p[y] = ++l] = {x, y};
        return 1;
    }
 
    int timer = 0;
    void dfs(int u, int p)
    {
        rv[tin[u] = timer++] = u;
        for (auto v : g[u]) if (v != p) dfs(v, u);
        tout[u] = timer - 1;
    }
 
    void tour()
    {
        for (int i = l +1; i--;) if (!tin[i]) dfs(i, i);
    }
};
 

vector<int> t[NN<<3];
 
void add(int v, int l, int r, int p, int k)
{
    t[v].push_back(k);
    if (l == r) return;
    if (p <= (l+r)/2) add(2*v+1, l, (l+r)/2, p, k);
    else add(2*v+2, (l+r)/2+1, r, p, k);
}
 
int qry(int v, int l, int r, int x, int y, int k)
{
    if (r < x || y < l) return 1e9;
    if (x <= l && r <= y)
    {
        auto it = lower_bound(t[v].begin(), t[v].end(), k);
        return it == t[v].end() ? 1e9 : *it;
    }
    return min(qry(2*v+1, l, (l+r)/2, x, y, k), qry(2*v+2, (l+r)/2+1, r, x, y, k));
}
 
vector<int> check_validity(int N, vector<int> X, vector<int> Y,
        vector<int> S, vector<int> E,
        vector<int> L, vector<int> R) {
    int M = X.size();
    kruskal_reconstruction_tree krt1(N+M, N-1);
    kruskal_reconstruction_tree krt2(N+M, N-1);
 
    int Q = S.size();
    vector<int> A(Q);
    vector<vector<int>> g(N);
 
    for (int i = 0; i < M; ++i) g[X[i]].push_back(Y[i]), g[Y[i]].push_back(X[i]);
 
    /* build krt */
    vector<int> rtl(Q), rtr(Q);
    {
        vector<vector<int>> byl(N), byr(N);
        for (int i = 0; i < Q; ++i) byl[L[i]].push_back(i), byr[R[i]].push_back(i);
        for (int i = N; i--;)
        {
            for (auto v : g[i]) if (v >= i) krt1.unite(v, i);
            for (auto j : byl[i]) rtl[j] = krt1.find(S[j]);
        }
        for (int i = 0; i < N; ++i)
        {
            for (auto v : g[i]) if (v <= i) krt2.unite(v, i);
            for (auto j : byr[i]) rtr[j] = krt2.find(E[j]);
        }
    }
 
    krt1.tour();
    krt2.tour();
    
     int LL = krt1.timer - 1;
    /* build merge sort tree */
    {
        vector<pair<int, int>> ins;
        for (int i = 0; i < N; ++i)
             ins.emplace_back(krt2.tin[i], krt1.tin[i]);
             sort(ins.begin(), ins.end());
             for (auto [x, y] : ins) add(0, 0, LL, y, x);
    }
    
    for (int i = 0; i < Q; ++i)
    {
        A[i] = qry(0, 0, LL, krt1.tin[rtl[i]], krt1.tout[rtl[i]], krt2.tin[rtr[i]]) <= krt2.tout[rtr[i]];
    }
 
    return A;
}

#ifdef SHINLENA
int main()
{
    int n,m,q;
    cin>>n>>m>>q;
    vector<int>x,y,s,e,l,r;
    for (int u,v,i=0;i<m;++i)cin>>u>>v, x.push_back(u), y.push_back(v);
    
    for (int a,b,c,d,i=0;i<q;++i)
    {
        cin>>a>>b>>c>>d;
        s.push_back(a);
        e.push_back(b);
        l.push_back(c);
        r.push_back(d);
        
    }
    auto A = check_validity(n,x,y,s,e,l,r);
    for (auto x : A) cout << x << endl;
    return 0;
}

#endif

/*

6 6 3
5 1
1 3
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:116:9: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  116 |         for (int i = 0; i < N; ++i)
      |         ^~~
werewolf.cpp:118:14: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  118 |              sort(ins.begin(), ins.end());
      |              ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 38012 KB Output is correct
2 Correct 8 ms 37980 KB Output is correct
3 Correct 9 ms 38048 KB Output is correct
4 Correct 8 ms 37980 KB Output is correct
5 Correct 8 ms 37980 KB Output is correct
6 Correct 8 ms 38036 KB Output is correct
7 Correct 8 ms 37980 KB Output is correct
8 Correct 8 ms 37976 KB Output is correct
9 Correct 8 ms 37980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 38012 KB Output is correct
2 Correct 8 ms 37980 KB Output is correct
3 Correct 9 ms 38048 KB Output is correct
4 Correct 8 ms 37980 KB Output is correct
5 Correct 8 ms 37980 KB Output is correct
6 Correct 8 ms 38036 KB Output is correct
7 Correct 8 ms 37980 KB Output is correct
8 Correct 8 ms 37976 KB Output is correct
9 Correct 8 ms 37980 KB Output is correct
10 Correct 15 ms 39512 KB Output is correct
11 Correct 15 ms 39516 KB Output is correct
12 Correct 13 ms 39316 KB Output is correct
13 Correct 17 ms 39512 KB Output is correct
14 Correct 14 ms 39512 KB Output is correct
15 Correct 14 ms 39772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 606 ms 141552 KB Output is correct
2 Correct 644 ms 144300 KB Output is correct
3 Correct 610 ms 142760 KB Output is correct
4 Correct 568 ms 141356 KB Output is correct
5 Correct 592 ms 141488 KB Output is correct
6 Correct 662 ms 141360 KB Output is correct
7 Correct 595 ms 141140 KB Output is correct
8 Correct 605 ms 144396 KB Output is correct
9 Correct 492 ms 142364 KB Output is correct
10 Correct 497 ms 141720 KB Output is correct
11 Correct 508 ms 141516 KB Output is correct
12 Correct 472 ms 141356 KB Output is correct
13 Correct 571 ms 141596 KB Output is correct
14 Correct 547 ms 141592 KB Output is correct
15 Correct 579 ms 141324 KB Output is correct
16 Correct 561 ms 141480 KB Output is correct
17 Correct 544 ms 141496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 38012 KB Output is correct
2 Correct 8 ms 37980 KB Output is correct
3 Correct 9 ms 38048 KB Output is correct
4 Correct 8 ms 37980 KB Output is correct
5 Correct 8 ms 37980 KB Output is correct
6 Correct 8 ms 38036 KB Output is correct
7 Correct 8 ms 37980 KB Output is correct
8 Correct 8 ms 37976 KB Output is correct
9 Correct 8 ms 37980 KB Output is correct
10 Correct 15 ms 39512 KB Output is correct
11 Correct 15 ms 39516 KB Output is correct
12 Correct 13 ms 39316 KB Output is correct
13 Correct 17 ms 39512 KB Output is correct
14 Correct 14 ms 39512 KB Output is correct
15 Correct 14 ms 39772 KB Output is correct
16 Correct 606 ms 141552 KB Output is correct
17 Correct 644 ms 144300 KB Output is correct
18 Correct 610 ms 142760 KB Output is correct
19 Correct 568 ms 141356 KB Output is correct
20 Correct 592 ms 141488 KB Output is correct
21 Correct 662 ms 141360 KB Output is correct
22 Correct 595 ms 141140 KB Output is correct
23 Correct 605 ms 144396 KB Output is correct
24 Correct 492 ms 142364 KB Output is correct
25 Correct 497 ms 141720 KB Output is correct
26 Correct 508 ms 141516 KB Output is correct
27 Correct 472 ms 141356 KB Output is correct
28 Correct 571 ms 141596 KB Output is correct
29 Correct 547 ms 141592 KB Output is correct
30 Correct 579 ms 141324 KB Output is correct
31 Correct 561 ms 141480 KB Output is correct
32 Correct 544 ms 141496 KB Output is correct
33 Correct 771 ms 143032 KB Output is correct
34 Correct 228 ms 98824 KB Output is correct
35 Correct 844 ms 143424 KB Output is correct
36 Correct 710 ms 138080 KB Output is correct
37 Correct 857 ms 141456 KB Output is correct
38 Correct 760 ms 138808 KB Output is correct
39 Correct 647 ms 144596 KB Output is correct
40 Correct 620 ms 158356 KB Output is correct
41 Correct 720 ms 140428 KB Output is correct
42 Correct 612 ms 137824 KB Output is correct
43 Correct 852 ms 150188 KB Output is correct
44 Correct 796 ms 141860 KB Output is correct
45 Correct 562 ms 144944 KB Output is correct
46 Correct 594 ms 144508 KB Output is correct
47 Correct 536 ms 137384 KB Output is correct
48 Correct 568 ms 137128 KB Output is correct
49 Correct 565 ms 137552 KB Output is correct
50 Correct 522 ms 137012 KB Output is correct
51 Correct 582 ms 159476 KB Output is correct
52 Correct 576 ms 159528 KB Output is correct