[杂题] Codeforces 627F 8VC Venture Cup 2016
来源:互联网 发布:柜子设计软件手机 编辑:程序博客网 时间:2024/05/20 13:07
题目大意
有一棵n+1个点树 在上面玩n数码 问是否有解 最小步数
否则问加一条边是否有解 最小步数
这个好像WC的T1啊?
根据当时考场上玩出来的经验
一棵树直接跑整条路径就好了
环套树是先移动到环上,然后绕着环走若干圈,最后再走向目的地。
我们先当做树跑一通
然后看一下所有不在自己的目标位置的宝石是否可以添一条边组成一个环。如果不能组成,答案就是-1
怎么看 所有不在自己的目标位置的宝石是否可以添一条边组成一个环
可以发现这些点必然是一条链或两条链
- 一条链的话 两个点 x y 分别表示离终点近的和远的那端 肯定是连 y x’ 其中 x’ 表示x到终点的路径上x的邻接点也就是第二个点
- 两条链 按远近分为 a - b c - d 必须要b’==d’ 然后连 a c
这样就确定了连哪条边 然后就跑到环 在环上跑个几圈 这个由位置的偏差决定 然后再跑到终点就好了
写的我醉生梦死 从来没写过130+的main()
#include<cstdio>#include<cstdlib>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline void read(int &x){ char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int N=200005;struct edge{ int u,v,next;}G[N<<1];int head[N],inum;inline void add(int u,int v,int p){ G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;}#define V G[p].vint n;int s,t;int S[N],T[N],bS[N],bT[N];int c[N];int fat[N],depth[N];int cir[N];inline void dfs(int u,int fa){ depth[u]=depth[fa]+1; fat[u]=fa; for (int p=head[u];p;p=G[p].next) if (V!=fa && !cir[V]) dfs(V,u);}int vst[N],deg[N];int tot;int a[N],b[N];int lst[N],pnt;int Ansu,Ansv;int main(){ int iu,iv; read(n); for (int i=1;i<=n;i++) { read(S[i]); if (!S[i]) s=i; } for (int i=1;i<=n;i++) { read(T[i]); if (!T[i]) t=i; } for (int i=1;i<n;i++) read(iu),read(iv),add(iu,iv,++inum),add(iv,iu,++inum); dfs(t,0); int p=s; for (int i=1;i<=n;i++) c[i]=S[i]; while (p!=t) swap(c[p],c[fat[p]]),p=fat[p]; int flag=0; for (int i=1;i<=n;i++) flag|=c[i]!=T[i]; if (!flag) return printf("0 %d\n",depth[s]-1),0; for (int i=1;i<=n;i++) vst[i]=c[i]!=T[i]; while (1){ int k=0; for (int i=1;i<=n;i++) if (vst[i]) { k=i; break; } if (!k || tot>2) break; int t=k; while (1){ vst[t]=0; int tmp=t; for (int p=head[t];p;p=G[p].next) if (vst[V]){ t=V; break; } if (tmp==t) break; } a[++tot]=t; t=k; while (1){ vst[t]=0; int tmp=t; for (int p=head[t];p;p=G[p].next) if (vst[V]){ t=V; break; } if (tmp==t) break; } b[tot]=t; } if (tot>2) return printf("-1\n"),0; if (tot==1){ int &a=::a[1],&b=::b[1]; if (depth[a]>depth[b]) swap(a,b); int t=b; while (t && t!=a) t=fat[t]; if (!t) return printf("-1\n"),0; add(fat[a],b,++inum); add(b,fat[a],++inum); if (fat[a]<b) Ansu=fat[a],Ansv=b; else Ansu=b,Ansv=fat[a]; t=b; while (t!=fat[a]) cir[t]=1,t=fat[t]; cir[fat[a]]=1; }else{ for (int i=1;i<=2;i++){ int &a=::a[i],&b=::b[i]; if (depth[a]>depth[b]) swap(a,b); int t=b; while (t && t!=a) t=fat[t]; if (!t) return printf("-1\n"),0; } if (fat[a[1]]!=fat[a[2]]) return printf("-1\n"),0; add(b[1],b[2],++inum); add(b[2],b[1],++inum); if (b[1]<b[2]) Ansu=b[1],Ansv=b[2]; else Ansu=b[2],Ansv=b[1]; for (int i=1;i<=2;i++){ int t=b[i]; while (t!=fat[a[i]]) cir[t]=1,t=fat[t]; cir[fat[a[i]]]=1; } } for (int i=1;i<=n;i++) if (cir[i]) dfs(i,0); ll ans=0; while (!cir[s]) swap(S[s],S[fat[s]]),s=fat[s],ans++; while (!cir[t]) swap(T[t],T[fat[t]]),t=fat[t],ans++; int _t=s; pnt=-1; while (1){ cir[_t]=0; lst[++pnt]=_t; int tmp=_t; for (int p=head[_t];p;p=G[p].next) if (cir[V]){ _t=V; break; } if (tmp==_t) break; } ll ret=1LL<<60; int step=0; int pt=0; pnt++; for (int i=0;i<pnt;i++) if (lst[i]==t) pt=i; memcpy(bS,S,sizeof(S)); memcpy(bT,T,sizeof(T)); for (int i=pt;i;i--) swap(T[lst[i]],T[lst[i-1]]),step++; int pp=1,qq=0,gap; for (int i=1;i<pnt;i++) if (T[lst[i]]==S[lst[1]]) qq=i; gap=qq; for (int i=1;i<pnt;i++){ if (S[lst[pp]]!=T[lst[qq]]) return printf("-1\n",0),0; pp=pp==pnt-1?1:pp+1; qq=qq==pnt-1?1:qq+1; } ret=min(ret,step+(ll)pnt*(min(pnt-gap,gap-1))); memcpy(S,bS,sizeof(S)); memcpy(T,bT,sizeof(T)); step=0; if (pt) for (int i=pt;i<pnt;i++) swap(T[lst[i]],T[lst[(i+1)%pnt]]),step++; pp=1,qq=0,gap; for (int i=1;i<pnt;i++) if (T[lst[i]]==S[lst[1]]) qq=i; gap=qq; ret=min(ret,step+(ll)pnt*(min(pnt-gap,gap-1))); printf("%d %d %I64d\n",Ansu,Ansv,ans+ret); return 0;}
0 0
- [杂题] Codeforces 627F 8VC Venture Cup 2016
- Codeforces 8VC Venture Cup
- Codeforces 8VC Venture Cup 2016 - Elimination Round解题报告
- Codeforces 8VC Venture Cup 2016 - Elimination Round
- 8VC Venture Cup 2016 - Elimination Round F. Group Projects(DP)
- 8VC Venture Cup 2016 - Elimination Round F. Group Projects(DP)★ ★
- Codeforces 8VC Venture Cup 2017 - Elimination Round
- 8VC Venture Cup 2017
- Codeforces8VC Venture Cup 2016 - Elimination RoundA---F
- 8VC Venture Cup 2016 - Elimination Round题解
- 8VC Venture Cup 2016 - Elimination RoundA. Robot Sequence
- 8VC Venture Cup 2016 - Elimination RoundB. Cards
- 8VC Venture Cup 2016 - Elimination RoundC. Block Towers
- 8VC Venture Cup 2016 - Elimination Round A. Robot Sequence
- 8VC Venture Cup 2016 - Elimination Round 总结
- 8VC Venture Cup 2016 - Elimination Round C - Block Towers
- 8VC Venture Cup 2016 -A - Robot Sequence/B - Cards
- 8VC Venture Cup 2016 -C - Block Towers-二分
- Java中的Listener监听器
- poj 2029 树状数组
- abstract class和interface的区别
- 免费馅饼(注释加解析)
- MySQL多表操作
- [杂题] Codeforces 627F 8VC Venture Cup 2016
- oracle5
- 欢迎使用CSDN-markdown编辑器
- 关键字: 抽象类与接口的区别
- 面试题总结(一)
- 原子操作
- @ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
- python公开课第六节
- 设计模式