Submission #557322

# Submission time Handle Problem Language Result Execution time Memory
557322 2022-05-05T06:49:13 Z Koosha_mv Swapping Cities (APIO20_swap) C++14
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#include "swap.h"

const int N=2e5+99,lg=20,inf=2e9;

int n,m,h[N],res[N],col[N],deg[N],ok[N];
vector<int> vec[N],nok[N];
vector<pair<int,int>> g[N];
pair<int,int> par[lg][N];

void dfs1(int u,int p){
	f(l,1,lg) par[l][u]={par[l-1][par[l-1][u].F].F,max(par[l-1][u].S,par[l-1][par[l-1][u].F].S)};
	for(auto [v,w] : g[u]){
		if(v==p) continue ;
		h[v]=h[u]+1;
		par[0][v]={u,w};
		dfs1(v,u);
	}
}
int dist(int u,int v){
	int ans=0;
	if(h[u]<h[v]) swap(u,v);
	f_(l,lg-1,0){
		if(h[v]<=h[par[l][u].F]){
			maxm(ans,par[l][u].S);
			u=par[l][u].F;
		}
	}
	if(u==v) return ans;
	f_(l,lg-1,0){
		if(par[l][u].F!=par[l][v].F){
			maxm(ans,par[l][u].S);
			maxm(ans,par[l][v].S);
			u=par[l][u].F;
			v=par[l][v].F;
		}
	}
	maxm(ans,par[0][u].S);
	maxm(ans,par[0][v].S);
	return ans;
}
void init(int _n,int _m,std::vector<int> U, std::vector<int> V, std::vector<int> W){
	fill(res,res+N,inf);
	n=_n,m=_m;
	vector<int> p(m);
	iota(all(p),0);
	sort(all(p),[&](int u,int v){ return W[u]<W[v]; });
	f(i,0,n) vec[i].pb(i),nok[i].pb(i),col[i]=i;
	for(auto id : p){
		int u=U[id],v=V[id],w=W[id];
		//cout<<u<<" "<<v<<" "<<w<<endl;
		deg[u]++,deg[v]++;
		if(col[u]==col[v]){
			ok[col[u]]=1;
			//dbgv(nok[col[u]]);
			for(auto x : nok[col[u]]){
				//cout<<x<<" -------- "<<w<<endl;
				res[x]=w;
			}
			nok[col[u]].clear();
			continue ;
		}
		g[u].pb({v,w});
		g[v].pb({u,w});
		if(vec[col[u]].size()<vec[col[v]].size()) swap(u,v);
		ok[col[u]]|=ok[col[v]];
		ok[col[u]]|=(deg[u]>2 || deg[v]>2);
		for(auto x : nok[col[v]]) nok[col[u]].pb(x);
		for(auto x : vec[col[v]]) col[x]=col[u],vec[col[u]].pb(x);
		if(ok[col[u]]){
			for(auto x : nok[col[u]]) res[x]=w;
			nok[col[u]].clear();
		}
	}
	//f(i,0,n) eror(res[i]);
	dfs1(0,0);
}
int getMinimumFuelCapacity(int u, int v) {
	int ans=max(dist(u,v),max(res[u],res[v]));
	if(ans==inf) return -1;
	return ans;
}

int32_t main(){
	int n,m;
	vector<int> U,V,W;
	cin>>n>>m;
	f(i,0,m){
		int x;
		cin>>x;
		U.pb(x);
	}
	f(i,0,m){
		int x;
		cin>>x;
		V.pb(x);
	}
	f(i,0,m){
		int x;
		cin>>x;
		W.pb(x);
	}
	init(n,m,U,V,W);
	int q;
	cin>>q;
	while(q--){
		int u,v;
		cin>>u>>v;
		cout<<getMinimumFuelCapacity(u,v)<<endl;
	}
}

/*
5 6
0 0 1 1 1 2
1 2 2 3 4 3
4 4 1 2 10 3
*/

Compilation message

swap.cpp: In function 'void dfs1(int, int)':
swap.cpp:33:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |  for(auto [v,w] : g[u]){
      |           ^
/usr/bin/ld: /tmp/ccTlis5U.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccpgnsTS.o:swap.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status