zoukankan      html  css  js  c++  java
  • UVALive 3713 Astronauts

    Astronauts

    Time Limit: 3000ms
    Memory Limit: 131072KB
    This problem will be judged on UVALive. Original ID: 3713
    64-bit integer IO format: %lld      Java class name: Main
     

    The Bandulu Space Agency (BSA) has plans for the following three space missions:

    • Mission A: Landing on Ganymede, the largest moon of Jupiter.
    • Mission B: Landing on Callisto, the second largest moon of Jupiter.
    • Mission C: Landing on Titan, the largest moon of Saturn.

    Your task is to assign a crew for each mission. BSA has trained a number of excellent astronauts; everyone of them can be assigned to any mission. However, if two astronauts hate each other, then it is not wise to put them on the same mission. Furthermore, Mission A is clearly more prestigious than Mission B; who would like to go to the second largest moon if there is also a mission to the largest one? Therefore, the assignments have to be done in such a way that only young, inexperienced astronauts go to Mission B, and only senior astronauts are assigned to Mission A. An astronaut is considered young if their age is less than the average age of the astronauts and an astronaut is senior if their age is at least the averageage. Every astronaut can be assigned to Mission C, regardless of their age (but you must not assign two astronauts to the same mission if they hate each other).

     

    Input

    The input contains several blocks of test cases. Each case begins with a line containing two integers 1$ le$n$ le$100000 <tex2html_verbatim_mark>and 1$ le$m$ le$100000 <tex2html_verbatim_mark>. The number n <tex2html_verbatim_mark>is the number of astronauts. The next n <tex2html_verbatim_mark>lines specify the age of the n <tex2html_verbatim_mark>astronauts; each line contains a single integer number between 0 and 200. The next m <tex2html_verbatim_mark>lines contains two integers each, separated by a space. A line containing i <tex2html_verbatim_mark>and j <tex2html_verbatim_mark>(1$ le$ij$ le$n) <tex2html_verbatim_mark>means that the i <tex2html_verbatim_mark>-th astronaut and the j <tex2html_verbatim_mark>-th astronaut hate each other.

    The input is terminated by a block with n = m = 0 <tex2html_verbatim_mark>.

     

    Output

    For each test case, you have to output n lines, each containing a single letter. This letter is either `A', `B', or `C'. The i <tex2html_verbatim_mark>-th line describes which mission the i <tex2html_verbatim_mark>-th astronaut is assigned to. Astronauts that hate each other should not be assigned to the same mission, only young astronauts should be assigned to Mission B and only senior astronauts should be assigned to Mission A. If there is no such assignment, then output the single line `No solution.' (without quotes).

     

    Sample Input

    16 20
    21
    22
    23
    24
    25
    26
    27
    28
    101
    102
    103
    104
    105
    106
    107
    108
    1 2
    3 4
    5 6 
    7 8
    9 10
    11 12
    13 14
    15 16
    1 10
    2 9
    3 12
    4 11
    5 14
    6 13 
    7 16
    8 15
    1 12
    1 13
    3 16
    6 15
    0 0
    

    Sample Output

    B
    C
    C
    B
    C
    B
    C
    B
    A
    C
    C
    A
    C
    A
    C
    A
    

    Source

     
    解题:2-SAT 年龄相同的话,只能一样一个,两个人不能同在一个
     
      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 const int maxn = 200010;
      4 int n,m,sum,age[maxn];
      5 struct arc {
      6     int to,next;
      7     arc(int x = 0,int y = -1) {
      8         to = x;
      9         next = y;
     10     }
     11 } e[maxn<<4];
     12 int head[maxn],tot;
     13 void add(int u,int v) {
     14     e[tot] = arc(v,head[u]);
     15     head[u] = tot++;
     16 }
     17 int dfn[maxn],low[maxn],belong[maxn];
     18 bool instack[maxn];
     19 int scc,idx,mark[maxn],color[maxn],ind[maxn];
     20 stack<int>stk;
     21 vector<int>g[maxn];
     22 queue<int>q;
     23 void init() {
     24     for(int i = 0; i < maxn; ++i) {
     25         g[i].clear();
     26         instack[i] = false;
     27         color[i] = dfn[i] = 0;
     28         ind[i] = belong[i] = 0;
     29         head[i] = -1;
     30     }
     31     idx = scc = tot = sum = 0;
     32     while(!stk.empty()) stk.pop();
     33     while(!q.empty()) q.pop();
     34 
     35 }
     36 void tarjan(int u) {
     37     dfn[u] = low[u] = ++idx;
     38     instack[u] = true;
     39     stk.push(u);
     40     for(int i = head[u]; ~i; i = e[i].next) {
     41         if(!dfn[e[i].to]) {
     42             tarjan(e[i].to);
     43             low[u] = min(low[u],low[e[i].to]);
     44         } else if(instack[e[i].to]) low[u] = min(low[u],dfn[e[i].to]);
     45     }
     46     if(dfn[u] == low[u]) {
     47         int v;
     48         scc++;
     49         do {
     50             instack[v = stk.top()] = false;
     51             stk.pop();
     52             belong[v] = scc;
     53         } while(v!=u);
     54     }
     55 }
     56 bool ageCheck(int a) {
     57     return age[a]*n < sum;
     58 }
     59 bool TwoSAT() {
     60     for(int i = 1; i <= n*2; ++i)
     61         if(!dfn[i]) tarjan(i);
     62     for(int i = 1; i <= n; ++i) {
     63         if(belong[i] == belong[i+n]) return false;
     64         mark[belong[i]] = belong[i+n];
     65         mark[belong[i+n]] = belong[i];
     66     }
     67     for(int i = 1; i <= 2*n; ++i)
     68         for(int j = head[i]; ~j; j = e[j].next) {
     69             if(belong[i] != belong[e[j].to]) {
     70                 g[belong[e[j].to]].push_back(belong[i]);
     71                 ind[belong[i]]++;
     72             }
     73         }
     74     for(int i = 1; i <= scc; ++i)
     75         if(!ind[i]) q.push(i);
     76     while(!q.empty()) {
     77         int u = q.front();
     78         q.pop();
     79         if(!color[u]) {
     80             color[u] = 1;
     81             color[mark[u]] = -1;
     82         }
     83         for(int i = g[u].size()-1; i >= 0; --i)
     84             if(--ind[g[u][i]] == 0) q.push(g[u][i]);
     85     }
     86     return true;
     87 }
     88 int main() {
     89     int u,v;
     90     while(scanf("%d%d",&n,&m),n||m) {
     91         init();
     92         for(int i = 1; i <= n; ++i) {
     93             scanf("%d",age+i);
     94             sum += age[i];
     95         }
     96         for(int i = 0; i < m; ++i) {
     97             scanf("%d%d",&u,&v);
     98             if(ageCheck(u) == ageCheck(v)) {
     99                 add(u,v+n);
    100                 add(v,u+n);
    101             }
    102             add(u+n,v);
    103             add(v+n,u);
    104         }
    105         if(TwoSAT()) {
    106             for(int i = 1; i <= n; ++i) {
    107                 if(color[belong[i]] == -1) puts("C");
    108                 else if(ageCheck(i)) puts("B");
    109                 else puts("A");
    110             }
    111         } else puts("No solution.");
    112     }
    113     return 0;
    114 }
    View Code
  • 相关阅读:
    上传本地项目到git服务器
    linux服务器部署web环境(一)
    nginx+tomcat负载集群部署
    selenium学习历程(二)
    selenium学习历程(一)
    在 Acer p236 上装 win7 和 ubuntu 双系统
    Ubuntu (14.04) 如何安装和配置Qt
    Android 蓝牙的常用操作
    OpenCL: Shared memory between CPU and GPU in Android development of Qaulcomm plateform
    Android 系统支持的编解码器
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4698643.html
Copyright © 2011-2022 走看看