JOISC 2020 DAY4

Day 4

T1

每个颜色连向这个颜色最小连通块内的点,倍增优化建边之后跑tarjan即可。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
const int M = 6e6+5;
vector<int> E[N];
vector<int> e[M];
int c[N],n,k;
int num[N];

int anc[N][20],dep[N];
int Node[N][20];
int cnt;
inline void dfs(int x,int pre){
	dep[x]=dep[pre]+1;
	Node[x][0]=k+pre;
	e[k+x].push_back(c[x]);
	anc[x][0]=pre;
	for(int i=1;i<=19;i++){
		anc[x][i]=anc[anc[x][i-1]][i-1];
		if(anc[x][i]){
			Node[x][i]=++cnt;
			e[Node[x][i]].push_back(Node[x][i-1]);
			e[Node[x][i]].push_back(Node[anc[x][i-1]][i-1]);
		}
	}
	for(size_t i=0;i<E[x].size();i++){
		int v=E[x][i];if(v==pre)continue;
		dfs(v,x);
	}
}
vector<int> node[N];
inline int lca(int u,int v){
	if(dep[u]<dep[v])swap(u,v);
	for(int i=19;~i;i--)if(dep[anc[u][i]]>=dep[v])u=anc[u][i];
	if(u==v)return u;
	for(int i=19;~i;i--)if(anc[u][i]!=anc[v][i])u=anc[u][i],v=anc[v][i];
	return anc[u][0];
}
void get(int id,int u,int v){
	for(int i=19;~i;i--){
		if(dep[anc[u][i]]>=dep[v]){
			e[id].push_back(Node[u][i]);
			u=anc[u][i];
		}
	}
}
int col[M],dfn[M],low[M],stk[M],top;
int dfnum;
int cnum;
int Cnt[M];
int d[M];
int ans;
inline void tarjan(int x){
	dfn[x]=low[x]=++dfnum;
	stk[++top]=x;
	for(size_t i=0;i<e[x].size();i++){
		int v=e[x][i];
		if(!dfn[v]){
			tarjan(v);
			low[x]=min(low[x],low[v]);
		}
		else if(!col[v])low[x]=min(low[x],dfn[v]);
	}
	if(dfn[x]==low[x]){
		++cnum;
		while(stk[top]!=x){
			col[stk[top--]]=cnum;
		}col[stk[top--]]=cnum;
		
	}
}

int main()
{
	cin >> n >> k;
	ans=k-1;
	for(int i=1;i<n;i++){
		int u,v;scanf("%d%d",&u,&v);
		E[u].push_back(v),E[v].push_back(u);
	}
	cnt=n+k;
	for(int i=1;i<=n;i++){
		scanf("%d",&c[i]);
		num[c[i]]++;
		node[c[i]].push_back(i);
	}
	dfs(1,0);
	for(int i=1;i<=k;i++){
		int ca = node[i][0];
		for(int j=1;j<num[i];j++)ca=lca(ca,node[i][j]);
		for(int j=0;j<num[i];j++){
			get(i,node[i][j],ca);
		}
	}
	for(int i=1;i<=cnt;i++){
		if(!dfn[i])tarjan(i);
	}
	for(int i=1;i<=cnt;i++){
		if(i<=k)Cnt[col[i]]++;
		for(size_t j=0;j<e[i].size();j++){
			int v=e[i][j];if(col[v]!=col[i])d[col[i]]++;
		}
	}
	for(int i=1;i<=cnum;i++){
		if(Cnt[i]&&!d[i]){
			ans=min(ans,Cnt[i]-1);
		}
	}
	cout << ans << endl;
}

T3

每个点的右侧点连向若干点的左侧点,这些左侧点满足在右侧点的左上/左下45度轮廓里面。

然后优化建边跑最短路就行了。

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;

#define int long long
int n;
long long m;
typedef long long ll;
typedef pair<int,ll> pi;
vector<pi> e[N*33+N*2];
int cnt;
int ch[N*33+N*2][2];
int _root;
inline void ins(int &x,int pre,ll l,ll r,ll p,int id){
	x=++cnt;
	ll mid = (l+r)>>1;
	e[x].push_back(pi(pre,0));
	if(l==r){
		e[x].push_back(pi(id,0));return;
	}
	if(p<=mid){	
		ins(ch[x][0],ch[pre][0],l,mid,p,id);
		e[x].push_back(pi(ch[x][0],0));
		ch[x][1]=ch[pre][1];
	}else {
		ins(ch[x][1],ch[pre][1],mid+1,r,p,id);
		e[x].push_back(pi(ch[x][1],0));
		ch[x][0]=ch[pre][0];
	}
}
inline void Qry(int x,ll l,ll r,ll p,ll q,int id){
	ll mid = (l+r)>>1;
	if(!x)return ;
	if(p<=l&&r<=q){
		e[id].push_back(pi(x,0));
		return ;
	}
	if(p<=mid)Qry(ch[x][0],l,mid,p,q,id);
	if(q>mid)Qry(ch[x][1],mid+1,r,p,q,id);
}

ll t[N], l[N], r[N], c[N];
ll dis[N*33+N*2];
typedef pair<ll,int> pr;
int vis[N*33+N*2];
priority_queue < pr,vector<pr>,greater<pr> > Q;

int ifed[N*33+N*2],ifst[N*33+N*2];

pair<pair<ll,ll>,int> s[N],g[N];
#undef int
int main()
{
	#define int long long
	cin >> m >> n;
	cnt=n*2;
	for(int i=1;i<=n;i++){
		scanf("%lld%lld%lld%lld",&t[i],&l[i],&r[i],&c[i]);
		t[i]*=2;
		e[i].push_back(pi(n+i,c[i]));
		l[i] = l[i]*2-1, r[i] = r[i]*2+1;
		s[i]=make_pair(make_pair(l[i]-t[i],l[i]+t[i]),i);
		g[i]=make_pair(make_pair(r[i]-t[i],r[i]+t[i]),i);
		if(l[i]==1)ifst[i]=1;
		if(r[i]==m*2+1)ifed[i+n]=1;
	}
	sort(s+1,s+n+1);sort(g+1,g+n+1);
	int j=1;
	for(int i=1;i<=n;i++){
		while(j<=n&&s[j].first.first<=g[i].first.first){
			ins(_root,_root,-3000000000ll,4500000000ll,s[j].first.second,s[j].second);
			++j;
		}
		Qry(_root,-3000000000ll,4500000000ll,-3000000000ll,g[i].first.second,g[i].second+n);
	}
	for(int i=1;i<=cnt;i++)dis[i]=1000000000000000000ll;
	for(int i=1;i<=n;i++)if(ifst[i]){
		dis[i]=0;
		Q.push(pr(dis[i],i));
	}
	while(Q.size()){
		int u=Q.top().second;Q.pop();
		if(vis[u])continue;
		vis[u]=1;
		for(size_t i=0;i<e[u].size();i++){
			int v=e[u][i].first;
			ll d=e[u][i].second;
			if(dis[u]+d<dis[v]){
				dis[v]=dis[u]+d;
				Q.push(pr(dis[v],v));
			}
		}
	}
	ll ans = 1000000000000000000ll;
	for(int i=n+1;i<=n*2;i++)if(ifed[i]){
		ans = min(ans, dis[i]);
	}
	printf("%lld\n",ans==1000000000000000000ll?-1:ans);
	#undef int
}

posted @ 2020-03-24 20:42  jerome_wei  阅读(261)  评论(0编辑  收藏  举报