uva 1391 Astronauts(2-SAT)

/*翻译好题意 n个变量 不超过m*2句话*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 200010
using namespace std;
int n,m,f[maxn],c,s[maxn],age[maxn],sum,a,b;
vector<int>G[maxn];
bool Judge(int a,int b)
{
    if(age[a]*n<sum&&age[b]*n<sum)return 1;
    if(age[a]*n>=sum&&age[b]*n>=sum)return 1;
    return 0;
}
void Add(int x,int a,int y,int b)
{
    x=x*2+a;y=y*2+b;
    G[x^1].push_back(y);
    G[y^1].push_back(x);
}
bool Dfs(int x)
{
    if(f[x^1])return 0;if(f[x])return 1;
    f[x]=1;s[c++]=x;
    for(int i=0;i<G[x].size();i++)
      if(!Dfs(G[x][i]))return 0;
    return 1;
}
bool Solve()
{
    for(int i=0;i<n*2;i+=2)
      {
          if(f[i]||f[i+1])continue;c=0;
          if(!Dfs(i))
            {
                while(c>0)f[s[--c]]=0;
                if(!Dfs(i+1))return 0;
          }
      }
    return 1;
}
int main()
{
    while(1)
      {
          memset(f,0,sizeof(f));sum=0;
          for(int i=0;i<n*2;i++)G[i].clear();
          scanf("%d%d",&n,&m);if(n==0&&m==0)break;
          for(int i=0;i<n;i++)
            scanf("%d",&age[i]),sum+=age[i];
          for(int i=1;i<=m;i++)
            {
                scanf("%d%d",&a,&b);a--;b--;
                if(a==b)continue;Add(a,1,b,1);
                if(Judge(a,b))Add(a,0,b,0);
          }
        if(Solve()==0)printf("No solution\n");
        else for(int i=0;i<n;i++)
          {
              if(f[i*2])printf("C\n");
              else if(age[i]*n<sum)printf("B\n");
              else printf("A\n");
          }
      }
    return 0;
}

 

posted @ 2016-08-24 15:42  一入OI深似海  阅读(198)  评论(0编辑  收藏  举报