do_while_true

一言(ヒトコト)

「题解」ARC150C Path and Subsequence

由于子序列匹配是贪心能匹配就匹配,所以想要判断一个点 \(u\) 是否有一条到 \(n\) 的失配的路径,最优一定是选择已经匹配个数最少的那条路径继续往后走。

所以令 \(dis_u\)\(1\) 点走到 \(u\) 点是最短已经匹配到了 \(B\) 的哪个前缀。然后 \((u,v)\in E\) 则更新 \(dis_v\gets dis_u+[B_{dis_u+1}=A_v]\).如果注意到边权仅与 \(dis_u\) 有关而与 \(u\) 无关,所以每个点只有在第一次时被松弛才是有用的,故 Dijkstra 时间复杂度为 \(\mathcal{O}(N\log N+M)\),使用 01 bfs 的时间复杂度为 \(\mathcal{O}(N+M)\)

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<ctime>
#include<random>
#include<assert.h>
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cerr<<"In Line "<< __LINE__<<" the "<<#x<<" = "<<x<<'\n'
#define dpi(x,y) cerr<<"In Line "<<__LINE__<<" the "<<#x<<" = "<<x<<" ; "<<"the "<<#y<<" = "<<y<<'\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef pair<ll,int>pli;
typedef pair<ll,ll>pll;
typedef pair<int,ll>pil;
typedef vector<int>vi;
typedef vector<ll>vll;
typedef vector<pii>vpii;
typedef vector<pil>vpil;
template<typename T>T cmax(T &x, T y){return x=x>y?x:y;}
template<typename T>T cmin(T &x, T y){return x=x<y?x:y;}
template<typename T>
T &read(T &r){
	r=0;bool w=0;char ch=getchar();
	while(ch<'0'||ch>'9')w=ch=='-'?1:0,ch=getchar();
	while(ch>='0'&&ch<='9')r=r*10+(ch^48),ch=getchar();
	return r=w?-r:r;
}
template<typename T1,typename... T2>
void read(T1 &x,T2& ...y){read(x);read(y...);}
const int mod=998244353;
inline void cadd(int &x,int y){x=(x+y>=mod)?(x+y-mod):(x+y);}
inline void cdel(int &x,int y){x=(x-y<0)?(x-y+mod):(x-y);}
inline int add(int x,int y){return (x+y>=mod)?(x+y-mod):(x+y);}
inline int del(int x,int y){return (x-y<0)?(x-y+mod):(x-y);}
int qpow(int x,int y){
	int s=1;
	while(y){
		if(y&1)s=1ll*s*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return s;
}
#define Win do{puts("Yes");return ;}while(0)
#define Lose do{puts("No");return ;}while(0)
#define Alice do{puts("Alice");return ;}while(0)
#define Bob do{puts("Bob");return ;}while(0)
#define Line cerr << "----------\n"
const int N=200010;
const int inf=0x7fffffff;
int n,m,k,a[N],b[N];
int dis[N],vis[N];
vi eg[N];
void solve(){
	read(n,m,k);
	for(int i=1,x,y;i<=m;i++){
		read(x,y);
		eg[x].pb(y);
		eg[y].pb(x);
	}
	for(int i=1;i<=n;i++)read(a[i]),dis[i]=inf;
	for(int i=1;i<=k;i++)read(b[i]);
	dis[1]=a[1]==b[1];
	priority_queue<pii,vector<pii>,greater<pii>>q;
	q.push(mp(dis[1],1));
	while(!q.empty()){
		int x=q.top().se;q.pop();
		if(vis[x])continue;
		vis[x]=1;
		for(auto v:eg[x]){
			int w=dis[x]+(a[v]==b[dis[x]+1]);
			if(dis[v]==inf)dis[v]=w,q.push(mp(dis[v],v));
		}
	}
	if(dis[n]>=k)Win;
	Lose;
}
signed main(){
	#ifdef do_while_true
//		assert(freopen("data.in","r",stdin));
//		assert(freopen("data.out","w",stdout));
	#endif
	int T=1;
//	read(T);
	while(T--){
		solve();
//		Line;
	}
    #ifdef do_while_true
		cerr<<'\n'<<"Time:"<<1.0*clock()/CLOCKS_PER_SEC*1000<<" ms"<<'\n';
	#endif
	return 0;
}
posted @ 2022-11-14 16:26  do_while_true  阅读(18)  评论(0编辑  收藏  举报