BZOJ 2730 HNOI2012 礦場搭建 Tarjan

來源:互聯網
上載者:User

題目大意:給定一個無向圖,要求將一些點設為出口 要求圖中刪掉任意一個點後剩餘的任意一個點都與至少一個出口相連 求最少建多少個出口以及建最少出口的方案數

首先看到割點就是Tarjan搞 但是怎麼搞

首先假設我們把所有的點雙都縮點 那麼我們一定可以得到一棵樹 然後我們就會發現

葉子節點(只含有一個割點的點雙)必須建 因為葉子節點如果不建 一旦割點被爆就死翹了

非分葉節點(含有兩個或兩個以上的割點的點雙)不用建 因為即使一個割點被爆了也可以沿著另一個割點走到一個分葉節點

還有一種情況就是整個聯通塊都是點雙(即不含割點的點雙) 這樣我們討論點雙的大小

如果只有一個點 那麼這個點必須建 資料沒有卡這個的點所以我沒寫(其實是我忘寫了 然後還過了)

如果有兩個或兩個以上的點 那麼要建兩個 一個被爆了還可以走另一個

方案數就是乘法原理的問題了 注意分葉節點那裡出口不能建在割點上

#include<cstdio>  #include<cstring>  #include<iostream>  #include<algorithm>  #define M 1010  using namespace std;  typedef long long ll;  struct abcd{      int to,next;  }table[M];  int head[M],tot=1;  int n,m,cases,ans1;  long long ans2;  int dpt[M],low[M],cnt,belong[M],stack[M],top;  inline void Initialize()  {      memset(head,0,sizeof head);      memset(dpt,0,sizeof dpt);      memset(belong,0,sizeof belong);      tot=1;n=0;ans1=0;ans2=1;  }  void Add(int x,int y)  {      table[++tot].to=y;      table[tot].next=head[x];      head[x]=tot;  }  void Tarjan(int x)  {      int i;      dpt[x]=low[x]=++cnt;      for(i=head[x];i;i=table[i].next)      {          int y=table[i].to;          if(dpt[y])              low[x]=min(low[x],dpt[y]);          else          {              Tarjan(y);              low[x]=min(low[x],low[y]);              if(low[y]>=dpt[x])                  belong[x]++;          }      }  }  void _Tarjan(int x)  {      int i;      dpt[x]=low[x]=++cnt;      stack[++top]=x;      for(i=head[x];i;i=table[i].next)      {          int y=table[i].to;          if(dpt[y])              low[x]=min(low[x],dpt[y]);          else          {              _Tarjan(y);              low[x]=min(low[x],low[y]);              if(low[y]>=dpt[x])              {                  int t,temp=0,size=0;                  do{                      t=stack[top--];                      if(belong[t]>=2)                          ++temp;                      ++size;                  }while(t!=y);                  t=x;                  if(belong[t]>=2)                      ++temp;                  ++size;                 if(!temp)                      ans1+=2,ans2*=size*(size-1)/2;                  else if(temp==1)                      ans1++,ans2*=size-1;              }          }      }  }  int main()  {        //freopen("2730.in","r",stdin);        int i,x,y;      while(scanf("%d",&m),m)      {          Initialize();          for(i=1;i<=m;i++)          {              scanf("%d%d",&x,&y);              n=max(n,x);              n=max(n,y);              Add(x,y);              Add(y,x);          }          for(i=1;i<=n;i++)              if(!dpt[i])                  Tarjan(i);              else                  belong[i]++;          memset(dpt,0,sizeof dpt);          for(i=1;i<=n;i++)              if(!dpt[i])                  _Tarjan(i);          cout<<"Case "<<++cases<<": ";          cout<<ans1<<' '<<ans2<<endl;      }  }  


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.