演算法:UVA 1391 Astronauts(2-SAT + 輸出方案)

來源:互聯網
上載者:User

【題目大意】

有n個宇航員,按照年齡劃分,年齡低於平均年齡的是年輕宇航員,而年齡大於等 於平均年齡的是老練的宇航員。

現在要分配他們去A,B,C三個空間站,其中A站只有老練的宇航員 才能去,而B站是只有年輕的才能去,C站都可以去。

有m對宇航員相互討厭,不能讓他們在同一個空 間站工作。

輸出每個宇航員應分配到哪個空間站,如果沒有則輸出No solution.

【思路】

對於每一個宇航員,他都只有兩個地方能去:C或者非C(如果是年輕的是B,老練的是A), 所以是 二選一的,可以想到是2-SAT判定問題。

建邊時,分好情況加邊即可。然後就是套模板了。

【代碼】

#include<iostream>  #include<queue>  #include<cstdio>  #include<cstring>  #define WHITE -1  #define RED 1  #define BLUE 0  using namespace std;          const int INF  = 0x3f3f3f3f;  const int MAXN = 100010;  const int VN = MAXN*2;  const int EN = VN*5;      int n, m;  int age[MAXN];  int sum;      struct Graph{      int size, head[VN];      struct{int u, v, next; }E[EN];      void init(){size=0; memset(head, -1, sizeof(head)); }      void addEdge(int u, int v){          E[size].u = u;          E[size].v = v;          E[size].next = head[u];          head[u] = size++;      }  }g, g1;      class Tow_Sat{  public:      bool check(){          scc();          for(int i=0; i<n; ++i)              if(belong[i] == belong[i+n])                  return false;          return true;      }      void toposort(){          g1.init();          memset(indeg, 0, sizeof(indeg));          for(int i=0; i<n; ++i){              opp[belong[i]] = belong[i+n];              opp[belong[i+n]] = belong[i];          }          for(int e=0; e<g.size; ++e){              int u = belong[g.E[e].u];              int v = belong[g.E[e].v];              if(u==v) continue;              ++indeg[u];              g1.addEdge(v, u);          }                      queue<int>que;          memset(color, WHITE, sizeof(color));          for(int i=1; i<=bcnt; ++i)              if(!indeg[i])                  que.push(i);                      while(!que.empty()){              int u = que.front();              que.pop();              if(color[u] != WHITE) continue;              color[u] = RED;              color[opp[u]] = BLUE;              for(int e=g1.head[u]; e!=-1; e=g1.E[e].next){                  int v=g1.E[e].v;                  if(--indeg[v] == 0)                      que.push(v);              }          }          // output          for(int i=0; i<n; ++i){              if(color[belong[i]] == RED){                  if(age[i]*n < sum) puts("B");                  else puts("A");              }else{                  puts("C");              }          }      }      private:      void tarjan(const int u){          int v;          dfn[u] = low[u] = ++idx;          instack[u] = true;          sta[top++] = u;          for(int e=g.head[u]; e!=-1; e=g.E[e].next){              v = g.E[e].v;              if(dfn[v] == -1) tarjan(v), low[u]=min(low[u], low[v]);              else if(instack[v]) low[u]=min(low[u], dfn[v]);          }          if(low[u] == dfn[u]){              ++bcnt;              do{                  v=sta[--top];                  instack[v] = false;                  belong[v] = bcnt;              }while(u != v);          }          }      void scc(){          idx = bcnt = top = 0;          memset(instack, 0, sizeof(instack));          memset(dfn, -1, sizeof(dfn));          for(int i=0; i<2*n; ++i)              if(dfn[i] == -1)                  tarjan(i);      }      private:      int idx, top, bcnt;      int low[VN],dfn[VN],belong[VN],sta[VN];      bool instack[VN];      int color[VN], indeg[VN], opp[VN];  }sat;      int main(){      while(~scanf("%d%d", &n, &m) && n+m){                  sum = 0;          for(int i=0; i<n; ++i){              scanf("%d", &age[i]);              sum += age[i];              }              g.init();          int a, b;          for(int i=0; i<m; ++i){              scanf("%d%d", &a, &b);              --a; --b;              if(age[a]*n < sum && age[b]*n < sum){ //小,小                  g.addEdge(a, b+n);                  g.addEdge(a+n, b);                  g.addEdge(b, a+n);                  g.addEdge(b+n, a);              }else if(age[a]*n < sum && age[b]*n >= sum){//小,大                  g.addEdge(a+n, b);                  g.addEdge(b+n, a);              }else if(age[a]*n >= sum && age[b]*n < sum){//大,小                  g.addEdge(a+n, b);                  g.addEdge(b+n, a);              }else{ // 大,大                  g.addEdge(a, b+n);                  g.addEdge(a+n, b);                  g.addEdge(b, a+n);                  g.addEdge(b+n, a);              }              }          if(!sat.check()) puts("No solution.");          else sat.toposort();      }      return 0;  }

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.