UVALive 3713 - Astronauts(2-SAT)

题目:

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=72618#problem/F

题意:

有A,B,C三个任务要分给n个宇航员,年龄大于等于平均年龄的分A, 年龄小于平均年龄的分在B,C组没有界限,互为敌人的两个宇航员不可以在同一组.求出一个分配的方案.

思路:

2-SAT.

同一年龄互为敌人:

x^y=1 !x^!y=1

不同年龄互为敌人:

!x^!y=1

注意如果用平均数来对比的话有精度问题,要用age[i]*n>=sum. 或 age[i]*n <sum

AC.

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>

using namespace std;
const int MAXN = 2e5+5;
int age[100005];
int n, m, sum;

int V;
vector<int> G[200005];
vector<int> rG[200005];
vector<int> vs;
bool used[200005];
int cmp[200005];

void addedge(int from, int to)
{
    G[from].push_back(to);
    rG[to].push_back(from);
}
void dfs(int v)
{
    used[v] = true;
    for(int i = 0; i < G[v].size(); ++i) {
        if(!used[G[v][i]]) dfs(G[v][i]);
    }
    vs.push_back(v);
}
void rdfs(int v, int k)
{

    used[v] = 1;
    cmp[v] = k;
    for(int i = 0; i < rG[v].size(); ++i) {
        if(!used[rG[v][i]]) rdfs(rG[v][i], k);
    }
}
void scc()
{
    memset(used, 0, sizeof(used));
    vs.clear();
    for(int v = 0; v < V; ++v) {
        if(!used[v]) dfs(v);
    }
    memset(used, 0, sizeof(used));
    int k = 0;
    for(int i = vs.size()-1; i >= 0; --i) {
        if(!used[vs[i]]) rdfs(vs[i], k++);
    }
}
void build()
{
    V = 2*n;
    for(int i = 0; i < V; ++i) {
        G[i].clear();
        rG[i].clear();
    }

    for(int i = 0; i < m; ++i) {
        int u, v;
        scanf("%d %d", &u, &v);
        u--; v--;
        if((age[u]*n>=sum&&age[v]*n<sum) || (age[u]*n<sum&&age[v]*n>=sum)) {
            addedge(n+u, v);
            addedge(n+v, u);
        }
        else {
            addedge(u, n+v);
            addedge(v, n+u);
            addedge(n+u, v);
            addedge(n+v, u);
        }
    }

}
void solve()
{
    build();
    scc();
    for(int i = 0; i < n; ++i) {
        if(cmp[i] == cmp[n+i]) {
            printf("No solution\n");
            return;
        }
    }
    for(int i = 0; i < n; ++i) {
        if(cmp[i] > cmp[n+i]) {
            if(age[i]*n >= sum) printf("A\n");
            else printf("B\n");
        }
        else printf("C\n");
    }
    return;
}
int main()
{
//freopen("in", "r", stdin);
    while(~scanf("%d %d", &n, &m)) {
        if(n == 0 && m == 0) break;

        sum = 0;
        for(int i = 0; i < n; ++i) {
            scanf("%d", &age[i]);
            sum += age[i];
        }

        solve();

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
Hello, today I want to introduce one of my favorite movies, Interstellar. Interstellar is a science-fiction drama film directed by Christopher Nolan, released in 2014. The movie takes place in a dystopian future where a blight caused by environmental changes has wiped out most of Earth's crops, making farming nearly impossible. Cooper, a former astronaut, is recruited by a secret NASA project to find a new home for the human race on another planet, as Earth is becoming uninhabitable. Cooper, played by actor Matthew McConaughey, leads a group of other astronauts into space, travelling through a wormhole to another galaxy in the hope of finding a suitable planet to populate. Along the way, they encounter several obstacles including physical dangers, time dilation, and the potential of never returning home. The movie explores complex scientific concepts such as the theory of relativity and the nature of time, while also examining the emotional toll that space travel has on individuals and family relationships. The soundtrack, composed by Hans Zimmer, further enhances the emotional impact of the film. Interstellar received critical acclaim for its innovative storytelling, stunning visual effects, and thought-provoking themes. The movie also earned five Oscar nominations and won the award for Best Visual Effects. Overall, Interstellar is an extraordinary film that pushes the boundaries of science-fiction and delivers a powerful narrative about humanity's search for a new home in the universe.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值