답안 #262765

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
262765 2020-08-13T08:33:42 Z dantoh000 Colors (RMI18_colors) C++14
78 / 100
905 ms 99664 KB
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int,int> ii;
int n,m;
int A[150005];
int B[150005];
int U[150005];
int V[150005];
int has[150005];
vector<int> source[150005];
vector<int> sink[150005];
int ans = 0;
struct UFDS{
    vector<ii> op;
    int p[150005];
    int sz[150005];
    void init(int n){
        op.clear();
        for (int i = 1 ; i <= n; i++){
            p[i] = i;
            sz[i] = 1;
        }
    }
    int find(int x){
        return x == p[x] ? x : find(p[x]);
    }
    void un(int x, int y){
        x = find(x), y = find(y);
        if (x == y) return;
        if (sz[x] < sz[y]) swap(x,y);
        p[y] = x;
        sz[x] += sz[y];
        op.push_back({x,y});
    }
    void roll(int SZ){
        int x,y;
        while (op.size() > SZ){
            tie(x,y) = op.back(); op.pop_back();
            sz[x] -= sz[y];
            p[y] = y;
        }
    }
} ufds;
struct node{
    int s,e,m;
    vector<ii> V;
    node *l, *r;
    node (int _s, int _e): s(_s), e(_e){
        m = (s+e)/2;
        if (s != e){
            l = new node(s,m);
            r = new node(m+1,e);
        }
    }
    void up(int qs ,int qe, ii v){
        if (qs == s && qe == e){
            V.push_back(v);
            return;
        }
        if (qs > m) r->up(qs,qe,v);
        else if (qe<=m) l->up(qs,qe,v);
        else l->up(qs,m,v), r->up(m+1,qe,v);
    }
    void dfs(){
        int oldsz = ufds.op.size();
        for (auto x : V){
            ufds.un(x.fi,x.se);
        }
        if (s == e){
            for (auto x : source[s]){
                has[ufds.find(x)] = 1;
            }
            for (auto x : sink[s]){
                if (!has[ufds.find(x)]) ans = 0;
            }
            for (auto x : source[s]){
                has[ufds.find(x)] = 0;
            }
        }
        else{
            l->dfs();
            r->dfs();
        }
        ufds.roll(oldsz);
    }
} *root;
int main(){
    int TC;
    scanf("%d",&TC);
    while (TC--){
        ans = 1;
        scanf("%d%d",&n,&m);
        root = new node(1,n);
        ufds.init(n);
        for (int i = 1; i <= n; i++){
            source[i].clear();
            sink[i].clear();
        }
        for (int i = 1; i <= n; i++) {
            scanf("%d",&A[i]);
            source[A[i]].push_back(i);
        }
        for (int i = 1; i <= n; i++) {
            scanf("%d",&B[i]);
            sink[B[i]].push_back(i);
        }
        for (int i = 1; i <= m; i++) {
            scanf("%d%d",&U[i],&V[i]);
            int L = max(B[U[i]],B[V[i]]);
            int R = min(A[U[i]],A[V[i]]);
            if (L > R) continue;
            root->up(L,R,ii(U[i],V[i]));
        }
        root->dfs();
        printf("%d\n",ans);
    }

}

Compilation message

colors.cpp: In member function 'void UFDS::roll(int)':
colors.cpp:39:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   39 |         while (op.size() > SZ){
      |                ~~~~~~~~~~^~~~
colors.cpp: In function 'int main()':
colors.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   91 |     scanf("%d",&TC);
      |     ~~~~~^~~~~~~~~~
colors.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   94 |         scanf("%d%d",&n,&m);
      |         ~~~~~^~~~~~~~~~~~~~
colors.cpp:102:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  102 |             scanf("%d",&A[i]);
      |             ~~~~~^~~~~~~~~~~~
colors.cpp:106:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  106 |             scanf("%d",&B[i]);
      |             ~~~~~^~~~~~~~~~~~
colors.cpp:110:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  110 |             scanf("%d%d",&U[i],&V[i]);
      |             ~~~~~^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 29828 KB Output is correct
2 Correct 47 ms 15480 KB Output is correct
3 Correct 9 ms 8320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 103 ms 15352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 30200 KB Output is correct
2 Correct 51 ms 15608 KB Output is correct
3 Correct 12 ms 8448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 30200 KB Output is correct
2 Correct 51 ms 15608 KB Output is correct
3 Correct 12 ms 8448 KB Output is correct
4 Correct 265 ms 55100 KB Output is correct
5 Correct 557 ms 78816 KB Output is correct
6 Correct 905 ms 99664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 29828 KB Output is correct
2 Correct 47 ms 15480 KB Output is correct
3 Correct 9 ms 8320 KB Output is correct
4 Correct 129 ms 30200 KB Output is correct
5 Correct 51 ms 15608 KB Output is correct
6 Correct 12 ms 8448 KB Output is correct
7 Correct 125 ms 29944 KB Output is correct
8 Correct 57 ms 15480 KB Output is correct
9 Correct 11 ms 8576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 253 ms 54136 KB Output is correct
2 Correct 592 ms 78660 KB Output is correct
3 Correct 571 ms 75360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 12012 KB Output is correct
2 Correct 22 ms 9088 KB Output is correct
3 Correct 13 ms 8448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 29828 KB Output is correct
2 Correct 47 ms 15480 KB Output is correct
3 Correct 9 ms 8320 KB Output is correct
4 Correct 103 ms 15352 KB Output is correct
5 Correct 129 ms 30200 KB Output is correct
6 Correct 51 ms 15608 KB Output is correct
7 Correct 12 ms 8448 KB Output is correct
8 Correct 265 ms 55100 KB Output is correct
9 Correct 557 ms 78816 KB Output is correct
10 Correct 905 ms 99664 KB Output is correct
11 Correct 125 ms 29944 KB Output is correct
12 Correct 57 ms 15480 KB Output is correct
13 Correct 11 ms 8576 KB Output is correct
14 Correct 253 ms 54136 KB Output is correct
15 Correct 592 ms 78660 KB Output is correct
16 Correct 571 ms 75360 KB Output is correct
17 Correct 53 ms 12012 KB Output is correct
18 Correct 22 ms 9088 KB Output is correct
19 Correct 13 ms 8448 KB Output is correct
20 Correct 153 ms 20128 KB Output is correct
21 Correct 512 ms 69868 KB Output is correct
22 Incorrect 786 ms 97724 KB Output isn't correct