[构造] Codeforces Gym 101190 NEERC 16 C. Cactus Construction

我们递归地搞 使得包含点P的某个仙人掌被构造出来且除了P是1其他都是2
然后就是拆桥边或拆环 递归下去 然后在合起来就行了
把桥边接起来很显然 环的话 自己画画也不难搞出来了吧

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<set>
#define pb push_back
using namespace std;
typedef pair<int,int> abcd;

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=50005;
set<abcd> Set;
struct edge{
  int u,v,next;
}G[N<<2];
int head[N],inum=1;
int cur[N];
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;
}
inline void Add(int u,int v){
  if (u>v) swap(u,v); if (!Set.count(abcd(u,v))) add(u,v,++inum),add(v,u,++inum),Set.insert(abcd(u,v));
}
#define V G[p].v

int n,m,cnt;
int cir[N<<2],depth[N],fap[N],fat[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)
      if (!depth[V])
    fap[V]=p,dfs(V,u);
      else if (depth[V]<depth[u]){
    int t=u; ++cnt;
    while (t!=V) cir[fap[t]]=cir[fap[t]^1]=cnt,t=fat[t];
    cir[p]=cir[p^1]=cnt;
      }
}

int del[N<<2];

struct info{
  char f; int x,y,z;
  info(char f,int x,int y,int z=0):f(f),x(x),y(y),z(z) { }
};
vector<info> Ans;

#define J(x,y) (Ans.pb(info('j',x,y)))
#define R(x,y,z) (Ans.pb(info('r',x,y,z)))
#define C(x,y,z) (Ans.pb(info('c',x,y,z)))

inline void Solve(int u){
  int ed=0;
  for (int p=cur[u];p;p=G[p].next)
    if (!del[p]){
      ed=p; break;
    }
  cur[u]=ed;
  if (!ed) return;
  del[ed]=del[ed^1]=1;
  int v=G[ed].v;
  if (!cir[ed]){
    Solve(u); Solve(v);
    R(v,1,3); J(u,v); C(u,1,3); R(u,3,2);
    return;
  }
  vector<int> path;
  path.pb(u); path.pb(v); int cur=v;
  while (1){
    int p;
    for (p=head[cur];p;p=G[p].next)
      if (cir[p]==cir[ed] && !del[p])
    break;
    del[p]=del[p^1]=1;
    if (V==u) break;
    path.pb(V); cur=V;
  }
  for (int x:path)
    Solve(x);
  R(v,1,3); J(u,v); C(u,1,3);
  int last=v;
  for (int i=2;i<(int)path.size();i++){
    int x=path[i];
    R(x,1,4); J(last,x); C(x,3,4); R(x,3,2); R(x,4,3);
    last=x;
  }
  C(u,1,3); R(u,3,2);
}

int main(){
  int x,y,k;
  freopen("cactus.in","r",stdin);
  freopen("cactus.out","w",stdout);
  read(n); read(m);
  while (m--){
    read(k); read(x);
    for (int i=2;i<=k;i++)
      read(y),Add(x,y),x=y;
  }
  dfs(1,0);
  for (int i=1;i<=n;i++) cur[i]=head[i];
  Solve(1);
  printf("%d\n",(int)Ans.size());
  for (info x:Ans)
    if (x.f=='j')
      printf("%c %d %d\n",x.f,x.x,x.y);
    else
      printf("%c %d %d %d\n",x.f,x.x,x.y,x.z);
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值