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

来源:互联网 发布:法医秦明 知乎 编辑:程序博客网 时间:2024/05/16 04:15

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

#include<cstdio>#include<cstdlib>#include<algorithm>#include<vector>#include<set>#define pb push_backusing 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].vint 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
原创粉丝点击