HDOJ 1814 Peaceful Commissiont

本文探讨了一个关于和平委员会组成的2-SAT问题,通过构造特定的图结构,并使用染色法来寻找字典序最小的解决方案。介绍了问题背景、输入输出格式及样例,提供了完整的C++代码实现。

Peaceful Commission

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2775    Accepted Submission(s): 865


Problem Description
The Public Peace Commission should be legislated in Parliament of The Democratic Republic of Byteland according to The Very Important Law. Unfortunately one of the obstacles is the fact that some deputies do not get on with some others. 

The Commission has to fulfill the following conditions: 
1.Each party has exactly one representative in the Commission, 
2.If two deputies do not like each other, they cannot both belong to the Commission. 

Each party has exactly two deputies in the Parliament. All of them are numbered from 1 to 2n. Deputies with numbers 2i-1 and 2i belong to the i-th party . 

Task 
Write a program, which: 
1.reads from the text file SPO.IN the number of parties and the pairs of deputies that are not on friendly terms, 
2.decides whether it is possible to establish the Commission, and if so, proposes the list of members, 
3.writes the result in the text file SPO.OUT. 
 

Input
In the first line of the text file SPO.IN there are two non-negative integers n and m. They denote respectively: the number of parties, 1 <= n <= 8000, and the number of pairs of deputies, who do not like each other, 0 <= m <=2 0000. In each of the following m lines there is written one pair of integers a and b, 1 <= a < b <= 2n, separated by a single space. It means that the deputies a and b do not like each other. 
There are multiple test cases. Process to end of file. 
 

Output
The text file SPO.OUT should contain one word NIE (means NO in Polish), if the setting up of the Commission is impossible. In case when setting up of the Commission is possible the file SPO.OUT should contain n integers from the interval from 1 to 2n, written in the ascending order, indicating numbers of deputies who can form the Commission. Each of these numbers should be written in a separate line. If the Commission can be formed in various ways, your program may write mininum number sequence. 
 

Sample Input
  
3 2 1 3 2 4
 

Sample Output
  
1 4 5
 

Source
 

此题是一个标准的2-SAT问题,固然解决方法可以是用Tarjan算法,但是由于这道题需要我们进行输出的是字典序最小的解,用Tarjan来做的话可能不是太方便,所以我们采取染色法,网上看别人做这题基本也都写的染色法。染色法的用法,首先我们得搞清楚此方法建边的方法,先举个例子,如果1和3不能同时取,那么如果我们取1就必定会取到4,取2就必定会取到3,所以我们建边的方法是将不能同时取到的两点分别和另一个点同组的点建边。建好之后,我们先将一个未染色的点染红色,并将其一组的另一个点染成蓝色(颜色只是为了区分,并没有什么实际意义),然后运用刚刚建的边进行染色,如果出现染色失败,我们就折回讲同组的那个点染成红色,讲一开始选中的点染成蓝色,再往下进行染色,这个过程属于递归的过程,如果一个组的两个点都染色失败了,那么一定不存在一套染色方法。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 20000
#define W 0
#define R 1
#define B 2
int n, m;
vector<int> map[maxn];
int color[maxn];
// 用于存储尝试染色的结点,若失败则全部还原回白色
int temp[maxn], num;
bool dfs(int u)
{
    if(color[u] == R)
        return true;
    if(color[u] == B)
        return false;
    color[u] = R;// 结点未染色,试着染成红色并记录
    color[((u-1)^1)+1] = B;// 同组另一结点肯定淘汰了,蓝色
    temp[num++] = u;
    // 按照当前节点继续染色,看看是否会冲突
    // 冲突则代表当前这种染色办法不行
    // 那么就凭刚才做记录的temp数组还原结点(染白)
    for(int i=0; i<map[u].size(); i++)
        if(!dfs(map[u][i]))
            return false;
    return true;
}
bool solve()
{
    memset(color, W, sizeof(color));
    for(int i=1; i<=2*n; i++)
    {
        if(color[i] == W)// 此节点未访问过,试着染色
        {
            num = 0;
            if(!dfs(i))// 若染色失败
            {
                for(int j=0; j<num; j++)// 凭着染色时的记录,全部还原回白色
                {
                    color[temp[j]] = W;
                    color[((temp[j]-1)^1)+1] = W;
                }
                // 如果同组另一节点又失败了
                // 同组两个都不能选,无解
                if(!dfs(((i-1)^1)+1))
                    return false;
            }
        }
    }
    return true;
}
int main()
{
    int a, b;
    while(~scanf("%d%d", &n, &m))
    {
        for(int i=1; i<=2*n; i++)
        {
            map[i].clear();
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d", &a, &b);
            map[a].push_back(((b-1)^1)+1);
            map[b].push_back(((a-1)^1)+1);
        }
        if(solve())
        {
			for(int i=1; i<=2*n; i++)
                if(color[i] == R)
                    printf("%d\n",i);
		}
        else printf("NIE\n");
    }
    return 0;
}


基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值