Submission #149735

#TimeUsernameProblemLanguageResultExecution timeMemory
149735From The Sky (#200)Trip to the Galapagos Islands (FXCUP4_island)C++17
0 / 100
406 ms14436 KiB
//subtask K <= 1000 #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2") #include "island.h" #include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int maxk = 1000 + 5; const int inf = 1e8; int n, m, k; int a[maxn]; vector<int> have[maxk]; int head[maxn]; vector<pair<int,int>> way[maxn]; pair<int,int> par[maxn]; int mx[maxn]; int ans[maxk][maxk]; int findhead(int x) { if(head[x]==x) return x; return head[x] = findhead(head[x]); } void dfs(int u, int last) { //printf("dfs %d %d\n",u,last); for(auto tmp : way[u]) { int v = tmp.first, val = tmp.second; if(v == last) continue; par[v] = {u, val}; dfs(v, u); } } void up(int u, int ban) { int v = par[u].first, val = par[u].second; if(v!=-1 && a[v]!=ban) { mx[v] = min(mx[u], val); //printf("\tup mx %d = %d\n",v,mx[v]); up(v, ban); } } void down(int u) { //printf("\t\tdown mx %d = %d\n",u,mx[u]); for(auto tmp : way[u]) { int v = tmp.first, val = tmp.second; if(v==par[u].first) continue; mx[v] = max(mx[v], min(mx[u], val)); down(v); } } void Init(int K, vector<int> F, vector<int> S, vector<int> E){ n = F.size(); m = S.size(); k = K; for(int i=0;i<n;i++) { a[i] = F[i]; have[a[i]].push_back(i); } for(int i=0;i<n;i++) head[i] = i; for(int i=m-1;i>=0;i--) { if(findhead(S[i])!=findhead(E[i])) { head[findhead(S[i])] = findhead(E[i]); way[S[i]].push_back({E[i], i}); way[E[i]].push_back({S[i], i}); //printf("edge %d %d\n",S[i],E[i]); } } par[0] = {-1,0}; dfs(0,-1); for(int x=0;x<k;x++) { //printf("solve %d\n",x); for(int u=0;u<n;u++) mx[u] = 0; for(auto u : have[x]) { mx[u] = m; up(u, x); } down(0); for(int u=0;u<n;u++) ans[x][a[u]] = max(ans[x][a[u]], mx[u]); } } int Separate(int A, int B){ return ans[A][B] + 1; //if((int)have[A].size() >= wow) return ans[A][B] + 1; //if((int)have[B].size() >= wow) return ans[B][A] + 1; } /* #include <stdio.h> #include <stdlib.h> static void my_assert(int TF, const char* message){ if(!TF){ puts(message); exit(0); } } int main(){ int N, M, K, Q; std::vector<int> F, S, E; my_assert(scanf("%d%d%d%d", &N, &M, &K, &Q) == 4, "Error: Invalid Input"); my_assert(1 <= K && K <= N && N <= 300000, "Error: Invalid Input"); my_assert(N - 1 <= M && M <= 1000000, "Error: Invalid Input"); my_assert(1 <= Q && Q <= 300000, "Error: Invalid Input"); for(int i = 0; i < N; i++){ int a; my_assert(scanf("%d", &a) == 1, "Error: Invalid Input"); my_assert(0 <= a && a < K, "Error: Invalid Input"); F.push_back(a); } for(int i = 0; i < M; i++){ int a, b; my_assert(scanf("%d%d", &a, &b) == 2, "Error: Invalid Input"); my_assert(0 <= a && a < N && 0 <= b && b < N && a != b, "Error: Invalid Input"); S.push_back(a), E.push_back(b); } Init(K, F, S, E); for(int i = 0; i < Q; i++){ int a, b; my_assert(scanf("%d%d", &a, &b) == 2, "Error: Invalid Input"); my_assert(0 <= a && a < K && 0 <= b && b < K && a != b, "Error: Invalid Input"); printf("%d\n", Separate(a, b)); } return 0; } */

Compilation message (stderr)

island.cpp:2:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(linker, "/stack:200000000")
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...