Submission #262569

# Submission time Handle Problem Language Result Execution time Memory
262569 2020-08-13T04:30:50 Z cheeheng Colors (RMI18_colors) C++14
47 / 100
3000 ms 16632 KB
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;

vector<int> AdjList[150005];
vector<int> colourListA[150005];
vector<int> colourListB[150005];
ii EdgeList[150005];
int a[150005];
int b[150005];
ii ab[150005];
int deg[150005];
bool visited[150005];

int c[150005];
int d[150005];
int pos[150005];
int p[150005];
int rank1[150005];

bool satisfied[150005];

void init(int N){
    for(int i = 1; i <= N; i ++){
        p[i] = i;
        rank1[i] = 0;
    }
}

int findSet(int i){
    return (p[i] == i) ? i : (p[i] = findSet(p[i]));
}

int isSameSet(int i, int j){
    return findSet(i) == findSet(j);
}

void unionSet(int i, int j){
    if(!isSameSet(i, j)){
        int x = findSet(i);
        int y = findSet(j);
        if(rank1[x] > rank1[y]){
            p[y] = x;
        }else{
            p[x] = y;
            if(rank1[x] == rank1[y]){rank1[y] ++;}
        }
    }
}

int main(){
    int t;
    scanf("%d", &t);

    while(t --){
        int N, M;
        scanf("%d%d", &N, &M);

        for(int i = 1; i <= N; i ++){
            AdjList[i].clear();
            colourListA[i].clear();
            colourListB[i].clear();
            deg[i] = 0;
        }

        set<int> s1;
        for(int i = 1; i <= N; i ++){
            scanf("%d", &a[i]);
            s1.insert(a[i]);
            colourListA[a[i]].push_back(i);
        }

        for(int i = 1; i <= N; i ++){
            scanf("%d", &b[i]);
            colourListB[b[i]].push_back(i);
        }

        for(int i = 0; i < M; i ++){
            int u, v;
            scanf("%d%d", &u, &v);
            AdjList[u].push_back(v);
            AdjList[v].push_back(u);
            EdgeList[i] = ii(u, v);
            deg[u] ++;
            deg[v] ++;
        }

        int ans = 1;
        for(int i = 1; i <= N; i ++){
            if(b[i] > a[i] || s1.find(b[i]) == s1.end()){
                ans = 0;
                break;
            }
        }

        if(ans == 0){
            printf("0\n");
            continue;
        }

        int starVertex = -1;
        for(int i = 1; i <= N; i ++){
            if(deg[i] == N-1){
                starVertex = i;
                break;
            }
        }

        int cntDeg1 = 0;
        for(int i = 1; i <= N; i ++){
            cntDeg1 += (deg[i] == 1);
        }


        if((long long)M == (long long)N*(N-1)/2){
            multiset<int> s;
            for(int i = 1; i <= N; i ++){
                ab[i] = ii(a[i], b[i]);
                s.insert(a[i]);
            }
            sort(ab+1, ab+(N+1), greater<ii>());

            for(int i = 1; i <= N; i ++){
                if(ab[i].first == ab[i].second){continue;}
                if(ab[i].first < ab[i].second){
                    ans = 0;
                    break;
                }
                if(s.find(ab[i].second) != s.end()){

                }else{
                    ans = 0;
                    break;
                }

                s.erase(s.find(ab[i].first));
            }
            printf("%d\n", ans);
            continue;
        }

        if(starVertex != -1 && M == N-1){
            for(int i = 1; i <= N; i ++){
                if(i == starVertex){continue;}
                if(b[i] <= a[starVertex] && b[i] >= b[starVertex] || b[i] == a[i]){
                }else{
                    ans = 0;
                    break;
                }
            }
            printf("%d\n", ans);
            continue;
        }

        ans = 1;
        for(int c = N; c >= 1; c --){
            init(N);
            for(int i = 1; i <= N; i ++){
                satisfied[i] = b[i] <= c && c <= a[i];
                //printf("c=%d i=%d satisfied=%d\n", c, i, (int)satisfied[i]);
            }

            for(int i = 0; i < M; i ++){
                int u, v;
                tie(u, v) = EdgeList[i];
                if(satisfied[u] && satisfied[v]){
                    //printf("unionSet(%d, %d)\n", u, v);
                    unionSet(u, v);
                }
            }

            bool boleh = true;
            for(int u: colourListB[c]){
                bool found = false;
                for(int v: colourListA[c]){
                    if(isSameSet(u, v)){
                        //printf("isSameSet(%d, %d)\n", u, v);
                        found = true;
                        break;
                    }
                }
                if(!found){
                    boleh = false;
                    break;
                }
            }

            //printf("c=%d boleh=%d\n", c, (int)boleh);
            if(!boleh){ans = 0; break;}
        }

        printf("%d\n", ans);
    }

    return 0;
}

Compilation message

colors.cpp: In function 'int main()':
colors.cpp:147:42: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  147 |                 if(b[i] <= a[starVertex] && b[i] >= b[starVertex] || b[i] == a[i]){
      |                    ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
colors.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   55 |     scanf("%d", &t);
      |     ~~~~~^~~~~~~~~~
colors.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   59 |         scanf("%d%d", &N, &M);
      |         ~~~~~^~~~~~~~~~~~~~~~
colors.cpp:70:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |             scanf("%d", &a[i]);
      |             ~~~~~^~~~~~~~~~~~~
colors.cpp:76:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   76 |             scanf("%d", &b[i]);
      |             ~~~~~^~~~~~~~~~~~~
colors.cpp:82:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |             scanf("%d%d", &u, &v);
      |             ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 89 ms 12408 KB Output is correct
2 Correct 36 ms 11520 KB Output is correct
3 Correct 11 ms 11136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 12664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 12408 KB Output is correct
2 Correct 75 ms 11640 KB Output is correct
3 Correct 34 ms 11136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 144 ms 12408 KB Output is correct
2 Correct 75 ms 11640 KB Output is correct
3 Correct 34 ms 11136 KB Output is correct
4 Correct 319 ms 14232 KB Output is correct
5 Execution timed out 3072 ms 16632 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 89 ms 12408 KB Output is correct
2 Correct 36 ms 11520 KB Output is correct
3 Correct 11 ms 11136 KB Output is correct
4 Correct 144 ms 12408 KB Output is correct
5 Correct 75 ms 11640 KB Output is correct
6 Correct 34 ms 11136 KB Output is correct
7 Correct 129 ms 12424 KB Output is correct
8 Correct 69 ms 11512 KB Output is correct
9 Correct 41 ms 11136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 14132 KB Output is correct
2 Execution timed out 3052 ms 15992 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 77 ms 11768 KB Output is correct
2 Correct 49 ms 11384 KB Output is correct
3 Correct 45 ms 11136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 12408 KB Output is correct
2 Correct 36 ms 11520 KB Output is correct
3 Correct 11 ms 11136 KB Output is correct
4 Correct 86 ms 12664 KB Output is correct
5 Correct 144 ms 12408 KB Output is correct
6 Correct 75 ms 11640 KB Output is correct
7 Correct 34 ms 11136 KB Output is correct
8 Correct 319 ms 14232 KB Output is correct
9 Execution timed out 3072 ms 16632 KB Time limit exceeded
10 Halted 0 ms 0 KB -