live archive 3713 two-sat

3713 - Astronauts

Time limit: 3.000 seconds

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 and 1$ \le$m$ \le$100000 . The number n is the number of astronauts. The next n lines specify the age of the nastronauts; each line contains a single integer number between 0 and 200. The next m lines contains two integers each, separated by a space. A line containing i and j (1$ \le$ij$ \le$n) means that the i -th astronaut and the j -th astronaut hate each other.

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

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 -th line describes which mission the i -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


题意:有A,B,C三个任务分配给n个宇航员,其中每个宇航员恰好要分配一个任务。设所有宇航员的平均年龄为x,年龄大于等于x的才能分配A任务,小于x才能分配B任务,C没有限制。有m对宇航员相互讨厌,不能分配到同一任务。输出一个满足上述要求的任务分配方案。

思路:

不难发现,每个宇航员只有两种选择,年龄大于等于x只能选择A或C,小于x的只能选择B或C,我们容易想到two-sat;设大于等于x的宇航员做任务A为true,做C为false,小于x的宇航员做B为true,做C为false;对于相互讨厌的宇航员i和j,建边我们考虑两种情况(偶为真,奇为假) (1)属于同一类型的,2*i->2*j+1, 2*j->2*i+1,2*i+1->2*j, 2*j+1->2*i   (2) 不同类型的, 2*i+1->2*j, 2*j+1->2*i ;之后two-sat一下。详见代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=100010;
vector<int>G[2*MAXN];
int s[2*MAXN],mark[2*MAXN];
double a[MAXN];
int n,m,cnt;
void init()
{
    for(int i=0;i<2*n;i++)
        G[i].clear();
    memset(mark,0,sizeof(mark));
}
bool dfs(int x)
{
    if(mark[x^1]) return false;
    if(mark[x]) return true;
    mark[x]=true;
    s[cnt++]=x;
    for(int i=0;i<G[x].size();i++)
        if(!dfs(G[x][i])) return false;
    return true;
}
bool solve()
{
    for(int i=0;i<2*n;i+=2)
        if(!mark[i]&&!mark[i+1])
    {
        cnt=0;
        if(!dfs(i))
        {
            while(cnt>0) mark[s[--cnt]]=false;
            if(!dfs(i+1)) return false;
        }
    }
    return true;
}
int main()
{
    //freopen("text.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF&&n&&m)
    {
        init();
        double k=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf",&a[i]);
            k+=a[i];
        }
        k/=n;
        int u,v;
       for(int i=0;i<m;i++)
       {
           scanf("%d%d",&u,&v);
           if(u==v) continue;
           u--; v--;
           if((a[u]>=k&&a[v]>=k)||(a[u]<k&&a[v]<k))
           {
               G[2*u].push_back(2*v+1);
               G[2*v].push_back(2*u+1);
           }
           G[2*u+1].push_back(2*v);
           G[2*v+1].push_back(2*u);
       }
        int flag=solve();
        if(flag)
        {
            for(int i=0;i<2*n;i+=2)
            {
                if(mark[i])
                {
                    if(a[i/2]>=k)
                        printf("A\n");
                    else
                        printf("B\n");
                }
                else
                    printf("C\n");

            }
        }
        else
            printf("No solution.\n");
    }
    return 0;
}

不过有个坑爹的地方,我把vector改为邻接表就跪了= =我只能说数据见鬼了。。

附邻接表代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=100000+100;
int n,m,edge_cnt,top;
double a[MAXN];
int head[2*MAXN],mark[2*MAXN],s[2*MAXN];
struct Edge
{
    int v;
    int next;
}edge[2*MAXN];
void init()
{
    edge_cnt=0;
    memset(mark,0,sizeof(mark));
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v)
{
    edge[edge_cnt].v=v;
    edge[edge_cnt].next=head[u];
    head[u]=edge_cnt++;
}
bool dfs(int x)
{
    if(mark[x^1]) return false;
    if(mark[x]) return true;
    mark[x]=true;
    s[top++]=x;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfs(v))
            return false;
    }
    return true;
}
bool solve()
{
    for(int i=0;i<2*n;i+=2)
        if(!mark[i]&&!mark[i+1])
        {
            top=0;
            if(!dfs(i))
            {
                while(top>0) mark[s[--top]]=false;
                if(!dfs(i+1)) return false;
            }
        }
    return true;
}
int main()
{
    //freopen("text.txt","r",stdin);
    while(~scanf("%d%d",&n,&m) && n+m)
    {
        printf("%d %d\n",n,m);
        init();
        double sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%lf",&a[i]);
            sum+=a[i];
        }
        sum/=n;

        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(u==v)
                continue;
            u--;v--;
            if((a[u]>=sum&&a[v]>=sum)||(a[u]<sum&&a[v]<sum))
            {
                addedge(2*u,2*v+1); addedge(2*v,2*u+1);
            }
            addedge(2*u+1,2*v); addedge(2*v+1,2*u);
        }
        int flag=solve();
        if(flag)
        {
            for(int i=0;i<2*n;i+=2)
            {
                if(mark[i])
                {
                    if(a[i/2]>=sum)
                        printf("A\n");
                    else
                        printf("B\n");
                }
                else
                    printf("C\n");

            }
        }
        else
            printf("No solution.\n");
    }
    return 0;
}


在车辆工程中,悬架系统的性能评估和优化一直是研究的热点。悬架不仅关乎车辆的乘坐舒适性,还直接影响到车辆的操控性和稳定性。为了深入理解悬架的动态行为,研究人员经常使用“二自由度悬架模型”来简化分析,并运用“传递函数”这一数学工具来描述悬架系统的动态特性。 二自由度悬架模型将复杂的车辆系统简化为两个独立的部分:车轮和车身。这种简化模型能够较准确地模拟出车辆在垂直方向上的运动行为,同时忽略了侧向和纵向的动态影响,这使得工程师能够更加专注于分析与优化与垂直动态相关的性能指标。 传递函数作为控制系统理论中的一种工具,能够描述系统输入和输出之间的关系。在悬架系统中,传递函数特别重要,因为它能够反映出路面不平度如何被悬架系统转化为车内乘员感受到的振动。通过传递函数,我们可以得到一个频率域上的表达式,从中分析出悬架系统的关键动态特性,如系统的振幅衰减特性和共振频率等。 在实际应用中,工程师通过使用MATLAB这类数学软件,建立双质量悬架的数学模型。模型中的参数包括车轮质量、车身质量、弹簧刚度以及阻尼系数等。通过编程求解,工程师可以得到悬架系统的传递函数,并据此绘制出传递函数曲线。这为评估悬架性能提供了一个直观的工具,使工程师能够了解悬架在不同频率激励下的响应情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值