Submission #262569

#TimeUsernameProblemLanguageResultExecution timeMemory
262569cheehengColors (RMI18_colors)C++14
47 / 100
3072 ms16632 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...